| 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package org.apache.commons.io.comparator; |
| 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.Serializable; |
| 21 | import java.util.Comparator; |
| 22 | |
| 23 | /** |
| 24 | * Compare two files using the {@link File#isDirectory()} method. |
| 25 | * <p> |
| 26 | * This comparator can be used to sort lists or arrays by directories and files. |
| 27 | * </p> |
| 28 | * <p> |
| 29 | * Example of sorting a list of files/directories using the {@link #DIRECTORY_COMPARATOR} singleton instance: |
| 30 | * </p> |
| 31 | * |
| 32 | * <pre> |
| 33 | * List<File> list = ... |
| 34 | * ((AbstractFileComparator) DirectoryFileComparator.DIRECTORY_COMPARATOR).sort(list); |
| 35 | * </pre> |
| 36 | * <p> |
| 37 | * Example of doing a <em>reverse</em> sort of an array of files/directories using the {@link #DIRECTORY_REVERSE} |
| 38 | * singleton instance: |
| 39 | * </p> |
| 40 | * |
| 41 | * <pre> |
| 42 | * File[] array = ... |
| 43 | * ((AbstractFileComparator) DirectoryFileComparator.DIRECTORY_REVERSE).sort(array); |
| 44 | * </pre> |
| 45 | * <h2>Deprecating Serialization</h2> |
| 46 | * <p> |
| 47 | * <em>Serialization is deprecated and will be removed in 3.0.</em> |
| 48 | * </p> |
| 49 | * |
| 50 | * @since 2.0 |
| 51 | */ |
| 52 | public class DirectoryFileComparator extends AbstractFileComparator implements Serializable { |
| 53 | |
| 54 | private static final int TYPE_FILE = 2; |
| 55 | |
| 56 | private static final int TYPE_DIRECTORY = 1; |
| 57 | |
| 58 | private static final long serialVersionUID = 296132640160964395L; |
| 59 | |
| 60 | /** Singleton default comparator instance */ |
| 61 | public static final Comparator<File> DIRECTORY_COMPARATOR = new DirectoryFileComparator(); |
| 62 | |
| 63 | /** Singleton reverse default comparator instance */ |
| 64 | public static final Comparator<File> DIRECTORY_REVERSE = new ReverseFileComparator(DIRECTORY_COMPARATOR); |
| 65 | |
| 66 | /** |
| 67 | * Construct a new instance. |
| 68 | */ |
| 69 | public DirectoryFileComparator() { |
| 70 | // empty |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Compares the two files using the {@link File#isDirectory()} method. |
| 75 | * |
| 76 | * @param file1 The first file to compare. |
| 77 | * @param file2 The second file to compare. |
| 78 | * @return the result of calling file1's {@link File#compareTo(File)} with file2 as the parameter. |
| 79 | */ |
| 80 | @Override |
| 81 | public int compare(final File file1, final File file2) { |
| 82 | return getType(file1) - getType(file2); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Converts type to numeric value. |
| 87 | * |
| 88 | * @param file The file. |
| 89 | * @return 1 for directories and 2 for files. |
| 90 | */ |
| 91 | private int getType(final File file) { |
| 92 | return file.isDirectory() ? TYPE_DIRECTORY : TYPE_FILE; |
| 93 | } |
| 94 | } |