001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.io.comparator;
018    
019    import java.io.File;
020    import java.io.Serializable;
021    import java.util.Comparator;
022    
023    import org.apache.commons.io.FilenameUtils;
024    import org.apache.commons.io.IOCase;
025    
026    /**
027     * Compare the file name <b>extensions</b> for order
028     * (see {@link FilenameUtils#getExtension(String)}).
029     * <p>
030     * This comparator can be used to sort lists or arrays of files
031     * by their file extension either in a case-sensitive, case-insensitive or
032     * system dependant case sensitive way. A number of singleton instances
033     * are provided for the various case sensitivity options (using {@link IOCase})
034     * and the reverse of those options.
035     * <p>
036     * Example of a <i>case-sensitive</i> file extension sort using the
037     * {@link #EXTENSION_COMPARATOR} singleton instance:
038     * <pre>
039     *       List&lt;File&gt; list = ...
040     *       ExtensionFileComparator.EXTENSION_COMPARATOR.sort(list);
041     * </pre>
042     * <p>
043     * Example of a <i>reverse case-insensitive</i> file extension sort using the
044     * {@link #EXTENSION_INSENSITIVE_REVERSE} singleton instance:
045     * <pre>
046     *       File[] array = ...
047     *       ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE.sort(array);
048     * </pre>
049     * <p>
050     *
051     * @version $Id: ExtensionFileComparator.java 1304052 2012-03-22 20:55:29Z ggregory $
052     * @since 1.4
053     */
054    public class ExtensionFileComparator extends AbstractFileComparator implements Serializable {
055    
056        /** Case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
057        public static final Comparator<File> EXTENSION_COMPARATOR = new ExtensionFileComparator();
058    
059        /** Reverse case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
060        public static final Comparator<File> EXTENSION_REVERSE = new ReverseComparator(EXTENSION_COMPARATOR);
061    
062        /** Case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
063        public static final Comparator<File> EXTENSION_INSENSITIVE_COMPARATOR
064                                                    = new ExtensionFileComparator(IOCase.INSENSITIVE);
065    
066        /** Reverse case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
067        public static final Comparator<File> EXTENSION_INSENSITIVE_REVERSE
068                                                    = new ReverseComparator(EXTENSION_INSENSITIVE_COMPARATOR);
069    
070        /** System sensitive extension comparator instance (see {@link IOCase#SYSTEM}) */
071        public static final Comparator<File> EXTENSION_SYSTEM_COMPARATOR = new ExtensionFileComparator(IOCase.SYSTEM);
072    
073        /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
074        public static final Comparator<File> EXTENSION_SYSTEM_REVERSE = new ReverseComparator(EXTENSION_SYSTEM_COMPARATOR);
075    
076        /** Whether the comparison is case sensitive. */
077        private final IOCase caseSensitivity;
078    
079        /**
080         * Construct a case sensitive file extension comparator instance.
081         */
082        public ExtensionFileComparator() {
083            this.caseSensitivity = IOCase.SENSITIVE;
084        }
085    
086        /**
087         * Construct a file extension comparator instance with the specified case-sensitivity.
088         *
089         * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
090         */
091        public ExtensionFileComparator(IOCase caseSensitivity) {
092            this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
093        }
094    
095        /**
096         * Compare the extensions of two files the specified case sensitivity.
097         * 
098         * @param file1 The first file to compare
099         * @param file2 The second file to compare
100         * @return a negative value if the first file's extension
101         * is less than the second, zero if the extensions are the
102         * same and a positive value if the first files extension
103         * is greater than the second file.
104         * 
105         */
106        public int compare(File file1, File file2) {
107            String suffix1 = FilenameUtils.getExtension(file1.getName());
108            String suffix2 = FilenameUtils.getExtension(file2.getName());
109            return caseSensitivity.checkCompareTo(suffix1, suffix2);
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() + "[caseSensitivity=" + caseSensitivity + "]";
120        }
121    }