use of org.commonjava.maven.atlas.ident.version.SingleVersion in project indy by Commonjava.
the class PackageMetadata method merge.
public boolean merge(PackageMetadata source) {
Logger logger = LoggerFactory.getLogger(getClass());
boolean changed = false;
if (source.getName() != null) {
this.setName(source.getName());
changed = true;
}
if (source.getDescription() != null) {
this.setDescription(source.getDescription());
changed = true;
}
if (source.getAuthor() != null) {
this.setAuthor(source.getAuthor());
changed = true;
}
if (source.getRepository() != null) {
this.setRepository(source.getRepository());
changed = true;
}
if (source.getReadme() != null) {
this.setReadme(source.getReadme());
changed = true;
}
if (source.getReadmeFilename() != null) {
this.setReadmeFilename(source.getReadmeFilename());
changed = true;
}
if (source.getHomepage() != null) {
this.setHomepage(source.getHomepage());
changed = true;
}
if (source.getBugs() != null) {
this.setBugs(source.getBugs());
changed = true;
}
if (source.getLicense() != null) {
this.setLicense(source.getLicense());
changed = true;
}
// merge maintainers list
Iterator maintainer = source.getMaintainers().iterator();
while (maintainer.hasNext()) {
UserInfo m = (UserInfo) maintainer.next();
if (!maintainers.contains(m)) {
this.addMaintainers(new UserInfo(m.getName(), m.getEmail(), m.getUrl()));
changed = true;
}
}
// merge keywords list
Iterator keyword = source.getKeywords().iterator();
while (keyword.hasNext()) {
String key = (String) keyword.next();
if (!keywords.contains(key)) {
this.addKeywords(key);
changed = true;
}
}
// merge users map
Map<String, Boolean> sourceUsers = source.getUsers();
for (final String user : sourceUsers.keySet()) {
if (users.containsKey(user) && users.get(user).equals(sourceUsers.get(user))) {
continue;
}
users.put(user, sourceUsers.get(user));
changed = true;
}
// merge time map
SimpleDateFormat sdf = new SimpleDateFormat(TIMEOUT_FORMAT);
Map<String, Date> clone = new LinkedHashMap<>();
for (final String key : time.keySet()) {
String value = time.get(key);
Date date = null;
try {
date = sdf.parse(value);
} catch (ParseException e) {
logger.error(String.format("Cannot parse date: %s. Reason: %s", value, e));
}
clone.put(key, date);
}
Map<String, String> sourceTimes = source.getTime();
boolean added = false;
for (final String key : sourceTimes.keySet()) {
String value = sourceTimes.get(key);
Date date = null;
try {
date = sdf.parse(value);
} catch (ParseException e) {
logger.error(String.format("Cannot parse date: %s. Reason: %s", value, e));
}
// if source's version update time is more recent(sort as the letter order), will update it into the original map.
if (clone.containsKey(key) && date.compareTo(clone.get(key)) <= 0) {
continue;
}
clone.put(key, date);
added = true;
changed = true;
}
// only sorting the map when update occurred
if (added) {
// sort as the time value in map
List<Map.Entry<String, Date>> timeList = new ArrayList<>(clone.entrySet());
// sort the time as value (update time) asc
Collections.sort(timeList, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));
Map<String, String> result = new LinkedHashMap<>();
// make the 'modified' and 'created' value as the first two keys in final map
if (clone.get(MODIFIED) != null) {
result.put(MODIFIED, sdf.format(clone.get(MODIFIED)));
}
if (clone.get(CREATED) != null) {
result.put(CREATED, sdf.format(clone.get(CREATED)));
}
for (final Map.Entry<String, Date> entry : timeList) {
if (MODIFIED.equals(entry.getKey()) || CREATED.equals(entry.getKey())) {
continue;
}
result.put(entry.getKey(), sdf.format(entry.getValue()));
}
time = result;
}
// merge dist-tag object
DistTag sourceDist = source.getDistTags();
Map<String, String> sourceDistMap = sourceDist.fetchTagsMap();
Map<String, String> thisDistMap = distTags.fetchTagsMap();
if (thisDistMap.isEmpty() && !sourceDistMap.isEmpty()) {
this.setDistTags(sourceDist);
changed = true;
} else {
for (final String tag : sourceDistMap.keySet()) {
SingleVersion sourceVersion = VersionUtils.createSingleVersion(sourceDistMap.get(tag));
SingleVersion thisVersion = VersionUtils.createSingleVersion(thisDistMap.get(tag));
if (thisDistMap.containsKey(tag) && sourceVersion.compareTo(thisVersion) <= 0) {
continue;
}
distTags.putTag(tag, sourceDistMap.get(tag));
changed = true;
}
}
//merge versions, keep the first version metadata for a given version that coming across
Map<String, VersionMetadata> sourceVersions = source.getVersions();
for (final String v : sourceVersions.keySet()) {
VersionMetadata value = sourceVersions.get(v);
if (!versions.containsKey(v)) {
versions.put(v, value);
changed = true;
}
}
return changed;
}
use of org.commonjava.maven.atlas.ident.version.SingleVersion in project galley by Commonjava.
the class VersionResolverImpl method resolveLatestSnapshotRefWithLocation.
private ProjectVersionRefLocation resolveLatestSnapshotRefWithLocation(final List<? extends Location> locations, final ProjectVersionRef ref, final VersionSelectionStrategy selectionStrategy, final EventMetadata eventMetadata) throws TransferException {
final Map<SingleVersion, Location> available = new TreeMap<>();
for (final Location location : locations) {
try {
final MavenMetadataView metadata = metadataReader.getMetadata(ref, Collections.singletonList(location), eventMetadata);
if (metadata != null) {
addSnapshotFrom(metadata, location, ref, available);
}
} catch (final GalleyMavenException e) {
debug("Failed to resolve/parse metadata for snapshot version of: %s from: %s.", e, ref, location);
}
}
if (available.isEmpty()) {
return null;
}
final List<SingleVersion> versions = new ArrayList<>(available.keySet());
Collections.sort(versions);
final SingleVersion selected = selectionStrategy.select(versions);
if (selected == null) {
return null;
}
return new ProjectVersionRefLocation(ref.selectVersion(selected), available.get(selected));
}
use of org.commonjava.maven.atlas.ident.version.SingleVersion in project galley by Commonjava.
the class VersionResolverImpl method resolveAllMultiRefsWithLocations.
private List<ProjectVersionRefLocation> resolveAllMultiRefsWithLocations(final List<? extends Location> locations, final ProjectVersionRef ref, final VersionSelectionStrategy selectionStrategy, final EventMetadata eventMetadata) throws TransferException {
final Map<SingleVersion, Location> available = new TreeMap<>();
for (final Location location : locations) {
try {
final MavenMetadataView metadata = metadataReader.getMetadata(ref.asProjectRef(), Collections.singletonList(location), eventMetadata);
if (metadata != null) {
final List<String> versions = metadata.resolveValues("/metadata/versioning/versions/version");
if (versions != null) {
for (final String version : versions) {
try {
final SingleVersion spec = VersionUtils.createSingleVersion(version);
if (!available.containsKey(spec)) {
available.put(spec, location);
}
} catch (final InvalidVersionSpecificationException e) {
debug("Unparsable version spec found in metadata: '%s' for: %s from: %s.", e, version, ref, location);
}
}
}
}
} catch (final GalleyMavenException e) {
debug("Failed to resolve/parse metadata for variable version of: '%s' from: %s.", e, ref, location);
}
}
if (!available.isEmpty()) {
final List<ProjectVersionRefLocation> result = new ArrayList<>();
final VersionSpec spec = ref.getVersionSpec();
final List<SingleVersion> versions = new ArrayList<>(available.keySet());
Collections.sort(versions);
while (!versions.isEmpty()) {
final SingleVersion selected = selectionStrategy.select(versions);
if (selected != null) {
versions.remove(selected);
if (selected.isConcrete() && spec.contains(selected)) {
result.add(new ProjectVersionRefLocation(ref.selectVersion(selected), available.get(selected)));
}
}
}
return result;
}
return Collections.emptyList();
}
Aggregations