Skip to content

Commit 55dfa6e

Browse files
author
Niall Pemberton
committed
IO-145 - add new package of File Comparator implementations
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@609243 13f79535-47bb-0310-9956-ffa450edef68
1 parent bd61711 commit 55dfa6e

16 files changed

Lines changed: 1223 additions & 0 deletions

RELEASE-NOTES.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ Enhancements from 1.3.2
7979
- IOCase
8080
- Add a compare method to IOCase [IO-144]
8181

82+
- Add a package of java.util.Comparator implementations for files [IO-145]
83+
- DefaultFileComparator - compare files using the default File.compareTo(File) method.
84+
- ExtensionFileComparator - compares files using file name extensions.
85+
- LastModifiedFileComparator - compares files using the last modified date/time.
86+
- NameFileComparator - compares files using file names.
87+
- PathFileComparator - compares files using file paths.
88+
- SizeFileComparator - compares files using file sizes.
89+
8290

8391
Feedback
8492
--------
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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$ $Date$
45+
* @since Commons IO 1.4
46+
*/
47+
public class DefaultFileComparator implements Comparator, Serializable {
48+
49+
/** Singleton default comparator instance */
50+
public static final Comparator DEFAULT_COMPARATOR = new DefaultFileComparator();
51+
52+
/** Singleton reverse default comparator instance */
53+
public static final Comparator DEFAULT_REVERSE = new ReverseComparator(DEFAULT_COMPARATOR);
54+
55+
/**
56+
* Compare the two files using the {@link File#compareTo(File)} method.
57+
*
58+
* @param obj1 The first file to compare
59+
* @param obj2 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(Object obj1, Object obj2) {
64+
File file1 = (File)obj1;
65+
File file2 = (File)obj2;
66+
return file1.compareTo(file2);
67+
}
68+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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&lt;File&gt; 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$ $Date$
52+
* @since Commons IO 1.4
53+
*/
54+
public class ExtensionFileComparator implements Comparator, Serializable {
55+
56+
/** Case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
57+
public static final Comparator EXTENSION_COMPARATOR = new ExtensionFileComparator();
58+
59+
/** Reverse case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
60+
public static final Comparator EXTENSION_REVERSE = new ReverseComparator(EXTENSION_COMPARATOR);
61+
62+
/** Case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
63+
public static final Comparator EXTENSION_INSENSITIVE_COMPARATOR = new ExtensionFileComparator(IOCase.INSENSITIVE);
64+
65+
/** Reverse case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
66+
public static final Comparator 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 EXTENSION_SYSTEM_COMPARATOR = new ExtensionFileComparator(IOCase.SYSTEM);
71+
72+
/** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
73+
public static final Comparator 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 obj1 The first file to compare
98+
* @param obj2 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(Object obj1, Object obj2) {
106+
File file1 = (File)obj1;
107+
File file2 = (File)obj2;
108+
String suffix1 = FilenameUtils.getExtension(file1.getName());
109+
String suffix2 = FilenameUtils.getExtension(file2.getName());
110+
return caseSensitivity.checkCompareTo(suffix1, suffix2);
111+
}
112+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 the <b>last modified date/time</b> of two files for order
25+
* (see {@link File#lastModified()}).
26+
* <p>
27+
* This comparator can be used to sort lists or arrays of files
28+
* by their last modified date/time.
29+
* <p>
30+
* Example of sorting a list of files using the
31+
* {@link #LASTMODIFIED_COMPARATOR} singleton instance:
32+
* <pre>
33+
* List&lt;File&gt; list = ...
34+
* Collections.sort(list, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
35+
* </pre>
36+
* <p>
37+
* Example of doing a <i>reverse</i> sort of an array of files using the
38+
* {@link #LASTMODIFIED_REVERSE} singleton instance:
39+
* <pre>
40+
* File[] array = ...
41+
* Arrays.sort(array, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
42+
* </pre>
43+
* <p>
44+
*
45+
* @version $Revision$ $Date$
46+
* @since Commons IO 1.4
47+
*/
48+
public class LastModifiedFileComparator implements Comparator, Serializable {
49+
50+
/** Last modified comparator instance */
51+
public static final Comparator LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator();
52+
53+
/** Reverse last modified comparator instance */
54+
public static final Comparator LASTMODIFIED_REVERSE = new ReverseComparator(LASTMODIFIED_COMPARATOR);
55+
56+
/**
57+
* Compare the last the last modified date/time of two files.
58+
*
59+
* @param obj1 The first file to compare
60+
* @param obj2 The second file to compare
61+
* @return a negative value if the first file's lastmodified date/time
62+
* is less than the second, zero if the lastmodified date/time are the
63+
* same and a positive value if the first files lastmodified date/time
64+
* is greater than the second file.
65+
*
66+
*/
67+
public int compare(Object obj1, Object obj2) {
68+
File file1 = (File)obj1;
69+
File file2 = (File)obj2;
70+
long result = file1.lastModified() - file2.lastModified();
71+
if (result < 0) {
72+
return -1;
73+
} else if (result > 0) {
74+
return 1;
75+
} else {
76+
return 0;
77+
}
78+
}
79+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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>names</b> of two files for order (see {@link File#getName()}).
27+
* <p>
28+
* This comparator can be used to sort lists or arrays of files
29+
* by their name 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.
33+
* <p>
34+
* Example of a <i>case-sensitive</i> file name sort using the
35+
* {@link #NAME_COMPARATOR} singleton instance:
36+
* <pre>
37+
* List&lt;File&gt; list = ...
38+
* Collections.sort(list, NameFileComparator.NAME_COMPARATOR);
39+
* </pre>
40+
* <p>
41+
* Example of a <i>reverse case-insensitive</i> file name sort using the
42+
* {@link #NAME_INSENSITIVE_REVERSE} singleton instance:
43+
* <pre>
44+
* File[] array = ...
45+
* Arrays.sort(array, NameFileComparator.NAME_INSENSITIVE_REVERSE);
46+
* </pre>
47+
* <p>
48+
*
49+
* @version $Revision$ $Date$
50+
* @since Commons IO 1.4
51+
*/
52+
public class NameFileComparator implements Comparator, Serializable {
53+
54+
/** Case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */
55+
public static final Comparator NAME_COMPARATOR = new NameFileComparator();
56+
57+
/** Reverse case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */
58+
public static final Comparator NAME_REVERSE = new ReverseComparator(NAME_COMPARATOR);
59+
60+
/** Case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */
61+
public static final Comparator NAME_INSENSITIVE_COMPARATOR = new NameFileComparator(IOCase.INSENSITIVE);
62+
63+
/** Reverse case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */
64+
public static final Comparator NAME_INSENSITIVE_REVERSE = new ReverseComparator(NAME_INSENSITIVE_COMPARATOR);
65+
66+
/** System sensitive name comparator instance (see {@link IOCase#SYSTEM}) */
67+
public static final Comparator NAME_SYSTEM_COMPARATOR = new NameFileComparator(IOCase.SYSTEM);
68+
69+
/** Reverse system sensitive name comparator instance (see {@link IOCase#SYSTEM}) */
70+
public static final Comparator NAME_SYSTEM_REVERSE = new ReverseComparator(NAME_SYSTEM_COMPARATOR);
71+
72+
/** Whether the comparison is case sensitive. */
73+
private final IOCase caseSensitivity;
74+
75+
/**
76+
* Construct a case sensitive file name comparator instance.
77+
*/
78+
public NameFileComparator() {
79+
this.caseSensitivity = IOCase.SENSITIVE;
80+
}
81+
82+
/**
83+
* Construct a file name comparator instance with the specified case-sensitivity.
84+
*
85+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
86+
*/
87+
public NameFileComparator(IOCase caseSensitivity) {
88+
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
89+
}
90+
91+
/**
92+
* Compare the names of two files with the specified case sensitivity.
93+
*
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 name
97+
* is less than the second, zero if the names are the
98+
* same and a positive value if the first files name
99+
* is greater than the second file.
100+
*/
101+
public int compare(Object obj1, Object obj2) {
102+
File file1 = (File)obj1;
103+
File file2 = (File)obj2;
104+
return caseSensitivity.checkCompareTo(file1.getName(), file2.getName());
105+
}
106+
}

0 commit comments

Comments
 (0)