@Test public void test() { compare( 0, "", ""); compare( 0, "1", "1"); compare( 0, null, null); compare( 1, "1", null); compare( 1, "1", ""); compare(-1, "1", "1.1"); compare(-1, "1", "1.10"); compare( 1, "1.10", "1.9"); compare(-1, "1.1", "1.2"); compare(-1, "1.1.123", "1.9.123"); compare( 0, "1.9.123", "1.9.123"); compare( 1, "1.9.124", "1.9.123"); compare(-1, "1.9.124", "latest"); compare(-1, "latest", "nightly"); compare(-1, "1.9_rc", "1.9"); compare( 1, "1.9_ga", "1.9_rc"); compare( 0, "1.9_ga", "1.9_ga"); compare(-1, "1.9_ga", "1.19_ga"); compare(-1, "1.8a", "1.10"); compare(-1, "1.8a", "1.8"); compare(-1,"0.2-dev.1-sha.23f4399.dirty", "0.12-dev.10-sha.9984399"); }
@Override public int compare(String left, String right) { if(left == null || right == null) { if(left == null) { return (right == null)? 0 : -1; } return 1; } if(left.equals(right)) { return 0; } final int llen = left.length(); final int rlen = right.length(); if (llen == 0 || rlen == 0) { return Integer.compare(llen, rlen); } Integer lo = latestMap.get(left); Integer ro = latestMap.get(right); if(lo != null || ro != null) { return compareOrders(lo, ro); } int lpp = 0, rpp = 0, llp = 0, rlp = 0; while(true) { llp = left.indexOf('.', llp); if(llp < 0) { llp = llen; } rlp = right.indexOf('.', rlp); if(rlp < 0) { rlp = rlen; } int lne = getNumEnd(left, lpp); int rne = getNumEnd(right, rpp); if (lne > 0 && rne > 0) { int res = compareTokens(left.substring(lpp, lne), right.substring(rpp, rne)); if(res != 0) { return res; } lpp = lne; rpp = rne; } boolean end = llp == llen && rlp == rlen; if(lpp < llp || rpp < rlp || end) { String ltoken = left.substring(lpp, llp); String rtoken = right.substring(rpp, rlp); int res = compareEnds(ltoken, rtoken); if (res != 0 || end) { return res; } } lpp = Math.min(++llp, llen); rpp = Math.min(++rlp, rlen); } }
VersionComparator implements Comparator<String> { @Override public int compare(String left, String right) { if(left == null || right == null) { if(left == null) { return (right == null)? 0 : -1; } return 1; } if(left.equals(right)) { return 0; } final int llen = left.length(); final int rlen = right.length(); if (llen == 0 || rlen == 0) { return Integer.compare(llen, rlen); } Integer lo = latestMap.get(left); Integer ro = latestMap.get(right); if(lo != null || ro != null) { return compareOrders(lo, ro); } int lpp = 0, rpp = 0, llp = 0, rlp = 0; while(true) { llp = left.indexOf('.', llp); if(llp < 0) { llp = llen; } rlp = right.indexOf('.', rlp); if(rlp < 0) { rlp = rlen; } int lne = getNumEnd(left, lpp); int rne = getNumEnd(right, rpp); if (lne > 0 && rne > 0) { int res = compareTokens(left.substring(lpp, lne), right.substring(rpp, rne)); if(res != 0) { return res; } lpp = lne; rpp = rne; } boolean end = llp == llen && rlp == rlen; if(lpp < llp || rpp < rlp || end) { String ltoken = left.substring(lpp, llp); String rtoken = right.substring(rpp, rlp); int res = compareEnds(ltoken, rtoken); if (res != 0 || end) { return res; } } lpp = Math.min(++llp, llen); rpp = Math.min(++rlp, rlen); } } }
VersionComparator implements Comparator<String> { @Override public int compare(String left, String right) { if(left == null || right == null) { if(left == null) { return (right == null)? 0 : -1; } return 1; } if(left.equals(right)) { return 0; } final int llen = left.length(); final int rlen = right.length(); if (llen == 0 || rlen == 0) { return Integer.compare(llen, rlen); } Integer lo = latestMap.get(left); Integer ro = latestMap.get(right); if(lo != null || ro != null) { return compareOrders(lo, ro); } int lpp = 0, rpp = 0, llp = 0, rlp = 0; while(true) { llp = left.indexOf('.', llp); if(llp < 0) { llp = llen; } rlp = right.indexOf('.', rlp); if(rlp < 0) { rlp = rlen; } int lne = getNumEnd(left, lpp); int rne = getNumEnd(right, rpp); if (lne > 0 && rne > 0) { int res = compareTokens(left.substring(lpp, lne), right.substring(rpp, rne)); if(res != 0) { return res; } lpp = lne; rpp = rne; } boolean end = llp == llen && rlp == rlen; if(lpp < llp || rpp < rlp || end) { String ltoken = left.substring(lpp, llp); String rtoken = right.substring(rpp, rlp); int res = compareEnds(ltoken, rtoken); if (res != 0 || end) { return res; } } lpp = Math.min(++llp, llen); rpp = Math.min(++rlp, rlen); } } private  VersionComparator(Builder b); }
VersionComparator implements Comparator<String> { @Override public int compare(String left, String right) { if(left == null || right == null) { if(left == null) { return (right == null)? 0 : -1; } return 1; } if(left.equals(right)) { return 0; } final int llen = left.length(); final int rlen = right.length(); if (llen == 0 || rlen == 0) { return Integer.compare(llen, rlen); } Integer lo = latestMap.get(left); Integer ro = latestMap.get(right); if(lo != null || ro != null) { return compareOrders(lo, ro); } int lpp = 0, rpp = 0, llp = 0, rlp = 0; while(true) { llp = left.indexOf('.', llp); if(llp < 0) { llp = llen; } rlp = right.indexOf('.', rlp); if(rlp < 0) { rlp = rlen; } int lne = getNumEnd(left, lpp); int rne = getNumEnd(right, rpp); if (lne > 0 && rne > 0) { int res = compareTokens(left.substring(lpp, lne), right.substring(rpp, rne)); if(res != 0) { return res; } lpp = lne; rpp = rne; } boolean end = llp == llen && rlp == rlen; if(lpp < llp || rpp < rlp || end) { String ltoken = left.substring(lpp, llp); String rtoken = right.substring(rpp, rlp); int res = compareEnds(ltoken, rtoken); if (res != 0 || end) { return res; } } lpp = Math.min(++llp, llen); rpp = Math.min(++rlp, rlen); } } private  VersionComparator(Builder b); @Override int compare(String left, String right); static Builder builder(); }
VersionComparator implements Comparator<String> { @Override public int compare(String left, String right) { if(left == null || right == null) { if(left == null) { return (right == null)? 0 : -1; } return 1; } if(left.equals(right)) { return 0; } final int llen = left.length(); final int rlen = right.length(); if (llen == 0 || rlen == 0) { return Integer.compare(llen, rlen); } Integer lo = latestMap.get(left); Integer ro = latestMap.get(right); if(lo != null || ro != null) { return compareOrders(lo, ro); } int lpp = 0, rpp = 0, llp = 0, rlp = 0; while(true) { llp = left.indexOf('.', llp); if(llp < 0) { llp = llen; } rlp = right.indexOf('.', rlp); if(rlp < 0) { rlp = rlen; } int lne = getNumEnd(left, lpp); int rne = getNumEnd(right, rpp); if (lne > 0 && rne > 0) { int res = compareTokens(left.substring(lpp, lne), right.substring(rpp, rne)); if(res != 0) { return res; } lpp = lne; rpp = rne; } boolean end = llp == llen && rlp == rlen; if(lpp < llp || rpp < rlp || end) { String ltoken = left.substring(lpp, llp); String rtoken = right.substring(rpp, rlp); int res = compareEnds(ltoken, rtoken); if (res != 0 || end) { return res; } } lpp = Math.min(++llp, llen); rpp = Math.min(++rlp, rlen); } } private  VersionComparator(Builder b); @Override int compare(String left, String right); static Builder builder(); static final VersionComparator INSTANCE; }