diff --git a/platform/util/src/com/intellij/util/text/SemVer.java b/platform/util/src/com/intellij/util/text/SemVer.java
index e5b829ff5101..e0ccb16e303e 100644
--- a/platform/util/src/com/intellij/util/text/SemVer.java
+++ b/platform/util/src/com/intellij/util/text/SemVer.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2013 JetBrains s.r.o.
+ * Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@ import org.jetbrains.annotations.Nullable;
/**
* Holds Semantic Version.
*/
-public class SemVer implements Comparable {
+public final class SemVer implements Comparable {
public static final SemVer UNKNOWN = new SemVer("?", 0, 0, 0);
private final String myRawVersion;
@@ -59,18 +59,24 @@ public class SemVer implements Comparable {
return myMajor + "." + myMinor + "." + myPatch;
}
+ @Override
+ public int compareTo(SemVer other) {
+ int diff = myMajor - other.myMajor;
+ if (diff != 0) return diff;
+
+ diff = myMinor - other.myMinor;
+ if (diff != 0) return diff;
+
+ return myPatch - other.myPatch;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SemVer semVer = (SemVer)o;
-
- if (myMajor != semVer.myMajor) return false;
- if (myMinor != semVer.myMinor) return false;
- if (myPatch != semVer.myPatch) return false;
-
- return true;
+ return myMajor == semVer.myMajor && myMinor == semVer.myMinor && myPatch == semVer.myPatch;
}
@Override
@@ -87,51 +93,35 @@ public class SemVer implements Comparable {
}
@Nullable
- public static SemVer parseFromText(@NotNull String text) {
- int majorEndInd = text.indexOf('.');
- if (majorEndInd < 0) {
- return null;
- }
- int major = StringUtil.parseInt(text.substring(0, majorEndInd), -1);
- int minorEndInd = text.indexOf('.', majorEndInd + 1);
- if (minorEndInd < 0) {
- return null;
- }
- int minor = StringUtil.parseInt(text.substring(majorEndInd + 1, minorEndInd), -1);
- final String patchStr;
- int dashInd = text.indexOf('-', minorEndInd + 1);
- if (dashInd >= 0) {
- patchStr = text.substring(minorEndInd + 1, dashInd);
- }
- else {
- patchStr = text.substring(minorEndInd + 1);
- }
- int patch = StringUtil.parseInt(patchStr, -1);
- if (major >= 0 && minor >= 0 && patch >= 0) {
- return new SemVer(text, major, minor, patch);
+ public static SemVer parseFromText(String text) {
+ if (text != null) {
+ int majorEndIdx = text.indexOf('.');
+ if (majorEndIdx >= 0) {
+ int minorEndIdx = text.indexOf('.', majorEndIdx + 1);
+ if (minorEndIdx >= 0) {
+ int patchEndIdx = text.indexOf('-', minorEndIdx + 1);
+ if (patchEndIdx < 0) patchEndIdx = text.length();
+
+ int major = StringUtil.parseInt(text.substring(0, majorEndIdx), -1);
+ int minor = StringUtil.parseInt(text.substring(majorEndIdx + 1, minorEndIdx), -1);
+ int patch = StringUtil.parseInt(text.substring(minorEndIdx + 1, patchEndIdx), -1);
+ if (major >= 0 && minor >= 0 && patch >= 0) {
+ return new SemVer(text, major, minor, patch);
+ }
+ }
+ }
}
+
return null;
}
- @NotNull
+ //
+ /** @deprecated use {@code ObjectUtils.notNull(SemVer.parseFromText(...), SemVer.UNKNOWN)} (to be removed in IDEA 2019) */
+ @SuppressWarnings("SpellCheckingInspection")
public static SemVer parseFromTextNonNullize(@Nullable final String text) {
if (text == null) return UNKNOWN;
final SemVer ver = parseFromText(text);
return ver == null ? UNKNOWN : ver;
}
-
- @Override
- public int compareTo(SemVer other) {
- // null is not permitted
- if (getMajor() != other.getMajor()) {
- return getMajor() - other.getMajor();
- }
- if (getMinor() != other.getMinor()) {
- return getMinor() - other.getMinor();
- }
- if (getPatch() != other.getPatch()) {
- return getPatch() - other.getPatch();
- }
- return 0;
- }
-}
+ //
+}
\ No newline at end of file
diff --git a/platform/util/testSrc/com/intellij/util/text/SemVerTest.java b/platform/util/testSrc/com/intellij/util/text/SemVerTest.java
index 00f39f14aa73..543eb5681b01 100644
--- a/platform/util/testSrc/com/intellij/util/text/SemVerTest.java
+++ b/platform/util/testSrc/com/intellij/util/text/SemVerTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2014 JetBrains s.r.o.
+ * Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,58 +15,56 @@
*/
package com.intellij.util.text;
-import junit.framework.TestCase;
-import org.jetbrains.annotations.NotNull;
+import org.junit.Test;
-public class SemVerTest extends TestCase {
- public void testParsing() throws Exception {
- checkParsed("0.9.2", 0, 9, 2);
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SemVerTest {
+ @Test
+ public void parsing() {
+ assertParsed("0.9.2", 0, 9, 2);
+ assertParsed("0.9.2-", 0, 9, 2);
+ assertParsed("0.9.2-dart", 0, 9, 2);
+ assertParsed("4.0.0-alpha.1", 4, 0, 0);
+ assertParsed("0.10.0-rc-1", 0, 10, 0);
+ assertParsed("1.0.0-rc-1", 1, 0, 0);
+ assertParsed("1.0.0-alpha", 1, 0, 0);
+ assertParsed("1.0.0-0.3.7", 1, 0, 0);
+ assertParsed("1.0.0-x.7.z.92", 1, 0, 0);
+
+ assertNotParsed(null);
+ assertNotParsed("");
+ assertNotParsed("1.0.a");
+ assertNotParsed("1.0");
+ assertNotParsed("1..a");
}
- public void testExtendedVersion() throws Exception {
- checkParsed("0.9.2-dart", 0, 9, 2);
+ @Test
+ public void comparing() {
+ assertThat(parse("1.0.0")).isGreaterThan(parse("0.10.0"));
+ assertThat(parse("1.0.0")).isLessThan(parse("2.10.0"));
+
+ assertThat(parse("0.30.0")).isGreaterThan(parse("0.5.1000"));
+ assertThat(parse("0.30.10")).isLessThan(parse("0.100.0"));
+
+ assertThat(parse("2.9.123-test")).isGreaterThan(parse("2.9.100"));
+ assertThat(parse("2.9.123-test")).isLessThan(parse("2.9.124"));
+
+ assertThat(parse("11.123.0")).isEqualTo(parse("11.123.0"));
+ assertThat(parse("11.123.0")).isEqualByComparingTo(parse("11.123.0"));
}
- public void testGulp4Alpha() throws Exception {
- checkParsed("4.0.0-alpha.1", 4, 0, 0);
+ private static void assertParsed(String version, int expectedMajor, int expectedMinor, int expectedPatch) {
+ assertThat(parse(version)).isEqualTo(new SemVer(version, expectedMajor, expectedMinor, expectedPatch));
}
- public void testMisc() throws Exception {
- checkParsed("0.10.0-rc-1", 0, 10, 0);
- checkParsed("1.0.0-rc-1", 1, 0, 0);
- checkParsed("1.0.0-alpha", 1, 0, 0);
- checkParsed("1.0.0-0.3.7", 1, 0, 0);
- checkParsed("1.0.0-x.7.z.92", 1, 0, 0);
- checkNotParsed("1.0.a");
- checkNotParsed("1.0");
- checkNotParsed("1..a");
+ private static void assertNotParsed(String version) {
+ assertThat(SemVer.parseFromText(version)).isNull();
}
- private static void checkParsed(@NotNull String version, int expectedMajor, int expectedMinor, int expectedPatch) {
- assertEquals(new SemVer(version, expectedMajor, expectedMinor, expectedPatch), parseNotNull(version));
- }
-
- private static void checkNotParsed(@NotNull String version) {
- assertNull(SemVer.parseFromText(version));
- }
-
- public void testCompare() throws Exception {
- assertTrue(parseNotNull("1.0.0").compareTo(parseNotNull("0.10.0")) > 0);
- assertTrue(parseNotNull("1.0.0").compareTo(parseNotNull("2.10.0")) < 0);
-
- assertTrue(parseNotNull("0.30.0").compareTo(parseNotNull("0.5.1000")) > 0);
- assertTrue(parseNotNull("0.30.10").compareTo(parseNotNull("0.100.0")) < 0);
-
- assertTrue(parseNotNull("2.9.123-test").compareTo(parseNotNull("2.9.100")) > 0);
- assertTrue(parseNotNull("2.9.123-test").compareTo(parseNotNull("2.9.124")) < 0);
-
- assertTrue(parseNotNull("11.123.0").compareTo(parseNotNull("11.123.0")) == 0);
- }
-
- @NotNull
- private static SemVer parseNotNull(@NotNull String text) {
+ private static SemVer parse(String text) {
SemVer semVer = SemVer.parseFromText(text);
- assertNotNull(semVer);
+ assertThat(semVer).describedAs(text).isNotNull();
return semVer;
}
-}
+}
\ No newline at end of file