    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
    023/**
    024 * Compare two files using the {@link File#isDirectory()} method.
    025 * <p>
    026 * This comparator can be used to sort lists or arrays by directories and files.
    027 * <p>
    028 * Example of sorting a list of files/directories using the
    029 * {@link #DIRECTORY_COMPARATOR} singleton instance:
    030 * <pre>
    031 *       List&lt;File&gt; list = ...
    032 *       ((AbstractFileComparator) DirectoryFileComparator.DIRECTORY_COMPARATOR).sort(list);
    033 * </pre>
    034 * <p>
    035 * Example of doing a <i>reverse</i> sort of an array of files/directories using the
    036 * {@link #DIRECTORY_REVERSE} singleton instance:
    037 * <pre>
    038 *       File[] array = ...
    039 *       ((AbstractFileComparator) DirectoryFileComparator.DIRECTORY_REVERSE).sort(array);
    040 * </pre>
    041 * <p>
    042 *
    043 * @since 2.0
    044 */
    045public class DirectoryFileComparator extends AbstractFileComparator implements Serializable {
    046
    047    private static final int TYPE_FILE = 2;
    048
    049    private static final int TYPE_DIRECTORY = 1;
    050
    051    private static final long serialVersionUID = 296132640160964395L;
    052
    053    /** Singleton default comparator instance */
    054    public static final Comparator<File> DIRECTORY_COMPARATOR = new DirectoryFileComparator();
    055
    056    /** Singleton reverse default comparator instance */
    057    public static final Comparator<File> DIRECTORY_REVERSE = new ReverseFileComparator(DIRECTORY_COMPARATOR);
    058
    059    /**
    060     * Compare the two files using the {@link File#isDirectory()} method.
    061     *
    062     * @param file1 The first file to compare
    063     * @param file2 The second file to compare
    064     * @return the result of calling file1's
    065     * {@link File#compareTo(File)} with file2 as the parameter.
    066     */
    067    @Override
    068    public int compare(final File file1, final File file2) {
    069        return getType(file1) - getType(file2);
    070    }
    071
    072    /**
    073     * Convert type to numeric value.
    074     *
    075     * @param file The file
    076     * @return 1 for directories and 2 for files
    077     */
    078    private int getType(final File file) {
    079        return file.isDirectory() ? TYPE_DIRECTORY : TYPE_FILE;
    080    }
    081}