Skip to content

Commit 2201d40

Browse files
committed
POM: be defensive if metadata is missing
When comparing POMs, apparently it is possible for groupId, artifactId and/or version to be missing/null. See: https://forum.image.sc/t/special-characters-no-longer-allowed-in-script-parameter-declaration/25244/3 Fortunately, Java 8 has a nice functional syntax to help with this.
1 parent 421ea3e commit 2201d40

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/main/java/org/scijava/util/POM.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,19 @@ public String getCIManagementURL() {
184184

185185
// -- Comparable methods --
186186

187-
@Override
188-
public int compareTo(final POM pom) {
187+
private static final Comparator<String> STRING_COMPARATOR = //
188+
Comparator.nullsFirst(String::compareTo);
189+
private static final Comparator<POM> POM_COMPARATOR = Comparator//
189190
// sort by groupId first
190-
final int gid = getGroupId().compareTo(pom.getGroupId());
191-
if (gid != 0) return gid;
192-
191+
.comparing(POM::getGroupId, STRING_COMPARATOR)
193192
// sort by artifactId second
194-
final int aid = getArtifactId().compareTo(pom.getArtifactId());
195-
if (aid != 0) return aid;
196-
193+
.thenComparing(POM::getArtifactId, STRING_COMPARATOR)//
197194
// finally, sort by version
198-
return compareVersions(getVersion(), pom.getVersion());
195+
.thenComparing(POM::getVersion, POM::compareVersions);
196+
197+
@Override
198+
public int compareTo(final POM pom) {
199+
return POM_COMPARATOR.compare(this, pom);
199200
}
200201

201202
// -- Versioned methods --

0 commit comments

Comments
 (0)