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 */
017package org.apache.commons.io.comparator;
018
019import java.io.File;
020import java.io.Serializable;
021import java.util.Comparator;
022
023import org.apache.commons.io.FilenameUtils;
024import 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 dependent 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 *       ((AbstractFileComparator) 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 *       ((AbstractFileComparator) ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE).sort(array);
048 * </pre>
049 * <p>
050 *
051 * @version $Id: ExtensionFileComparator.java 1642757 2014-12-01 21:09:30Z sebb $
052 * @since 1.4
053 */
054public class ExtensionFileComparator extends AbstractFileComparator implements Serializable {
055
056    private static final long serialVersionUID = 1928235200184222815L;
057
058    /** Case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
059    public static final Comparator<File> EXTENSION_COMPARATOR = new ExtensionFileComparator();
060
061    /** Reverse case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
062    public static final Comparator<File> EXTENSION_REVERSE = new ReverseComparator(EXTENSION_COMPARATOR);
063
064    /** Case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
065    public static final Comparator<File> EXTENSION_INSENSITIVE_COMPARATOR
066                                                = new ExtensionFileComparator(IOCase.INSENSITIVE);
067
068    /** Reverse case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
069    public static final Comparator<File> EXTENSION_INSENSITIVE_REVERSE
070                                                = new ReverseComparator(EXTENSION_INSENSITIVE_COMPARATOR);
071
072    /** System sensitive extension comparator instance (see {@link IOCase#SYSTEM}) */
073    public static final Comparator<File> EXTENSION_SYSTEM_COMPARATOR = new ExtensionFileComparator(IOCase.SYSTEM);
074
075    /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
076    public static final Comparator<File> EXTENSION_SYSTEM_REVERSE = new ReverseComparator(EXTENSION_SYSTEM_COMPARATOR);
077
078    /** Whether the comparison is case sensitive. */
079    private final IOCase caseSensitivity;
080
081    /**
082     * Construct a case sensitive file extension comparator instance.
083     */
084    public ExtensionFileComparator() {
085        this.caseSensitivity = IOCase.SENSITIVE;
086    }
087
088    /**
089     * Construct a file extension comparator instance with the specified case-sensitivity.
090     *
091     * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
092     */
093    public ExtensionFileComparator(final IOCase caseSensitivity) {
094        this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
095    }
096
097    /**
098     * Compare the extensions of two files the specified case sensitivity.
099     * 
100     * @param file1 The first file to compare
101     * @param file2 The second file to compare
102     * @return a negative value if the first file's extension
103     * is less than the second, zero if the extensions are the
104     * same and a positive value if the first files extension
105     * is greater than the second file.
106     * 
107     */
108    public int compare(final File file1, final File file2) {
109        final String suffix1 = FilenameUtils.getExtension(file1.getName());
110        final String suffix2 = FilenameUtils.getExtension(file2.getName());
111        return caseSensitivity.checkCompareTo(suffix1, suffix2);
112    }
113
114    /**
115     * String representation of this file comparator.
116     *
117     * @return String representation of this file comparator
118     */
119    @Override
120    public String toString() {
121        return super.toString() + "[caseSensitivity=" + caseSensitivity + "]";
122    }
123}