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.FilenameUtils;
24 import org.apache.commons.io.IOCase;
25
26 /**
27 * Compare the file name <b>extensions</b> for order
28 * (see {@link FilenameUtils#getExtension(String)}).
29 * <p>
30 * This comparator can be used to sort lists or arrays of files
31 * by their file extension either in a case-sensitive, case-insensitive or
32 * system dependant case sensitive way. A number of singleton instances
33 * are provided for the various case sensitivity options (using {@link IOCase})
34 * and the reverse of those options.
35 * <p>
36 * Example of a <i>case-sensitive</i> file extension sort using the
37 * {@link #EXTENSION_COMPARATOR} singleton instance:
38 * <pre>
39 * List<File> list = ...
40 * Collections.sort(list, ExtensionFileComparator.EXTENSION_COMPARATOR);
41 * </pre>
42 * <p>
43 * Example of a <i>reverse case-insensitive</i> file extension sort using the
44 * {@link #EXTENSION_INSENSITIVE_REVERSE} singleton instance:
45 * <pre>
46 * File[] array = ...
47 * Arrays.sort(array, ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE);
48 * </pre>
49 * <p>
50 *
51 * @version $Revision: 619103 $ $Date: 2008-02-06 19:01:17 +0000 (Wed, 06 Feb 2008) $
52 * @since Commons IO 1.4
53 */
54 public class ExtensionFileComparator implements Comparator<File>, Serializable {
55
56 /** Case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
57 public static final Comparator<File> EXTENSION_COMPARATOR = new ExtensionFileComparator();
58
59 /** Reverse case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
60 public static final Comparator<File> EXTENSION_REVERSE = new ReverseComparator(EXTENSION_COMPARATOR);
61
62 /** Case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
63 public static final Comparator<File> EXTENSION_INSENSITIVE_COMPARATOR = new ExtensionFileComparator(IOCase.INSENSITIVE);
64
65 /** Reverse case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
66 public static final Comparator<File> EXTENSION_INSENSITIVE_REVERSE
67 = new ReverseComparator(EXTENSION_INSENSITIVE_COMPARATOR);
68
69 /** System sensitive extension comparator instance (see {@link IOCase#SYSTEM}) */
70 public static final Comparator<File> EXTENSION_SYSTEM_COMPARATOR = new ExtensionFileComparator(IOCase.SYSTEM);
71
72 /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
73 public static final Comparator<File> EXTENSION_SYSTEM_REVERSE = new ReverseComparator(EXTENSION_SYSTEM_COMPARATOR);
74
75 /** Whether the comparison is case sensitive. */
76 private final IOCase caseSensitivity;
77
78 /**
79 * Construct a case sensitive file extension comparator instance.
80 */
81 public ExtensionFileComparator() {
82 this.caseSensitivity = IOCase.SENSITIVE;
83 }
84
85 /**
86 * Construct a file extension comparator instance with the specified case-sensitivity.
87 *
88 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
89 */
90 public ExtensionFileComparator(IOCase caseSensitivity) {
91 this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
92 }
93
94 /**
95 * Compare the extensions of two files the specified case sensitivity.
96 *
97 * @param file1 The first file to compare
98 * @param file2 The second file to compare
99 * @return a negative value if the first file's extension
100 * is less than the second, zero if the extensions are the
101 * same and a positive value if the first files extension
102 * is greater than the second file.
103 *
104 */
105 public int compare(File file1, File file2) {
106 String suffix1 = FilenameUtils.getExtension(file1.getName());
107 String suffix2 = FilenameUtils.getExtension(file2.getName());
108 return caseSensitivity.checkCompareTo(suffix1, suffix2);
109 }
110 }