    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.IOCase;
    024
    025    /**
    026     * Compare the <b>path</b> of two files for order (see {@link File#getPath()}).
    027     * <p>
    028     * This comparator can be used to sort lists or arrays of files
    029     * by their path either in a case-sensitive, case-insensitive or
    030     * system dependant case sensitive way. A number of singleton instances
    031     * are provided for the various case sensitivity options (using {@link IOCase})
    032     * and the reverse of those options.
    033     * <p>
    034     * Example of a <i>case-sensitive</i> file path sort using the
    035     * {@link #PATH_COMPARATOR} singleton instance:
    036     * <pre>
    037     *       List&lt;File&gt; list = ...
    038     *       PathFileComparator.PATH_COMPARATOR.sort(list);
    039     * </pre>
    040     * <p>
    041     * Example of a <i>reverse case-insensitive</i> file path sort using the
    042     * {@link #PATH_INSENSITIVE_REVERSE} singleton instance:
    043     * <pre>
    044     *       File[] array = ...
    045     *       PathFileComparator.PATH_INSENSITIVE_REVERSE.sort(array);
    046     * </pre>
    047     * <p>
    048     *
    049     * @version $Revision: 723942 $ $Date: 2008-12-06 01:24:06 +0000 (Sat, 06 Dec 2008) $
    050     * @since Commons IO 1.4
    051     */
    052    public class PathFileComparator extends AbstractFileComparator implements Serializable {
    053
    054        /** Case-sensitive path comparator instance (see {@link IOCase#SENSITIVE}) */
    055        public static final Comparator<File> PATH_COMPARATOR = new PathFileComparator();
    056
    057        /** Reverse case-sensitive path comparator instance (see {@link IOCase#SENSITIVE}) */
    058        public static final Comparator<File> PATH_REVERSE = new ReverseComparator(PATH_COMPARATOR);
    059
    060        /** Case-insensitive path comparator instance (see {@link IOCase#INSENSITIVE}) */
    061        public static final Comparator<File> PATH_INSENSITIVE_COMPARATOR = new PathFileComparator(IOCase.INSENSITIVE);
    062
    063        /** Reverse case-insensitive path comparator instance (see {@link IOCase#INSENSITIVE}) */
    064        public static final Comparator<File> PATH_INSENSITIVE_REVERSE = new ReverseComparator(PATH_INSENSITIVE_COMPARATOR);
    065
    066        /** System sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
    067        public static final Comparator<File> PATH_SYSTEM_COMPARATOR = new PathFileComparator(IOCase.SYSTEM);
    068
    069        /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
    070        public static final Comparator<File> PATH_SYSTEM_REVERSE = new ReverseComparator(PATH_SYSTEM_COMPARATOR);
    071
    072        /** Whether the comparison is case sensitive. */
    073        private final IOCase caseSensitivity;
    074
    075        /**
    076         * Construct a case sensitive file path comparator instance.
    077         */
    078        public PathFileComparator() {
    079            this.caseSensitivity = IOCase.SENSITIVE;
    080        }
    081
    082        /**
    083         * Construct a file path comparator instance with the specified case-sensitivity.
    084         *
    085         * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
    086         */
    087        public PathFileComparator(IOCase caseSensitivity) {
    088            this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
    089        }
    090
    091        /**
    092         * Compare the paths of two files the specified case sensitivity.
    093         *
    094         * @param file1 The first file to compare
    095         * @param file2 The second file to compare
    096         * @return a negative value if the first file's path
    097         * is less than the second, zero if the paths are the
    098         * same and a positive value if the first files path
    099         * is greater than the second file.
    100         *
    101         */
    102        public int compare(File file1, File file2) {
    103            return caseSensitivity.checkCompareTo(file1.getPath(), file2.getPath());
    104        }
    105
    106        /**
    107         * String representation of this file comparator.
    108         *
    109         * @return String representation of this file comparator
    110         */
    111        @Override
    112        public String toString() {
    113            return super.toString() + "[caseSensitivity=" + caseSensitivity + "]";
    114        }
    115    }