View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io.comparator;
18  
19  import java.io.File;
20  import java.io.Serializable;
21  import java.util.Comparator;
22  
23  /**
24   * Compare two files using the <b>default</b> {@link File#compareTo(File)} method.
25   * <p>
26   * This comparator can be used to sort lists or arrays of files
27   * by using the default file comparison.
28   * <p>
29   * Example of sorting a list of files using the
30   * {@link #DEFAULT_COMPARATOR} singleton instance:
31   * <pre>
32   *       List&lt;File&gt; list = ...
33   *       Collections.sort(list, DefaultFileComparator.DEFAULT_COMPARATOR);
34   * </pre>
35   * <p>
36   * Example of doing a <i>reverse</i> sort of an array of files using the
37   * {@link #DEFAULT_REVERSE} singleton instance:
38   * <pre>
39   *       File[] array = ...
40   *       Arrays.sort(array, DefaultFileComparator.DEFAULT_REVERSE);
41   * </pre>
42   * <p>
43   *
44   * @version $Revision: 619103 $ $Date: 2008-02-06 19:01:17 +0000 (Wed, 06 Feb 2008) $
45   * @since Commons IO 1.4
46   */
47  public class DefaultFileComparator implements Comparator<File>, Serializable {
48  
49      /** Singleton default comparator instance */
50      public static final Comparator<File> DEFAULT_COMPARATOR = new DefaultFileComparator();
51  
52      /** Singleton reverse default comparator instance */
53      public static final Comparator<File> DEFAULT_REVERSE = new ReverseComparator(DEFAULT_COMPARATOR);
54  
55      /**
56       * Compare the two files using the {@link File#compareTo(File)} method.
57       * 
58       * @param file1 The first file to compare
59       * @param file2 The second file to compare
60       * @return the result of calling file1's
61       * {@link File#compareTo(File)} with file2 as the parameter.
62       */
63      public int compare(File file1, File file2) {
64          return file1.compareTo(file2);
65      }
66  }