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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.io.comparator;
20
import java.io.Serializable;
21
import java.util.Comparator;
23
import org.apache.commons.io.IOCase;
26
* Compare the <b>path</b> of two files for order (see {@link File#getPath()}).
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 dependant 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.
34
* Example of a <i>case-sensitive</i> file path sort using the
35
* {@link #PATH_COMPARATOR} singleton instance:
37
* List<File> list = ...
38
* Collections.sort(list, PathFileComparator.PATH_COMPARATOR);
41
* Example of a <i>reverse case-insensitive</i> file path sort using the
42
* {@link #PATH_INSENSITIVE_REVERSE} singleton instance:
45
* Arrays.sort(array, PathFileComparator.PATH_INSENSITIVE_REVERSE);
49
* @version $Revision: 609243 $ $Date: 2008-01-06 00:30:42 +0000 (Sun, 06 Jan 2008) $
50
* @since Commons IO 1.4
52
public class PathFileComparator implements Comparator, Serializable {
54
/** Case-sensitive path comparator instance (see {@link IOCase#SENSITIVE}) */
55
public static final Comparator PATH_COMPARATOR = new PathFileComparator();
57
/** Reverse case-sensitive path comparator instance (see {@link IOCase#SENSITIVE}) */
58
public static final Comparator PATH_REVERSE = new ReverseComparator(PATH_COMPARATOR);
60
/** Case-insensitive path comparator instance (see {@link IOCase#INSENSITIVE}) */
61
public static final Comparator PATH_INSENSITIVE_COMPARATOR = new PathFileComparator(IOCase.INSENSITIVE);
63
/** Reverse case-insensitive path comparator instance (see {@link IOCase#INSENSITIVE}) */
64
public static final Comparator PATH_INSENSITIVE_REVERSE = new ReverseComparator(PATH_INSENSITIVE_COMPARATOR);
66
/** System sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
67
public static final Comparator PATH_SYSTEM_COMPARATOR = new PathFileComparator(IOCase.SYSTEM);
69
/** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
70
public static final Comparator PATH_SYSTEM_REVERSE = new ReverseComparator(PATH_SYSTEM_COMPARATOR);
72
/** Whether the comparison is case sensitive. */
73
private final IOCase caseSensitivity;
76
* Construct a case sensitive file path comparator instance.
78
public PathFileComparator() {
79
this.caseSensitivity = IOCase.SENSITIVE;
83
* Construct a file path comparator instance with the specified case-sensitivity.
85
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
87
public PathFileComparator(IOCase caseSensitivity) {
88
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
92
* Compare the paths of two files the specified case sensitivity.
94
* @param obj1 The first file to compare
95
* @param obj2 The second file to compare
96
* @return a negative value if the first file's path
97
* is less than the second, zero if the paths are the
98
* same and a positive value if the first files path
99
* is greater than the second file.
102
public int compare(Object obj1, Object obj2) {
103
File file1 = (File)obj1;
104
File file2 = (File)obj2;
105
return caseSensitivity.checkCompareTo(file1.getPath(), file2.getPath());