    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.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 dependent 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 * <p>
    035 * Example of a <i>case-sensitive</i> file path sort using the
    036 * {@link #PATH_COMPARATOR} singleton instance:
    037 * </p>
    038 * <pre>
    039 *       List&lt;File&gt; list = ...
    040 *       ((AbstractFileComparator) PathFileComparator.PATH_COMPARATOR).sort(list);
    041 * </pre>
    042 * <p>
    043 * Example of a <i>reverse case-insensitive</i> file path sort using the
    044 * {@link #PATH_INSENSITIVE_REVERSE} singleton instance:
    045 * </p>
    046 * <pre>
    047 *       File[] array = ...
    048 *       ((AbstractFileComparator) PathFileComparator.PATH_INSENSITIVE_REVERSE).sort(array);
    049 * </pre>
    050 * <h2>Deprecating Serialization</h2>
    051 * <p>
    052 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    053 * </p>
    054 * @since 1.4
    055 */
    056public class PathFileComparator extends AbstractFileComparator implements Serializable {
    057
    058    private static final long serialVersionUID = 6527501707585768673L;
    059
    060    /** Case-sensitive path comparator instance (see {@link IOCase#SENSITIVE}) */
    061    public static final Comparator<File> PATH_COMPARATOR = new PathFileComparator();
    062
    063    /** Reverse case-sensitive path comparator instance (see {@link IOCase#SENSITIVE}) */
    064    public static final Comparator<File> PATH_REVERSE = new ReverseFileComparator(PATH_COMPARATOR);
    065
    066    /** Case-insensitive path comparator instance (see {@link IOCase#INSENSITIVE}) */
    067    public static final Comparator<File> PATH_INSENSITIVE_COMPARATOR = new PathFileComparator(IOCase.INSENSITIVE);
    068
    069    /** Reverse case-insensitive path comparator instance (see {@link IOCase#INSENSITIVE}) */
    070    public static final Comparator<File> PATH_INSENSITIVE_REVERSE = new ReverseFileComparator(PATH_INSENSITIVE_COMPARATOR);
    071
    072    /** System sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
    073    public static final Comparator<File> PATH_SYSTEM_COMPARATOR = new PathFileComparator(IOCase.SYSTEM);
    074
    075    /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
    076    public static final Comparator<File> PATH_SYSTEM_REVERSE = new ReverseFileComparator(PATH_SYSTEM_COMPARATOR);
    077
    078    /** Whether the comparison is case-sensitive. */
    079    private final IOCase ioCase;
    080
    081    /**
    082     * Constructs a case-sensitive file path comparator instance.
    083     */
    084    public PathFileComparator() {
    085        this.ioCase = IOCase.SENSITIVE;
    086    }
    087
    088    /**
    089     * Constructs a file path comparator instance with the specified case-sensitivity.
    090     *
    091     * @param ioCase  how to handle case sensitivity, null means case-sensitive
    092     */
    093    public PathFileComparator(final IOCase ioCase) {
    094        this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE);
    095    }
    096
    097    /**
    098     * Compares the paths 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 path
    103     * is less than the second, zero if the paths are the
    104     * same and a positive value if the first files path
    105     * is greater than the second file.
    106     */
    107    @Override
    108    public int compare(final File file1, final File file2) {
    109        return ioCase.checkCompareTo(file1.getPath(), file2.getPath());
    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() + "[ioCase=" + ioCase + "]";
    120    }
    121}