GradleCoordinate { @Nullable public static GradleCoordinate parseCoordinateString(@NonNull String coordinateString) { if (coordinateString == null) { return null; } Matcher matcher = MAVEN_PATTERN.matcher(coordinateString); if (!matcher.matches()) { return null; } String groupId = matcher.group(1); String artifactId = matcher.group(2); String revision = matcher.group(3); String typeString = matcher.group(4); ArtifactType type = null; if (typeString != null) { type = ArtifactType.getArtifactType(typeString.substring(1)); } List<RevisionComponent> revisions = parseRevisionNumber(revision); return new GradleCoordinate(groupId, artifactId, revisions, type); } GradleCoordinate(@NonNull String groupId, @NonNull String artifactId,
            @NonNull RevisionComponent... revisions);  GradleCoordinate(@NonNull String groupId, @NonNull String artifactId,
            @NonNull String revision);  GradleCoordinate(@NonNull String groupId, @NonNull String artifactId,
            @NonNull int... revisions);  GradleCoordinate(@NonNull String groupId, @NonNull String artifactId,
            @NonNull List<RevisionComponent> revisions, @Nullable ArtifactType type); @Nullable static GradleCoordinate parseCoordinateString(@NonNull String coordinateString); static GradleCoordinate parseVersionOnly(@NonNull String revision); @NonNull static List<RevisionComponent> parseRevisionNumber(@NonNull String revision); @Override String toString(); @Nullable String getGroupId(); @Nullable String getArtifactId(); @Nullable ArtifactType getArtifactType(); @Nullable String getId(); @Nullable ArtifactType getType(); boolean acceptsGreaterRevisions(); @NonNull String getRevision(); boolean isPreview(); int getMajorVersion(); int getMinorVersion(); int getMicroVersion(); boolean isSameArtifact(@NonNull GradleCoordinate o); boolean matches(@NonNull GradleCoordinate pattern); @Override boolean equals(@NonNull Object o); @Override int hashCode(); static final String PREVIEW_ID; static final PlusComponent PLUS_REV; static final int PLUS_REV_VALUE; static final Comparator<GradleCoordinate> COMPARE_PLUS_LOWER; static final Comparator<GradleCoordinate> COMPARE_PLUS_HIGHER; }
@Test public void testCompareVersions() { GradleCoordinate a = new GradleCoordinate("a.b.c", "package", 5, 4, 2); GradleCoordinate b = new GradleCoordinate("a.b.c", "package", 5, 5, 5); assertTrue(COMPARE_PLUS_HIGHER.compare(a, b) < 0); assertTrue(COMPARE_PLUS_HIGHER.compare(b, a) > 0); a = new GradleCoordinate("a.b.c", "package", 5, 4, 10); b = new GradleCoordinate("a.b.c", "package", 5, 4, GradleCoordinate.PLUS_REV_VALUE); assertTrue(COMPARE_PLUS_HIGHER.compare(a, b) < 0); a = new GradleCoordinate("a.b.c", "package", 5, 6, GradleCoordinate.PLUS_REV_VALUE); b = new GradleCoordinate("a.b.c", "package", 6, 0, 0); assertTrue(COMPARE_PLUS_HIGHER.compare(a, b) < 0); a = new GradleCoordinate("a.b.c", "package", 5, 6, 0); b = new GradleCoordinate("a.b.c", "package", 5, 6, 0); assertTrue(COMPARE_PLUS_HIGHER.compare(a, b) == 0); a = new GradleCoordinate("a.b.c", "package", 5, 4, 2); b = new GradleCoordinate("a.b.c", "feature", 5, 4, 2); assertTrue((COMPARE_PLUS_HIGHER.compare(a, b) < 0) == ("package".compareTo("feature") < 0)); a = new GradleCoordinate("a.b.c", "package", 5, 6, 0); b = new GradleCoordinate("a.b.c", "package", 5, 6, GradleCoordinate.PLUS_REV_VALUE); assertTrue(COMPARE_PLUS_HIGHER.compare(a, b) < 0); a = new GradleCoordinate("a.b.c", "package", 5, 6, 0); b = new GradleCoordinate("a.b.c", "package", 5, GradleCoordinate.PLUS_REV_VALUE); assertTrue(COMPARE_PLUS_HIGHER.compare(a, b) < 0); a = GradleCoordinate.parseCoordinateString("a.b.c:package:5.4.2"); b = GradleCoordinate.parseCoordinateString("a.b.c:package:5.4.+"); assert a != null; assert b != null; assertTrue(COMPARE_PLUS_HIGHER.compare(a, b) < 0); assertTrue(COMPARE_PLUS_HIGHER.compare(b, a) > 0); a = GradleCoordinate.parseCoordinateString("a.b.c:package:5"); b = GradleCoordinate.parseCoordinateString("a.b.c:package:+"); assert a != null; assert b != null; assertTrue(COMPARE_PLUS_HIGHER.compare(a, b) < 0); assertTrue(COMPARE_PLUS_HIGHER.compare(b, a) > 0); a = GradleCoordinate.parseCoordinateString("a.b.c:package:1.any"); b = GradleCoordinate.parseCoordinateString("a.b.c:package:1.1"); assertTrue(COMPARE_PLUS_HIGHER.compare(a, b) < 0); assertTrue(COMPARE_PLUS_HIGHER.compare(b, a) > 0); a = GradleCoordinate.parseCoordinateString("a.b.c:package:1-1"); b = GradleCoordinate.parseCoordinateString("a.b.c:package:1-2"); assertTrue(COMPARE_PLUS_HIGHER.compare(a, b) < 0); assertTrue(COMPARE_PLUS_HIGHER.compare(b, a) > 0); }
@Test public void testCompareSpecificity() { GradleCoordinate a = new GradleCoordinate("a.b.c", "package", 5, 4, 2); GradleCoordinate b = new GradleCoordinate("a.b.c", "package", 5, 5, 5); assertTrue(COMPARE_PLUS_LOWER.compare(a, b) < 0); assertTrue(COMPARE_PLUS_LOWER.compare(b, a) > 0); a = new GradleCoordinate("a.b.c", "package", 5, 4, 10); b = new GradleCoordinate("a.b.c", "package", 5, 4, GradleCoordinate.PLUS_REV_VALUE); assertTrue(COMPARE_PLUS_LOWER.compare(a, b) > 0); a = new GradleCoordinate("a.b.c", "package", 5, 6, GradleCoordinate.PLUS_REV_VALUE); b = new GradleCoordinate("a.b.c", "package", 6, 0, 0); assertTrue(COMPARE_PLUS_LOWER.compare(a, b) < 0); a = new GradleCoordinate("a.b.c", "package", 5, 6, 0); b = new GradleCoordinate("a.b.c", "package", 5, 6, 0); assertTrue(COMPARE_PLUS_LOWER.compare(a, b) == 0); a = new GradleCoordinate("a.b.c", "package", 5, 4, 2); b = new GradleCoordinate("a.b.c", "feature", 5, 4, 2); assertTrue((COMPARE_PLUS_LOWER.compare(a, b) < 0) == ("package".compareTo("feature") < 0)); a = new GradleCoordinate("a.b.c", "package", 5, 6, 0); b = new GradleCoordinate("a.b.c", "package", 5, 6, GradleCoordinate.PLUS_REV_VALUE); assertTrue(COMPARE_PLUS_LOWER.compare(a, b) > 0); a = new GradleCoordinate("a.b.c", "package", 5, 6, 0); b = new GradleCoordinate("a.b.c", "package", 5, GradleCoordinate.PLUS_REV_VALUE); assertTrue(COMPARE_PLUS_LOWER.compare(a, b) > 0); a = GradleCoordinate.parseCoordinateString("a.b.c:package:5.4.2"); b = GradleCoordinate.parseCoordinateString("a.b.c:package:5.4.+"); assert a != null; assert b != null; assertTrue(COMPARE_PLUS_LOWER.compare(a, b) > 0); assertTrue(COMPARE_PLUS_LOWER.compare(b, a) < 0); }