You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2008/01/06 01:30:44 UTC
svn commit: r609243 - in /commons/proper/io/trunk: ./
src/java/org/apache/commons/io/comparator/
src/test/org/apache/commons/io/comparator/
Author: niallp
Date: Sat Jan 5 16:30:42 2008
New Revision: 609243
URL: http://svn.apache.org/viewvc?rev=609243&view=rev
Log:
IO-145 - add new package of File Comparator implementations
Added:
commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/
commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DefaultFileComparator.java (with props)
commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ExtensionFileComparator.java (with props)
commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/LastModifiedFileComparator.java (with props)
commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/NameFileComparator.java (with props)
commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/PathFileComparator.java (with props)
commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ReverseComparator.java (with props)
commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/SizeFileComparator.java (with props)
commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/package.html (with props)
commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/
commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ComparatorAbstractTestCase.java (with props)
commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DefaultFileComparatorTest.java (with props)
commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ExtensionFileComparatorTest.java (with props)
commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java (with props)
commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/NameFileComparatorTest.java (with props)
commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/PathFileComparatorTest.java (with props)
commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/SizeFileComparatorTest.java (with props)
Modified:
commons/proper/io/trunk/RELEASE-NOTES.txt
Modified: commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=609243&r1=609242&r2=609243&view=diff
==============================================================================
--- commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/io/trunk/RELEASE-NOTES.txt Sat Jan 5 16:30:42 2008
@@ -79,6 +79,14 @@
- IOCase
- Add a compare method to IOCase [IO-144]
+- Add a package of java.util.Comparator implementations for files [IO-145]
+ - DefaultFileComparator - compare files using the default File.compareTo(File) method.
+ - ExtensionFileComparator - compares files using file name extensions.
+ - LastModifiedFileComparator - compares files using the last modified date/time.
+ - NameFileComparator - compares files using file names.
+ - PathFileComparator - compares files using file paths.
+ - SizeFileComparator - compares files using file sizes.
+
Feedback
--------
Added: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DefaultFileComparator.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DefaultFileComparator.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DefaultFileComparator.java (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DefaultFileComparator.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import java.io.Serializable;
+import java.util.Comparator;
+
+/**
+ * Compare two files using the <b>default</b> {@link File#compareTo(File)} method.
+ * <p>
+ * This comparator can be used to sort lists or arrays of files
+ * by using the default file comparison.
+ * <p>
+ * Example of sorting a list of files using the
+ * {@link #DEFAULT_COMPARATOR} singleton instance:
+ * <pre>
+ * List<File> list = ...
+ * Collections.sort(list, DefaultFileComparator.DEFAULT_COMPARATOR);
+ * </pre>
+ * <p>
+ * Example of doing a <i>reverse</i> sort of an array of files using the
+ * {@link #DEFAULT_REVERSE} singleton instance:
+ * <pre>
+ * File[] array = ...
+ * Arrays.sort(array, DefaultFileComparator.DEFAULT_REVERSE);
+ * </pre>
+ * <p>
+ *
+ * @version $Revision$ $Date$
+ * @since Commons IO 1.4
+ */
+public class DefaultFileComparator implements Comparator, Serializable {
+
+ /** Singleton default comparator instance */
+ public static final Comparator DEFAULT_COMPARATOR = new DefaultFileComparator();
+
+ /** Singleton reverse default comparator instance */
+ public static final Comparator DEFAULT_REVERSE = new ReverseComparator(DEFAULT_COMPARATOR);
+
+ /**
+ * Compare the two files using the {@link File#compareTo(File)} method.
+ *
+ * @param obj1 The first file to compare
+ * @param obj2 The second file to compare
+ * @return the result of calling file1's
+ * {@link File#compareTo(File)} with file2 as the parameter.
+ */
+ public int compare(Object obj1, Object obj2) {
+ File file1 = (File)obj1;
+ File file2 = (File)obj2;
+ return file1.compareTo(file2);
+ }
+}
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DefaultFileComparator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DefaultFileComparator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ExtensionFileComparator.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ExtensionFileComparator.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ExtensionFileComparator.java (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ExtensionFileComparator.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import java.io.Serializable;
+import java.util.Comparator;
+
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOCase;
+
+/**
+ * Compare the file name <b>extensions</b> for order
+ * (see {@link FilenameUtils#getExtension(String)}).
+ * <p>
+ * This comparator can be used to sort lists or arrays of files
+ * by their file extension either in a case-sensitive, case-insensitive or
+ * system dependant case sensitive way. A number of singleton instances
+ * are provided for the various case sensitivity options (using {@link IOCase})
+ * and the reverse of those options.
+ * <p>
+ * Example of a <i>case-sensitive</i> file extension sort using the
+ * {@link #EXTENSION_COMPARATOR} singleton instance:
+ * <pre>
+ * List<File> list = ...
+ * Collections.sort(list, ExtensionFileComparator.EXTENSION_COMPARATOR);
+ * </pre>
+ * <p>
+ * Example of a <i>reverse case-insensitive</i> file extension sort using the
+ * {@link #EXTENSION_INSENSITIVE_REVERSE} singleton instance:
+ * <pre>
+ * File[] array = ...
+ * Arrays.sort(array, ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE);
+ * </pre>
+ * <p>
+ *
+ * @version $Revision$ $Date$
+ * @since Commons IO 1.4
+ */
+public class ExtensionFileComparator implements Comparator, Serializable {
+
+ /** Case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
+ public static final Comparator EXTENSION_COMPARATOR = new ExtensionFileComparator();
+
+ /** Reverse case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
+ public static final Comparator EXTENSION_REVERSE = new ReverseComparator(EXTENSION_COMPARATOR);
+
+ /** Case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
+ public static final Comparator EXTENSION_INSENSITIVE_COMPARATOR = new ExtensionFileComparator(IOCase.INSENSITIVE);
+
+ /** Reverse case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
+ public static final Comparator EXTENSION_INSENSITIVE_REVERSE
+ = new ReverseComparator(EXTENSION_INSENSITIVE_COMPARATOR);
+
+ /** System sensitive extension comparator instance (see {@link IOCase#SYSTEM}) */
+ public static final Comparator EXTENSION_SYSTEM_COMPARATOR = new ExtensionFileComparator(IOCase.SYSTEM);
+
+ /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
+ public static final Comparator EXTENSION_SYSTEM_REVERSE = new ReverseComparator(EXTENSION_SYSTEM_COMPARATOR);
+
+ /** Whether the comparison is case sensitive. */
+ private final IOCase caseSensitivity;
+
+ /**
+ * Construct a case sensitive file extension comparator instance.
+ */
+ public ExtensionFileComparator() {
+ this.caseSensitivity = IOCase.SENSITIVE;
+ }
+
+ /**
+ * Construct a file extension comparator instance with the specified case-sensitivity.
+ *
+ * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
+ */
+ public ExtensionFileComparator(IOCase caseSensitivity) {
+ this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
+ }
+
+ /**
+ * Compare the extensions of two files the specified case sensitivity.
+ *
+ * @param obj1 The first file to compare
+ * @param obj2 The second file to compare
+ * @return a negative value if the first file's extension
+ * is less than the second, zero if the extensions are the
+ * same and a positive value if the first files extension
+ * is greater than the second file.
+ *
+ */
+ public int compare(Object obj1, Object obj2) {
+ File file1 = (File)obj1;
+ File file2 = (File)obj2;
+ String suffix1 = FilenameUtils.getExtension(file1.getName());
+ String suffix2 = FilenameUtils.getExtension(file2.getName());
+ return caseSensitivity.checkCompareTo(suffix1, suffix2);
+ }
+}
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ExtensionFileComparator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ExtensionFileComparator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/LastModifiedFileComparator.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/LastModifiedFileComparator.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/LastModifiedFileComparator.java (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/LastModifiedFileComparator.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import java.io.Serializable;
+import java.util.Comparator;
+
+/**
+ * Compare the <b>last modified date/time</b> of two files for order
+ * (see {@link File#lastModified()}).
+ * <p>
+ * This comparator can be used to sort lists or arrays of files
+ * by their last modified date/time.
+ * <p>
+ * Example of sorting a list of files using the
+ * {@link #LASTMODIFIED_COMPARATOR} singleton instance:
+ * <pre>
+ * List<File> list = ...
+ * Collections.sort(list, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
+ * </pre>
+ * <p>
+ * Example of doing a <i>reverse</i> sort of an array of files using the
+ * {@link #LASTMODIFIED_REVERSE} singleton instance:
+ * <pre>
+ * File[] array = ...
+ * Arrays.sort(array, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
+ * </pre>
+ * <p>
+ *
+ * @version $Revision$ $Date$
+ * @since Commons IO 1.4
+ */
+public class LastModifiedFileComparator implements Comparator, Serializable {
+
+ /** Last modified comparator instance */
+ public static final Comparator LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator();
+
+ /** Reverse last modified comparator instance */
+ public static final Comparator LASTMODIFIED_REVERSE = new ReverseComparator(LASTMODIFIED_COMPARATOR);
+
+ /**
+ * Compare the last the last modified date/time of two files.
+ *
+ * @param obj1 The first file to compare
+ * @param obj2 The second file to compare
+ * @return a negative value if the first file's lastmodified date/time
+ * is less than the second, zero if the lastmodified date/time are the
+ * same and a positive value if the first files lastmodified date/time
+ * is greater than the second file.
+ *
+ */
+ public int compare(Object obj1, Object obj2) {
+ File file1 = (File)obj1;
+ File file2 = (File)obj2;
+ long result = file1.lastModified() - file2.lastModified();
+ if (result < 0) {
+ return -1;
+ } else if (result > 0) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+}
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/LastModifiedFileComparator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/LastModifiedFileComparator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/NameFileComparator.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/NameFileComparator.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/NameFileComparator.java (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/NameFileComparator.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import java.io.Serializable;
+import java.util.Comparator;
+
+import org.apache.commons.io.IOCase;
+
+/**
+ * Compare the <b>names</b> of two files for order (see {@link File#getName()}).
+ * <p>
+ * This comparator can be used to sort lists or arrays of files
+ * by their name either in a case-sensitive, case-insensitive or
+ * system dependant case sensitive way. A number of singleton instances
+ * are provided for the various case sensitivity options (using {@link IOCase})
+ * and the reverse of those options.
+ * <p>
+ * Example of a <i>case-sensitive</i> file name sort using the
+ * {@link #NAME_COMPARATOR} singleton instance:
+ * <pre>
+ * List<File> list = ...
+ * Collections.sort(list, NameFileComparator.NAME_COMPARATOR);
+ * </pre>
+ * <p>
+ * Example of a <i>reverse case-insensitive</i> file name sort using the
+ * {@link #NAME_INSENSITIVE_REVERSE} singleton instance:
+ * <pre>
+ * File[] array = ...
+ * Arrays.sort(array, NameFileComparator.NAME_INSENSITIVE_REVERSE);
+ * </pre>
+ * <p>
+ *
+ * @version $Revision$ $Date$
+ * @since Commons IO 1.4
+ */
+public class NameFileComparator implements Comparator, Serializable {
+
+ /** Case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */
+ public static final Comparator NAME_COMPARATOR = new NameFileComparator();
+
+ /** Reverse case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */
+ public static final Comparator NAME_REVERSE = new ReverseComparator(NAME_COMPARATOR);
+
+ /** Case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */
+ public static final Comparator NAME_INSENSITIVE_COMPARATOR = new NameFileComparator(IOCase.INSENSITIVE);
+
+ /** Reverse case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */
+ public static final Comparator NAME_INSENSITIVE_REVERSE = new ReverseComparator(NAME_INSENSITIVE_COMPARATOR);
+
+ /** System sensitive name comparator instance (see {@link IOCase#SYSTEM}) */
+ public static final Comparator NAME_SYSTEM_COMPARATOR = new NameFileComparator(IOCase.SYSTEM);
+
+ /** Reverse system sensitive name comparator instance (see {@link IOCase#SYSTEM}) */
+ public static final Comparator NAME_SYSTEM_REVERSE = new ReverseComparator(NAME_SYSTEM_COMPARATOR);
+
+ /** Whether the comparison is case sensitive. */
+ private final IOCase caseSensitivity;
+
+ /**
+ * Construct a case sensitive file name comparator instance.
+ */
+ public NameFileComparator() {
+ this.caseSensitivity = IOCase.SENSITIVE;
+ }
+
+ /**
+ * Construct a file name comparator instance with the specified case-sensitivity.
+ *
+ * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
+ */
+ public NameFileComparator(IOCase caseSensitivity) {
+ this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
+ }
+
+ /**
+ * Compare the names of two files with the specified case sensitivity.
+ *
+ * @param obj1 The first file to compare
+ * @param obj2 The second file to compare
+ * @return a negative value if the first file's name
+ * is less than the second, zero if the names are the
+ * same and a positive value if the first files name
+ * is greater than the second file.
+ */
+ public int compare(Object obj1, Object obj2) {
+ File file1 = (File)obj1;
+ File file2 = (File)obj2;
+ return caseSensitivity.checkCompareTo(file1.getName(), file2.getName());
+ }
+}
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/NameFileComparator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/NameFileComparator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/PathFileComparator.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/PathFileComparator.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/PathFileComparator.java (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/PathFileComparator.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import java.io.Serializable;
+import java.util.Comparator;
+
+import org.apache.commons.io.IOCase;
+
+/**
+ * Compare the <b>path</b> of two files for order (see {@link File#getPath()}).
+ * <p>
+ * This comparator can be used to sort lists or arrays of files
+ * by their path either in a case-sensitive, case-insensitive or
+ * system dependant case sensitive way. A number of singleton instances
+ * are provided for the various case sensitivity options (using {@link IOCase})
+ * and the reverse of those options.
+ * <p>
+ * Example of a <i>case-sensitive</i> file path sort using the
+ * {@link #PATH_COMPARATOR} singleton instance:
+ * <pre>
+ * List<File> list = ...
+ * Collections.sort(list, PathFileComparator.PATH_COMPARATOR);
+ * </pre>
+ * <p>
+ * Example of a <i>reverse case-insensitive</i> file path sort using the
+ * {@link #PATH_INSENSITIVE_REVERSE} singleton instance:
+ * <pre>
+ * File[] array = ...
+ * Arrays.sort(array, PathFileComparator.PATH_INSENSITIVE_REVERSE);
+ * </pre>
+ * <p>
+ *
+ * @version $Revision$ $Date$
+ * @since Commons IO 1.4
+ */
+public class PathFileComparator implements Comparator, Serializable {
+
+ /** Case-sensitive path comparator instance (see {@link IOCase#SENSITIVE}) */
+ public static final Comparator PATH_COMPARATOR = new PathFileComparator();
+
+ /** Reverse case-sensitive path comparator instance (see {@link IOCase#SENSITIVE}) */
+ public static final Comparator PATH_REVERSE = new ReverseComparator(PATH_COMPARATOR);
+
+ /** Case-insensitive path comparator instance (see {@link IOCase#INSENSITIVE}) */
+ public static final Comparator PATH_INSENSITIVE_COMPARATOR = new PathFileComparator(IOCase.INSENSITIVE);
+
+ /** Reverse case-insensitive path comparator instance (see {@link IOCase#INSENSITIVE}) */
+ public static final Comparator PATH_INSENSITIVE_REVERSE = new ReverseComparator(PATH_INSENSITIVE_COMPARATOR);
+
+ /** System sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
+ public static final Comparator PATH_SYSTEM_COMPARATOR = new PathFileComparator(IOCase.SYSTEM);
+
+ /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
+ public static final Comparator PATH_SYSTEM_REVERSE = new ReverseComparator(PATH_SYSTEM_COMPARATOR);
+
+ /** Whether the comparison is case sensitive. */
+ private final IOCase caseSensitivity;
+
+ /**
+ * Construct a case sensitive file path comparator instance.
+ */
+ public PathFileComparator() {
+ this.caseSensitivity = IOCase.SENSITIVE;
+ }
+
+ /**
+ * Construct a file path comparator instance with the specified case-sensitivity.
+ *
+ * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
+ */
+ public PathFileComparator(IOCase caseSensitivity) {
+ this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
+ }
+
+ /**
+ * Compare the paths of two files the specified case sensitivity.
+ *
+ * @param obj1 The first file to compare
+ * @param obj2 The second file to compare
+ * @return a negative value if the first file's path
+ * is less than the second, zero if the paths are the
+ * same and a positive value if the first files path
+ * is greater than the second file.
+ *
+ */
+ public int compare(Object obj1, Object obj2) {
+ File file1 = (File)obj1;
+ File file2 = (File)obj2;
+ return caseSensitivity.checkCompareTo(file1.getPath(), file2.getPath());
+ }
+}
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/PathFileComparator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/PathFileComparator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ReverseComparator.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ReverseComparator.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ReverseComparator.java (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ReverseComparator.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.Serializable;
+import java.util.Comparator;
+
+/**
+ * Reverses the result of comparing two objects using
+ * the delegate {@link Comparator}.
+ *
+ * @version $Revision$ $Date$
+ * @since Commons IO 1.4
+ */
+class ReverseComparator implements Comparator, Serializable {
+
+ private final Comparator delegate;
+
+ /**
+ * Construct an instance with the sepecified delegate {@link Comparator}.
+ *
+ * @param delegate The comparator to delegate to
+ */
+ public ReverseComparator(Comparator delegate) {
+ if (delegate == null) {
+ throw new IllegalArgumentException("Delegate comparator is missing");
+ }
+ this.delegate = delegate;
+ }
+
+ /**
+ * Compare using the delegate Comparator, but reversing the result.
+ *
+ * @param obj1 The first object to compare
+ * @param obj2 The second object to compare
+ * @return the result from the delegate {@link Comparator#compare(Object, Object)}
+ * reversing the value (i.e. positive becomes negative and vice versa)
+ */
+ public int compare(Object obj1, Object obj2) {
+ return delegate.compare(obj2, obj1); // parameters switched round
+ }
+
+}
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ReverseComparator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/ReverseComparator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/SizeFileComparator.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/SizeFileComparator.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/SizeFileComparator.java (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/SizeFileComparator.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import java.io.Serializable;
+import java.util.Comparator;
+
+import org.apache.commons.io.FileUtils;
+
+/**
+ * Compare the <b>length/size</b> of two files for order (see
+ * {@link File#length()} and {@link FileUtils#sizeOfDirectory(File)}).
+ * <p>
+ * This comparator can be used to sort lists or arrays of files
+ * by their length/size.
+ * <p>
+ * Example of sorting a list of files using the
+ * {@link #SIZE_COMPARATOR} singleton instance:
+ * <pre>
+ * List<File> list = ...
+ * Collections.sort(list, LengthFileComparator.LENGTH_COMPARATOR);
+ * </pre>
+ * <p>
+ * Example of doing a <i>reverse</i> sort of an array of files using the
+ * {@link #SIZE_REVERSE} singleton instance:
+ * <pre>
+ * File[] array = ...
+ * Arrays.sort(array, LengthFileComparator.LENGTH_REVERSE);
+ * </pre>
+ * <p>
+ * <strong>N.B.</strong> Directories are treated as <b>zero size</b> unless
+ * <code>sumDirectoryContents</code> is <code>true</code>.
+ *
+ * @version $Revision$ $Date$
+ * @since Commons IO 1.4
+ */
+public class SizeFileComparator implements Comparator, Serializable {
+
+ /** Size comparator instance - directories are treated as zero size */
+ public static final Comparator SIZE_COMPARATOR = new SizeFileComparator();
+
+ /** Reverse size comparator instance - directories are treated as zero size */
+ public static final Comparator SIZE_REVERSE = new ReverseComparator(SIZE_COMPARATOR);
+
+ /**
+ * Size comparator instance which sums the size of a directory's contents
+ * using {@link FileUtils#sizeOfDirectory(File)}
+ */
+ public static final Comparator SIZE_SUMDIR_COMPARATOR = new SizeFileComparator(true);
+
+ /**
+ * Reverse size comparator instance which sums the size of a directory's contents
+ * using {@link FileUtils#sizeOfDirectory(File)}
+ */
+ public static final Comparator SIZE_SUMDIR_REVERSE = new ReverseComparator(SIZE_SUMDIR_COMPARATOR);
+
+ /** Whether the sum of the directory's contents should be calculated. */
+ private final boolean sumDirectoryContents;
+
+ /**
+ * Construct a file size comparator instance (directories treated as zero size).
+ */
+ public SizeFileComparator() {
+ this.sumDirectoryContents = false;
+ }
+
+ /**
+ * Construct a file size comparator instance specifying whether the size of
+ * the directory contents should be aggregated.
+ * <p>
+ * If the <code>sumDirectoryContents</code> is <code>true</code> The size of
+ * directories is calculated using {@link FileUtils#sizeOfDirectory(File)}.
+ *
+ * @param sumDirectoryContents <code>true</code> if the sum of the directoryies contents
+ * should be calculated, otherwise <code>false</code> if directories should be treated
+ * as size zero (see {@link FileUtils#sizeOfDirectory(File)}).
+ */
+ public SizeFileComparator(boolean sumDirectoryContents) {
+ this.sumDirectoryContents = sumDirectoryContents;
+ }
+
+ /**
+ * Compare the length of two files.
+ *
+ * @param obj1 The first file to compare
+ * @param obj2 The second file to compare
+ * @return a negative value if the first file's length
+ * is less than the second, zero if the lengths are the
+ * same and a positive value if the first files length
+ * is greater than the second file.
+ *
+ */
+ public int compare(Object obj1, Object obj2) {
+ File file1 = (File)obj1;
+ File file2 = (File)obj2;
+ long size1 = 0;
+ if (file1.isDirectory()) {
+ size1 = sumDirectoryContents && file1.exists() ? FileUtils.sizeOfDirectory(file1) : 0;
+ } else {
+ size1 = file1.length();
+ }
+ long size2 = 0;
+ if (file2.isDirectory()) {
+ size2 = sumDirectoryContents && file2.exists() ? FileUtils.sizeOfDirectory(file2) : 0;
+ } else {
+ size2 = file2.length();
+ }
+ long result = size1 - size2;
+ if (result < 0) {
+ return -1;
+ } else if (result > 0) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+}
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/SizeFileComparator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/SizeFileComparator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/package.html
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/package.html?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/package.html (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/package.html Sat Jan 5 16:30:42 2008
@@ -0,0 +1,25 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+<html>
+<body>
+<p>This package provides various {@link java.util.Comparator} implementations
+for {@link java.io.File}s.
+</p>
+
+</body>
+</html>
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/package.html
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/package.html
------------------------------------------------------------------------------
svn:mime-type = text/html
Added: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ComparatorAbstractTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ComparatorAbstractTestCase.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ComparatorAbstractTestCase.java (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ComparatorAbstractTestCase.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import java.util.Comparator;
+import org.apache.commons.io.testtools.FileBasedTestCase;
+
+/**
+ * Base Test case for Comparator implementations.
+ */
+public abstract class ComparatorAbstractTestCase extends FileBasedTestCase {
+
+ /** comparator instance */
+ protected Comparator comparator;
+
+ /** reverse comparator instance */
+ protected Comparator reverse;
+
+ /** File which compares equal to "equalFile2" */
+ protected File equalFile1;
+
+ /** File which compares equal to "equalFile1" */
+ protected File equalFile2;
+
+ /** File which is less than the "moreFile" */
+ protected File lessFile;
+
+ /** File which is more than the "lessFile" */
+ protected File moreFile;
+
+ /**
+ * Construct a new test case with the specified name
+ * @param name Name of the test
+ */
+ public ComparatorAbstractTestCase(String name) {
+ super(name);
+ }
+
+ /** @see junit.framework.TestCase#setUp() */
+ protected void setUp() throws Exception {
+ comparator = DefaultFileComparator.DEFAULT_COMPARATOR;
+ reverse = DefaultFileComparator.DEFAULT_REVERSE;
+ }
+
+ /** @see junit.framework.TestCase#tearDown() */
+ protected void tearDown() throws Exception {
+ comparator = null;
+ reverse = null;
+ equalFile1 = null;
+ equalFile2 = null;
+ lessFile = null;
+ moreFile = null;
+ }
+
+ /**
+ * Test the comparator.
+ */
+ public void testComparator() {
+ assertTrue("equal", comparator.compare(equalFile1, equalFile2) == 0);
+ assertTrue("less", comparator.compare(lessFile, moreFile) < 0);
+ assertTrue("more", comparator.compare(moreFile, lessFile) > 0);
+ }
+
+ /**
+ * Test the comparator reversed.
+ */
+ public void testReverseComparator() {
+ assertTrue("equal", reverse.compare(equalFile1, equalFile2) == 0);
+ assertTrue("less", reverse.compare(moreFile, lessFile) < 0);
+ assertTrue("more", reverse.compare(lessFile, moreFile) > 0);
+ }
+}
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ComparatorAbstractTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ComparatorAbstractTestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DefaultFileComparatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DefaultFileComparatorTest.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DefaultFileComparatorTest.java (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DefaultFileComparatorTest.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * Test case for {@link DefaultFileComparator}.
+ */
+public class DefaultFileComparatorTest extends ComparatorAbstractTestCase {
+
+ /**
+ * Run the test.
+ *
+ * @param args arguments
+ */
+ public static void main(String[] args) {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Create a test suite.
+ *
+ * @return The test suite
+ */
+ public static Test suite() {
+ return new TestSuite(DefaultFileComparatorTest.class);
+ }
+
+ /**
+ * Construct a new test case with the specified name.
+ *
+ * @param name Name of the test
+ */
+ public DefaultFileComparatorTest(String name) {
+ super(name);
+ }
+
+ /** @see junit.framework.TestCase#setUp() */
+ protected void setUp() throws Exception {
+ super.setUp();
+ comparator = DefaultFileComparator.DEFAULT_COMPARATOR;
+ reverse = DefaultFileComparator.DEFAULT_REVERSE;
+ equalFile1 = new File("same");
+ equalFile2 = new File("same");
+ lessFile = new File("ABC");
+ moreFile = new File("DEF");
+ }
+}
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DefaultFileComparatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DefaultFileComparatorTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ExtensionFileComparatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ExtensionFileComparatorTest.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ExtensionFileComparatorTest.java (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ExtensionFileComparatorTest.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * Test case for {@link ExtensionFileComparator}.
+ */
+public class ExtensionFileComparatorTest extends ComparatorAbstractTestCase {
+
+ /**
+ * Run the test.
+ *
+ * @param args arguments
+ */
+ public static void main(String[] args) {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Create a test suite.
+ *
+ * @return The test suite
+ */
+ public static Test suite() {
+ return new TestSuite(ExtensionFileComparatorTest.class);
+ }
+
+ /**
+ * Construct a new test case with the specified name.
+ *
+ * @param name Name of the test
+ */
+ public ExtensionFileComparatorTest(String name) {
+ super(name);
+ }
+
+ /** @see junit.framework.TestCase#setUp() */
+ protected void setUp() throws Exception {
+ super.setUp();
+ comparator = ExtensionFileComparator.EXTENSION_COMPARATOR;
+ reverse = ExtensionFileComparator.EXTENSION_REVERSE;
+ equalFile1 = new File("foo.txt");
+ equalFile2 = new File("bar.txt");
+ lessFile = new File("DEF.doc");
+ moreFile = new File("ABC.txt");
+ }
+}
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ExtensionFileComparatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/ExtensionFileComparatorTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * Test case for {@link LastModifiedFileComparator}.
+ */
+public class LastModifiedFileComparatorTest extends ComparatorAbstractTestCase {
+
+ /**
+ * Run the test.
+ *
+ * @param args arguments
+ */
+ public static void main(String[] args) {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Create a test suite.
+ *
+ * @return The test suite
+ */
+ public static Test suite() {
+ return new TestSuite(LastModifiedFileComparatorTest.class);
+ }
+
+ /**
+ * Construct a new test case with the specified name.
+ *
+ * @param name Name of the test
+ */
+ public LastModifiedFileComparatorTest(String name) {
+ super(name);
+ }
+
+ /** @see junit.framework.TestCase#setUp() */
+ protected void setUp() throws Exception {
+ super.setUp();
+ comparator = LastModifiedFileComparator.LASTMODIFIED_COMPARATOR;
+ reverse = LastModifiedFileComparator.LASTMODIFIED_REVERSE;
+ File dir = getTestDirectory();
+ File olderFile = new File(dir, "older.txt");
+ createFile(olderFile, 0);
+
+ File newerFile = new File(dir, "newer.txt");
+ do {
+ try {
+ Thread.sleep(300);
+ } catch(InterruptedException ie) {
+ // ignore
+ }
+ createFile(newerFile, 0);
+ } while( olderFile.lastModified() == newerFile.lastModified() );
+ equalFile1 = olderFile;
+ equalFile2 = olderFile;
+ lessFile = olderFile;
+ moreFile = newerFile;
+ }
+
+}
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/NameFileComparatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/NameFileComparatorTest.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/NameFileComparatorTest.java (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/NameFileComparatorTest.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * Test case for {@link NameFileComparator}.
+ */
+public class NameFileComparatorTest extends ComparatorAbstractTestCase {
+
+ /**
+ * Run the test.
+ *
+ * @param args arguments
+ */
+ public static void main(String[] args) {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Create a test suite.
+ *
+ * @return The test suite
+ */
+ public static Test suite() {
+ return new TestSuite(NameFileComparatorTest.class);
+ }
+
+ /**
+ * Construct a new test case with the specified name.
+ *
+ * @param name Name of the test
+ */
+ public NameFileComparatorTest(String name) {
+ super(name);
+ }
+
+ /** @see junit.framework.TestCase#setUp() */
+ protected void setUp() throws Exception {
+ super.setUp();
+ comparator = NameFileComparator.NAME_COMPARATOR;
+ reverse = NameFileComparator.NAME_REVERSE;
+ equalFile1 = new File("a/foo.txt");
+ equalFile2 = new File("b/foo.txt");
+ lessFile = new File("c/ABC.txt");
+ moreFile = new File("d/DEF.txt");
+ }
+}
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/NameFileComparatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/NameFileComparatorTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/PathFileComparatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/PathFileComparatorTest.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/PathFileComparatorTest.java (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/PathFileComparatorTest.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * Test case for {@link PathFileComparator}.
+ */
+public class PathFileComparatorTest extends ComparatorAbstractTestCase {
+
+ /**
+ * Run the test.
+ *
+ * @param args arguments
+ */
+ public static void main(String[] args) {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Create a test suite.
+ *
+ * @return The test suite
+ */
+ public static Test suite() {
+ return new TestSuite(PathFileComparatorTest.class);
+ }
+
+ /**
+ * Construct a new test case with the specified name.
+ *
+ * @param name Name of the test
+ */
+ public PathFileComparatorTest(String name) {
+ super(name);
+ }
+
+ /** @see junit.framework.TestCase#setUp() */
+ protected void setUp() throws Exception {
+ super.setUp();
+ comparator = PathFileComparator.PATH_COMPARATOR;
+ reverse = PathFileComparator.PATH_REVERSE;
+ equalFile1 = new File("foo/file.txt");
+ equalFile2 = new File("foo/file.txt");
+ lessFile = new File("abc/file.txt");
+ moreFile = new File("def/file.txt");
+ }
+}
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/PathFileComparatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/PathFileComparatorTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/SizeFileComparatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/SizeFileComparatorTest.java?rev=609243&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/SizeFileComparatorTest.java (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/SizeFileComparatorTest.java Sat Jan 5 16:30:42 2008
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.comparator;
+
+import java.io.File;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * Test case for {@link SizeFileComparator}.
+ */
+public class SizeFileComparatorTest extends ComparatorAbstractTestCase {
+
+ private File smallerDir;
+ private File largerDir;
+ private File smallerFile;
+ private File largerFile;
+
+ /**
+ * Run the test.
+ *
+ * @param args arguments
+ */
+ public static void main(String[] args) {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Create a test suite.
+ *
+ * @return The test suite
+ */
+ public static Test suite() {
+ return new TestSuite(SizeFileComparatorTest.class);
+ }
+
+ /**
+ * Construct a new test case with the specified name.
+ *
+ * @param name Name of the test
+ */
+ public SizeFileComparatorTest(String name) {
+ super(name);
+ }
+
+ /** @see junit.framework.TestCase#setUp() */
+ protected void setUp() throws Exception {
+ super.setUp();
+ comparator = SizeFileComparator.SIZE_COMPARATOR;
+ reverse = SizeFileComparator.SIZE_REVERSE;
+ File dir = getTestDirectory();
+ smallerDir = new File(dir, "smallerdir");
+ largerDir = new File(dir, "largerdir");
+ smallerFile = new File(smallerDir, "smaller.txt");
+ largerFile = new File(largerDir, "larger.txt");
+ smallerDir.mkdir();
+ largerDir.mkdir();
+ createFile(smallerFile, 32);
+ createFile(largerFile, 64);
+ equalFile1 = smallerFile;
+ equalFile2 = smallerFile;
+ lessFile = smallerFile;
+ moreFile = largerFile;
+ }
+
+ /**
+ * Test a file which doesn't exist.
+ */
+ public void testNonexistantFile() {
+ File nonexistantFile = new File(new File("."), "nonexistant.txt");
+ assertFalse(nonexistantFile.exists());
+ assertTrue("less", comparator.compare(nonexistantFile, moreFile) < 0);
+ }
+
+ /**
+ * Test a file which doesn't exist.
+ */
+ public void testCompareDirectorySizes() {
+ assertEquals("sumDirectoryContents=false", 0, comparator.compare(smallerDir, largerDir));
+ assertEquals("less", -1, SizeFileComparator.SIZE_SUMDIR_COMPARATOR.compare(smallerDir, largerDir));
+ assertEquals("less", 1, SizeFileComparator.SIZE_SUMDIR_REVERSE.compare(smallerDir, largerDir));
+ }
+}
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/SizeFileComparatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/SizeFileComparatorTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL