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  import org.apache.commons.io.IOCase;
24  
25  /**
26   * Compare the <b>path</b> of two files for order (see {@link File#getPath()}).
27   * <p>
28   * This comparator can be used to sort lists or arrays of files
29   * by their path either in a case-sensitive, case-insensitive or
30   * system dependent case-sensitive way. A number of singleton instances
31   * are provided for the various case sensitivity options (using {@link IOCase})
32   * and the reverse of those options.
33   * </p>
34   * <p>
35   * Example of a <i>case-sensitive</i> file path sort using the
36   * {@link #PATH_COMPARATOR} singleton instance:
37   * </p>
38   * <pre>
39   *       List&lt;File&gt; list = ...
40   *       ((AbstractFileComparator) PathFileComparator.PATH_COMPARATOR).sort(list);
41   * </pre>
42   * <p>
43   * Example of a <i>reverse case-insensitive</i> file path sort using the
44   * {@link #PATH_INSENSITIVE_REVERSE} singleton instance:
45   * </p>
46   * <pre>
47   *       File[] array = ...
48   *       ((AbstractFileComparator) PathFileComparator.PATH_INSENSITIVE_REVERSE).sort(array);
49   * </pre>
50   * <h2>Deprecating Serialization</h2>
51   * <p>
52   * <em>Serialization is deprecated and will be removed in 3.0.</em>
53   * </p>
54   * @since 1.4
55   */
56  public class PathFileComparator extends AbstractFileComparator implements Serializable {
57  
58      private static final long serialVersionUID = 6527501707585768673L;
59  
60      /** Case-sensitive path comparator instance (see {@link IOCase#SENSITIVE}) */
61      public static final Comparator<File> PATH_COMPARATOR = new PathFileComparator();
62  
63      /** Reverse case-sensitive path comparator instance (see {@link IOCase#SENSITIVE}) */
64      public static final Comparator<File> PATH_REVERSE = new ReverseFileComparator(PATH_COMPARATOR);
65  
66      /** Case-insensitive path comparator instance (see {@link IOCase#INSENSITIVE}) */
67      public static final Comparator<File> PATH_INSENSITIVE_COMPARATOR = new PathFileComparator(IOCase.INSENSITIVE);
68  
69      /** Reverse case-insensitive path comparator instance (see {@link IOCase#INSENSITIVE}) */
70      public static final Comparator<File> PATH_INSENSITIVE_REVERSE = new ReverseFileComparator(PATH_INSENSITIVE_COMPARATOR);
71  
72      /** System sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
73      public static final Comparator<File> PATH_SYSTEM_COMPARATOR = new PathFileComparator(IOCase.SYSTEM);
74  
75      /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
76      public static final Comparator<File> PATH_SYSTEM_REVERSE = new ReverseFileComparator(PATH_SYSTEM_COMPARATOR);
77  
78      /** Whether the comparison is case-sensitive. */
79      private final IOCase ioCase;
80  
81      /**
82       * Constructs a case-sensitive file path comparator instance.
83       */
84      public PathFileComparator() {
85          this.ioCase = IOCase.SENSITIVE;
86      }
87  
88      /**
89       * Constructs a file path comparator instance with the specified case-sensitivity.
90       *
91       * @param ioCase  how to handle case sensitivity, null means case-sensitive
92       */
93      public PathFileComparator(final IOCase ioCase) {
94          this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE);
95      }
96  
97      /**
98       * Compares the paths of two files the specified case sensitivity.
99       *
100      * @param file1 The first file to compare
101      * @param file2 The second file to compare
102      * @return a negative value if the first file's path
103      * is less than the second, zero if the paths are the
104      * same and a positive value if the first files path
105      * is greater than the second file.
106      */
107     @Override
108     public int compare(final File file1, final File file2) {
109         return ioCase.checkCompareTo(file1.getPath(), file2.getPath());
110     }
111 
112     /**
113      * String representation of this file comparator.
114      *
115      * @return String representation of this file comparator
116      */
117     @Override
118     public String toString() {
119         return super.toString() + "[ioCase=" + ioCase + "]";
120     }
121 }