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 * @since 1.4
052 */
053public class ExtensionFileComparator extends AbstractFileComparator implements Serializable {
054
055    private static final long serialVersionUID = 1928235200184222815L;
056
057    /** Case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
058    public static final Comparator<File> EXTENSION_COMPARATOR = new ExtensionFileComparator();
059
060    /** Reverse case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
061    public static final Comparator<File> EXTENSION_REVERSE = new ReverseFileComparator(EXTENSION_COMPARATOR);
062
063    /** Case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
064    public static final Comparator<File> EXTENSION_INSENSITIVE_COMPARATOR
065                                                = new ExtensionFileComparator(IOCase.INSENSITIVE);
066
067    /** Reverse case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
068    public static final Comparator<File> EXTENSION_INSENSITIVE_REVERSE
069                                                = new ReverseFileComparator(EXTENSION_INSENSITIVE_COMPARATOR);
070
071    /** System sensitive extension comparator instance (see {@link IOCase#SYSTEM}) */
072    public static final Comparator<File> EXTENSION_SYSTEM_COMPARATOR = new ExtensionFileComparator(IOCase.SYSTEM);
073
074    /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
075    public static final Comparator<File> EXTENSION_SYSTEM_REVERSE = new ReverseFileComparator(EXTENSION_SYSTEM_COMPARATOR);
076
077    /** Whether the comparison is case sensitive. */
078    private final IOCase caseSensitivity;
079
080    /**
081     * Construct a case sensitive file extension comparator instance.
082     */
083    public ExtensionFileComparator() {
084        this.caseSensitivity = IOCase.SENSITIVE;
085    }
086
087    /**
088     * Construct a file extension comparator instance with the specified case-sensitivity.
089     *
090     * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
091     */
092    public ExtensionFileComparator(final IOCase caseSensitivity) {
093        this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
094    }
095
096    /**
097     * Compare the extensions of two files the specified case sensitivity.
098     *
099     * @param file1 The first file to compare
100     * @param file2 The second file to compare
101     * @return a negative value if the first file's extension
102     * is less than the second, zero if the extensions are the
103     * same and a positive value if the first files extension
104     * is greater than the second file.
105     *
106     */
107    @Override
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}