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