@Test public void compareFilesSnapshots2() { assert comparator != null; Path firstFile = Paths.get("bin", "org", "illarion", "download", "2.1.1.1-SNAPSHOT", "download-2.1.1.1-SNAPSHOT.jar"); Path secondFile = Paths.get("bin", "org", "illarion", "download", "2.1.1.1-SNAPSHOT", "download-2.1.1.1-20150607.174327-14.jar"); mockStaticPartial(Files.class, "isDirectory", Path.class, LinkOption[].class); expect(Files.isDirectory(firstFile)).andReturn(false).anyTimes(); expect(Files.isDirectory(secondFile)).andReturn(false).anyTimes(); replayAll(); int result = comparator.compare(firstFile, secondFile); Assert.assertTrue(result > 0); int result2 = comparator.compare(secondFile, firstFile); Assert.assertTrue(result2 < 0); verifyAll(); }
@Override public int compare(@Nonnull Path o1, @Nonnull Path o2) { String version1; String version2; if (Files.isDirectory(o1)) { if (Files.isDirectory(o2)) { version1 = o1.toString(); version2 = o2.toString(); } else { return 1; } } else { if (Files.isDirectory(o2)) { return -1; } else { version1 = o1.toString(); version2 = o2.toString(); version1 = version1.substring(version1.indexOf('-')); version2 = version2.substring(version2.indexOf('-')); } } boolean snapshot1; boolean snapshot2; if (version1.endsWith("-SNAPSHOT")) { snapshot1 = true; version1 = version1.substring(0, version1.length() - "-SNAPSHOT".length()); } else { snapshot1 = false; } if (version2.endsWith("-SNAPSHOT")) { snapshot2 = true; version2 = version2.substring(0, version2.length() - "-SNAPSHOT".length()); } else { snapshot2 = false; } String[] version1Parts = VERSION_SPLIT_PATTERN.split(version1); String[] version2Parts = VERSION_SPLIT_PATTERN.split(version2); int count = Math.min(version1Parts.length, version2Parts.length); for (int i = 0; i < count; i++) { try { int value1 = Integer.parseInt(version1Parts[i]); int value2 = Integer.parseInt(version2Parts[i]); int compareResult = Integer.compare(value1, value2); if (compareResult != 0) { return compareResult; } } catch (NumberFormatException ignored) { } int compareResult = version1Parts[i].compareTo(version2Parts[i]); if (compareResult != 0) { return compareResult; } } if (snapshot1 && !snapshot2) { return 1; } if (!snapshot1 && snapshot2) { return -1; } return 0; }
VersionComparator implements Comparator<Path>, Serializable { @Override public int compare(@Nonnull Path o1, @Nonnull Path o2) { String version1; String version2; if (Files.isDirectory(o1)) { if (Files.isDirectory(o2)) { version1 = o1.toString(); version2 = o2.toString(); } else { return 1; } } else { if (Files.isDirectory(o2)) { return -1; } else { version1 = o1.toString(); version2 = o2.toString(); version1 = version1.substring(version1.indexOf('-')); version2 = version2.substring(version2.indexOf('-')); } } boolean snapshot1; boolean snapshot2; if (version1.endsWith("-SNAPSHOT")) { snapshot1 = true; version1 = version1.substring(0, version1.length() - "-SNAPSHOT".length()); } else { snapshot1 = false; } if (version2.endsWith("-SNAPSHOT")) { snapshot2 = true; version2 = version2.substring(0, version2.length() - "-SNAPSHOT".length()); } else { snapshot2 = false; } String[] version1Parts = VERSION_SPLIT_PATTERN.split(version1); String[] version2Parts = VERSION_SPLIT_PATTERN.split(version2); int count = Math.min(version1Parts.length, version2Parts.length); for (int i = 0; i < count; i++) { try { int value1 = Integer.parseInt(version1Parts[i]); int value2 = Integer.parseInt(version2Parts[i]); int compareResult = Integer.compare(value1, value2); if (compareResult != 0) { return compareResult; } } catch (NumberFormatException ignored) { } int compareResult = version1Parts[i].compareTo(version2Parts[i]); if (compareResult != 0) { return compareResult; } } if (snapshot1 && !snapshot2) { return 1; } if (!snapshot1 && snapshot2) { return -1; } return 0; } }
VersionComparator implements Comparator<Path>, Serializable { @Override public int compare(@Nonnull Path o1, @Nonnull Path o2) { String version1; String version2; if (Files.isDirectory(o1)) { if (Files.isDirectory(o2)) { version1 = o1.toString(); version2 = o2.toString(); } else { return 1; } } else { if (Files.isDirectory(o2)) { return -1; } else { version1 = o1.toString(); version2 = o2.toString(); version1 = version1.substring(version1.indexOf('-')); version2 = version2.substring(version2.indexOf('-')); } } boolean snapshot1; boolean snapshot2; if (version1.endsWith("-SNAPSHOT")) { snapshot1 = true; version1 = version1.substring(0, version1.length() - "-SNAPSHOT".length()); } else { snapshot1 = false; } if (version2.endsWith("-SNAPSHOT")) { snapshot2 = true; version2 = version2.substring(0, version2.length() - "-SNAPSHOT".length()); } else { snapshot2 = false; } String[] version1Parts = VERSION_SPLIT_PATTERN.split(version1); String[] version2Parts = VERSION_SPLIT_PATTERN.split(version2); int count = Math.min(version1Parts.length, version2Parts.length); for (int i = 0; i < count; i++) { try { int value1 = Integer.parseInt(version1Parts[i]); int value2 = Integer.parseInt(version2Parts[i]); int compareResult = Integer.compare(value1, value2); if (compareResult != 0) { return compareResult; } } catch (NumberFormatException ignored) { } int compareResult = version1Parts[i].compareTo(version2Parts[i]); if (compareResult != 0) { return compareResult; } } if (snapshot1 && !snapshot2) { return 1; } if (!snapshot1 && snapshot2) { return -1; } return 0; }  }
VersionComparator implements Comparator<Path>, Serializable { @Override public int compare(@Nonnull Path o1, @Nonnull Path o2) { String version1; String version2; if (Files.isDirectory(o1)) { if (Files.isDirectory(o2)) { version1 = o1.toString(); version2 = o2.toString(); } else { return 1; } } else { if (Files.isDirectory(o2)) { return -1; } else { version1 = o1.toString(); version2 = o2.toString(); version1 = version1.substring(version1.indexOf('-')); version2 = version2.substring(version2.indexOf('-')); } } boolean snapshot1; boolean snapshot2; if (version1.endsWith("-SNAPSHOT")) { snapshot1 = true; version1 = version1.substring(0, version1.length() - "-SNAPSHOT".length()); } else { snapshot1 = false; } if (version2.endsWith("-SNAPSHOT")) { snapshot2 = true; version2 = version2.substring(0, version2.length() - "-SNAPSHOT".length()); } else { snapshot2 = false; } String[] version1Parts = VERSION_SPLIT_PATTERN.split(version1); String[] version2Parts = VERSION_SPLIT_PATTERN.split(version2); int count = Math.min(version1Parts.length, version2Parts.length); for (int i = 0; i < count; i++) { try { int value1 = Integer.parseInt(version1Parts[i]); int value2 = Integer.parseInt(version2Parts[i]); int compareResult = Integer.compare(value1, value2); if (compareResult != 0) { return compareResult; } } catch (NumberFormatException ignored) { } int compareResult = version1Parts[i].compareTo(version2Parts[i]); if (compareResult != 0) { return compareResult; } } if (snapshot1 && !snapshot2) { return 1; } if (!snapshot1 && snapshot2) { return -1; } return 0; }  @Override int compare(@Nonnull Path o1, @Nonnull Path o2); }
VersionComparator implements Comparator<Path>, Serializable { @Override public int compare(@Nonnull Path o1, @Nonnull Path o2) { String version1; String version2; if (Files.isDirectory(o1)) { if (Files.isDirectory(o2)) { version1 = o1.toString(); version2 = o2.toString(); } else { return 1; } } else { if (Files.isDirectory(o2)) { return -1; } else { version1 = o1.toString(); version2 = o2.toString(); version1 = version1.substring(version1.indexOf('-')); version2 = version2.substring(version2.indexOf('-')); } } boolean snapshot1; boolean snapshot2; if (version1.endsWith("-SNAPSHOT")) { snapshot1 = true; version1 = version1.substring(0, version1.length() - "-SNAPSHOT".length()); } else { snapshot1 = false; } if (version2.endsWith("-SNAPSHOT")) { snapshot2 = true; version2 = version2.substring(0, version2.length() - "-SNAPSHOT".length()); } else { snapshot2 = false; } String[] version1Parts = VERSION_SPLIT_PATTERN.split(version1); String[] version2Parts = VERSION_SPLIT_PATTERN.split(version2); int count = Math.min(version1Parts.length, version2Parts.length); for (int i = 0; i < count; i++) { try { int value1 = Integer.parseInt(version1Parts[i]); int value2 = Integer.parseInt(version2Parts[i]); int compareResult = Integer.compare(value1, value2); if (compareResult != 0) { return compareResult; } } catch (NumberFormatException ignored) { } int compareResult = version1Parts[i].compareTo(version2Parts[i]); if (compareResult != 0) { return compareResult; } } if (snapshot1 && !snapshot2) { return 1; } if (!snapshot1 && snapshot2) { return -1; } return 0; }  @Override int compare(@Nonnull Path o1, @Nonnull Path o2);  }