DirEntryComparator implements Comparator<DirEntry>, Serializable { @Override public int compare(final DirEntry entry1, final DirEntry entry2) { if (groupDirs) { final DirEntry.Kind kind1 = entry1.getKind(); final DirEntry.Kind kind2 = entry2.getKind(); if (!kind1.equals(kind2)) { if (DirEntry.Kind.DIR == kind1) { return -1; } else if (DirEntry.Kind.DIR == kind2) { return 1; } } } final String entryName1 = entry1.getName(); final String entryName2 = entry2.getName(); switch (sortType) { case NAME: final int nameCompare = nullSafeCompare(entryName1, entryName2); if (nameCompare == 0) { final long revision1 = entry1.getRevision(); final long revision2 = entry2.getRevision(); return revision1 == revision2 ? 0 : revision1 < revision2 ? -1 : 1; } else { return nameCompare; } case AUTHOR: final String author1 = entry1.getAuthor(); final String author2 = entry2.getAuthor(); final int authCompare = nullSafeCompare(author1, author2); return authCompare == 0 ? nullSafeCompare(entryName1, entryName2) : authCompare; case REVISION: final long revision1 = entry1.getRevision(); final long revision2 = entry2.getRevision(); if (revision1 == revision2) { return nullSafeCompare(entryName1, entryName2); } else { return revision1 < revision2 ? -1 : 1; } case SIZE: final long size1 = entry1.getSize(); final long size2 = entry2.getSize(); if (size1 == size2) { return nullSafeCompare(entryName1, entryName2); } else { return size1 < size2 ? -1 : 1; } case DATE: final Date date1 = entry1.getDate(); final Date date2 = entry2.getDate(); final int dateCompare = nullSafeCompare(date1, date2); return dateCompare == 0 ? nullSafeCompare(entryName1, entryName2) : dateCompare; case FULL_NAME: return nullSafeCompare(entry1.getFullEntryName(), entry2.getFullEntryName()); default: throw new IllegalStateException("Illegal sort type: " + sortType); } } DirEntryComparator(final SortType sortType, final boolean groupDirs); @Override int compare(final DirEntry entry1, final DirEntry entry2);  }
@Test public void testCompare() throws Exception { List<DirEntry> entries = new ArrayList<DirEntry>(); DirEntry e1 = new DirEntry("", "FirstClass.java", "patrik", new GregorianCalendar(2005, 4, 12).getTime(), DirEntry.Kind.FILE, 2, 134); DirEntry e2 = new DirEntry("", "SecondClass.java", "jesper", new GregorianCalendar(2005, 4, 13).getTime(), DirEntry.Kind.FILE, 3, 135); DirEntry e3 = new DirEntry("", "ThirdClass.java", "patrik", new GregorianCalendar(2005, 4, 14).getTime(), DirEntry.Kind.DIR, 4, 136); entries.add(e3); entries.add(e2); entries.add(e1); assertSame(e3, entries.get(0)); Collections.sort(entries, new DirEntryComparator(NAME, false)); assertSame(e1, entries.get(0)); assertSame(e2, entries.get(1)); assertSame(e3, entries.get(2)); Collections.sort(entries, new DirEntryComparator(NAME, true)); assertSame(e1, entries.get(1)); assertSame(e2, entries.get(2)); assertSame(e3, entries.get(0)); Collections.sort(entries, new DirEntryComparator(AUTHOR, false)); assertSame(e1, entries.get(1)); assertSame(e2, entries.get(0)); assertSame(e3, entries.get(2)); Collections.sort(entries, new DirEntryComparator(AUTHOR, true)); assertSame(e1, entries.get(2)); assertSame(e2, entries.get(1)); assertSame(e3, entries.get(0)); Collections.sort(entries, new DirEntryComparator(REVISION, false)); assertSame(e1, entries.get(0)); assertSame(e2, entries.get(1)); assertSame(e3, entries.get(2)); Collections.sort(entries, new DirEntryComparator(DATE, false)); assertSame(e1, entries.get(0)); assertSame(e2, entries.get(1)); assertSame(e3, entries.get(2)); Collections.sort(entries, new DirEntryComparator(FULL_NAME, false)); assertSame(e1, entries.get(0)); assertSame(e2, entries.get(1)); assertSame(e3, entries.get(2)); Collections.sort(entries, new DirEntryComparator(SIZE, false)); assertSame(e1, entries.get(0)); assertSame(e2, entries.get(1)); assertSame(e3, entries.get(2)); try { new DirEntryComparator(DirEntryComparator.SortType.valueOf("test"), false); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException iae) { } DirEntryComparator comparator = new DirEntryComparator(DATE, false); try { comparator.compare(null, null); fail("NullPointerException expected"); } catch (NullPointerException npe) { } entries = new ArrayList<DirEntry>(); e1 = new DirEntry("", "FirstClass.java", "patrik", new GregorianCalendar(2005, 4, 12).getTime(), DirEntry.Kind.FILE, 2, 134); e2 = new DirEntry("", "", "jesper", new GregorianCalendar(2005, 4, 13).getTime(), DirEntry.Kind.FILE, 3, 135); e3 = new DirEntry("", "ThirdClass.java", "patrik", new GregorianCalendar(2005, 4, 14).getTime(), DirEntry.Kind.DIR, 4, 136); entries.add(e3); entries.add(e2); entries.add(e1); Collections.sort(entries, new DirEntryComparator(NAME, false)); assertSame(e1, entries.get(1)); assertSame(e2, entries.get(0)); assertSame(e3, entries.get(2)); }