Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>// VersionComparator.java import java.util.Comparator; public class VersionComparator implements Comparator { public boolean equals(Object o1, Object o2) { return compare(o1, o2) == 0; } public int compare(Object o1, Object o2) { String version1 = (String) o1; String version2 = (String) o2; VersionTokenizer tokenizer1 = new VersionTokenizer(version1); VersionTokenizer tokenizer2 = new VersionTokenizer(version2); int number1 = 0, number2 = 0; String suffix1 = "", suffix2 = ""; while (tokenizer1.MoveNext()) { if (!tokenizer2.MoveNext()) { do { number1 = tokenizer1.getNumber(); suffix1 = tokenizer1.getSuffix(); if (number1 != 0 || suffix1.length() != 0) { // Version one is longer than number two, and non-zero return 1; } } while (tokenizer1.MoveNext()); // Version one is longer than version two, but zero return 0; } number1 = tokenizer1.getNumber(); suffix1 = tokenizer1.getSuffix(); number2 = tokenizer2.getNumber(); suffix2 = tokenizer2.getSuffix(); if (number1 &lt; number2) { // Number one is less than number two return -1; } if (number1 &gt; number2) { // Number one is greater than number two return 1; } boolean empty1 = suffix1.length() == 0; boolean empty2 = suffix2.length() == 0; if (empty1 &amp;&amp; empty2) continue; // No suffixes if (empty1) return 1; // First suffix is empty (1.2 &gt; 1.2b) if (empty2) return -1; // Second suffix is empty (1.2a &lt; 1.2) // Lexical comparison of suffixes int result = suffix1.compareTo(suffix2); if (result != 0) return result; } if (tokenizer2.MoveNext()) { do { number2 = tokenizer2.getNumber(); suffix2 = tokenizer2.getSuffix(); if (number2 != 0 || suffix2.length() != 0) { // Version one is longer than version two, and non-zero return -1; } } while (tokenizer2.MoveNext()); // Version two is longer than version one, but zero return 0; } return 0; } } // VersionTokenizer.java public class VersionTokenizer { private final String _versionString; private final int _length; private int _position; private int _number; private String _suffix; private boolean _hasValue; public int getNumber() { return _number; } public String getSuffix() { return _suffix; } public boolean hasValue() { return _hasValue; } public VersionTokenizer(String versionString) { if (versionString == null) throw new IllegalArgumentException("versionString is null"); _versionString = versionString; _length = versionString.length(); } public boolean MoveNext() { _number = 0; _suffix = ""; _hasValue = false; // No more characters if (_position &gt;= _length) return false; _hasValue = true; while (_position &lt; _length) { char c = _versionString.charAt(_position); if (c &lt; '0' || c &gt; '9') break; _number = _number * 10 + (c - '0'); _position++; } int suffixStart = _position; while (_position &lt; _length) { char c = _versionString.charAt(_position); if (c == '.') break; _position++; } _suffix = _versionString.substring(suffixStart, _position); if (_position &lt; _length) _position++; return true; } } </code></pre> <p><strong>Example:</strong></p> <pre><code>public class Main { private static VersionComparator cmp; public static void main (String[] args) { cmp = new VersionComparator(); Test(new String[]{"1.1.2", "1.2", "1.2.0", "1.2.1", "1.12"}); Test(new String[]{"1.3", "1.3a", "1.3b", "1.3-SNAPSHOT"}); } private static void Test(String[] versions) { for (int i = 0; i &lt; versions.length; i++) { for (int j = i; j &lt; versions.length; j++) { Test(versions[i], versions[j]); } } } private static void Test(String v1, String v2) { int result = cmp.compare(v1, v2); String op = "=="; if (result &lt; 0) op = "&lt;"; if (result &gt; 0) op = "&gt;"; System.out.printf("%s %s %s\n", v1, op, v2); } } </code></pre> <p><strong>Output:</strong></p> <pre><code>1.1.2 == 1.1.2 ---&gt; same length and value 1.1.2 &lt; 1.2 ---&gt; first number (1) less than second number (2) =&gt; -1 1.1.2 &lt; 1.2.0 ---&gt; first number (1) less than second number (2) =&gt; -1 1.1.2 &lt; 1.2.1 ---&gt; first number (1) less than second number (2) =&gt; -1 1.1.2 &lt; 1.12 ---&gt; first number (1) less than second number (12) =&gt; -1 1.2 == 1.2 ---&gt; same length and value 1.2 == 1.2.0 ---&gt; first shorter than second, but zero 1.2 &lt; 1.2.1 ---&gt; first shorter than second, and non-zero 1.2 &lt; 1.12 ---&gt; first number (2) less than second number (12) =&gt; -1 1.2.0 == 1.2.0 ---&gt; same length and value 1.2.0 &lt; 1.2.1 ---&gt; first number (0) less than second number (1) =&gt; -1 1.2.0 &lt; 1.12 ---&gt; first number (2) less than second number (12) =&gt; -1 1.2.1 == 1.2.1 ---&gt; same length and value 1.2.1 &lt; 1.12 ---&gt; first number (2) less than second number (12) =&gt; -1 1.12 == 1.12 ---&gt; same length and value 1.3 == 1.3 ---&gt; same length and value 1.3 &gt; 1.3a ---&gt; first suffix ('') is empty, but not second ('a') =&gt; 1 1.3 &gt; 1.3b ---&gt; first suffix ('') is empty, but not second ('b') =&gt; 1 1.3 &gt; 1.3-SNAPSHOT ---&gt; first suffix ('') is empty, but not second ('-SNAPSHOT') =&gt; 1 1.3a == 1.3a ---&gt; same length and value 1.3a &lt; 1.3b ---&gt; first suffix ('a') compared to second suffix ('b') =&gt; -1 1.3a &lt; 1.3-SNAPSHOT ---&gt; first suffix ('a') compared to second suffix ('-SNAPSHOT') =&gt; -1 1.3b == 1.3b ---&gt; same length and value 1.3b &lt; 1.3-SNAPSHOT ---&gt; first suffix ('b') compared to second suffix ('-SNAPSHOT') =&gt; -1 1.3-SNAPSHOT == 1.3-SNAPSHOT ---&gt; same length and value </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload