fedora.utilities
Class FileComparator

java.lang.Object
  extended by fedora.utilities.FileComparator
All Implemented Interfaces:
java.util.Comparator

public class FileComparator
extends java.lang.Object
implements java.util.Comparator

Comparator for sorting files and directories in a deterministic manner. The sort order can be case-sensitive (default) or case-insensitive, ascending (default) or descending, with directories occurring before (if ascending) or after files within the same directory. The default behavior has the convenient property that it sorts in the same order as would be done when performing a pre-order traversal of the filesystem.

 Example input:
   afile
   file1
   file2
   dir1/
   FILE3
   dir1/file4
   dir2/file5
   dir2/file10
   dir2/
   file6

 Example output (with default behavior: case-sensitive, ascending):
   dir1
   dir1\file4
   dir2
   dir2\file10
   dir2\file5
   FILE3
   afile
   file1
   file2
   file6
 

Author:
cwilper@cs.cornell.edu

Constructor Summary
FileComparator()
          Construct a case-sensitive comparator that sorts in ascending order.
FileComparator(boolean useDescendingOrder)
          Construct a case-sensitive comparator that sorts in the specified order.
FileComparator(boolean useDescendingOrder, boolean ignoreCase)
          Construct a comparator that sorts in the specified order using the specified case sensitivity.
 
Method Summary
 int compare(java.lang.Object o1, java.lang.Object o2)
           
 boolean descends()
          Does this comparator work for descending sorts?
 boolean equals(java.lang.Object o)
           
 boolean ignoresCase()
          Does this comparator ignore case?
static void main(java.lang.String[] args)
          Command-line entry point for simple testing of this class.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileComparator

public FileComparator()
Construct a case-sensitive comparator that sorts in ascending order.


FileComparator

public FileComparator(boolean useDescendingOrder)
Construct a case-sensitive comparator that sorts in the specified order.


FileComparator

public FileComparator(boolean useDescendingOrder,
                      boolean ignoreCase)
Construct a comparator that sorts in the specified order using the specified case sensitivity.

Method Detail

compare

public int compare(java.lang.Object o1,
                   java.lang.Object o2)
Specified by:
compare in interface java.util.Comparator

descends

public boolean descends()
Does this comparator work for descending sorts?


ignoresCase

public boolean ignoresCase()
Does this comparator ignore case?


equals

public boolean equals(java.lang.Object o)
Specified by:
equals in interface java.util.Comparator
Overrides:
equals in class java.lang.Object

main

public static void main(java.lang.String[] args)
Command-line entry point for simple testing of this class. Given a directory, get all files and dirs (recursively) in no particular order. Print all filenames (using spaces to denote directory names) in unsorted, default sorted, and FileComparator sorted order.