14fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/*
24fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Licensed to the Apache Software Foundation (ASF) under one or more
34fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * contributor license agreements.  See the NOTICE file distributed with
44fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * this work for additional information regarding copyright ownership.
54fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * The ASF licenses this file to You under the Apache License, Version 2.0
64fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * (the "License"); you may not use this file except in compliance with
74fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * the License.  You may obtain a copy of the License at
84fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
94fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Unless required by applicable law or agreed to in writing, software
124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * See the License for the specific language governing permissions and
154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * limitations under the License.
164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypackage org.apache.commons.io.comparator;
184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.File;
204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.Serializable;
214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.util.Comparator;
224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport org.apache.commons.io.FilenameUtils;
244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport org.apache.commons.io.IOCase;
254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/**
274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Compare the file name <b>extensions</b> for order
284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * (see {@link FilenameUtils#getExtension(String)}).
294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p>
304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * This comparator can be used to sort lists or arrays of files
314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * by their file extension either in a case-sensitive, case-insensitive or
324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * system dependant case sensitive way. A number of singleton instances
334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * are provided for the various case sensitivity options (using {@link IOCase})
344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * and the reverse of those options.
354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p>
364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Example of a <i>case-sensitive</i> file extension sort using the
374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * {@link #EXTENSION_COMPARATOR} singleton instance:
384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <pre>
394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *       List&lt;File&gt; list = ...
404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *       Collections.sort(list, ExtensionFileComparator.EXTENSION_COMPARATOR);
414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * </pre>
424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p>
434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Example of a <i>reverse case-insensitive</i> file extension sort using the
444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * {@link #EXTENSION_INSENSITIVE_REVERSE} singleton instance:
454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <pre>
464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *       File[] array = ...
474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *       Arrays.sort(array, ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE);
484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * </pre>
494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p>
504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @version $Revision: 609243 $ $Date: 2008-01-06 00:30:42 +0000 (Sun, 06 Jan 2008) $
524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @since Commons IO 1.4
534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypublic class ExtensionFileComparator implements Comparator<File>, Serializable {
554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** Case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public static final Comparator<File> EXTENSION_COMPARATOR = new ExtensionFileComparator();
584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** Reverse case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public static final Comparator<File> EXTENSION_REVERSE = new ReverseComparator<File>(EXTENSION_COMPARATOR);
614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** Case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public static final Comparator<File> EXTENSION_INSENSITIVE_COMPARATOR = new ExtensionFileComparator(IOCase.INSENSITIVE);
644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** Reverse case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public static final Comparator<File> EXTENSION_INSENSITIVE_REVERSE
674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy                                                = new ReverseComparator<File>(EXTENSION_INSENSITIVE_COMPARATOR);
684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** System sensitive extension comparator instance (see {@link IOCase#SYSTEM}) */
704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public static final Comparator<File> EXTENSION_SYSTEM_COMPARATOR = new ExtensionFileComparator(IOCase.SYSTEM);
714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public static final Comparator<File> EXTENSION_SYSTEM_REVERSE = new ReverseComparator<File>(EXTENSION_SYSTEM_COMPARATOR);
744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** Whether the comparison is case sensitive. */
764fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private final IOCase caseSensitivity;
774fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
784fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
794fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Construct a case sensitive file extension comparator instance.
804fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
814fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public ExtensionFileComparator() {
824fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.caseSensitivity = IOCase.SENSITIVE;
834fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
844fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
854fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
864fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Construct a file extension comparator instance with the specified case-sensitivity.
874fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
884fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
894fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
904fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public ExtensionFileComparator(IOCase caseSensitivity) {
914fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
924fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
934fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
944fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
954fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Compare the extensions of two files the specified case sensitivity.
964fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
974fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param obj1 The first file to compare
984fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param obj2 The second file to compare
994fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return a negative value if the first file's extension
1004fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * is less than the second, zero if the extensions are the
1014fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * same and a positive value if the first files extension
1024fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * is greater than the second file.
1034fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1044fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1054fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public int compare(File file1, File file2) {
1064fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        String suffix1 = FilenameUtils.getExtension(file1.getName());
1074fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        String suffix2 = FilenameUtils.getExtension(file2.getName());
1084fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return caseSensitivity.checkCompareTo(suffix1, suffix2);
1094fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy}
111