| 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 | import org.apache.commons.io.FilenameUtils; |
| 24 | import org.apache.commons.io.IOCase; |
| 25 | |
| 26 | /** |
| 27 | * Compare the file name <strong>extensions</strong> for order |
| 28 | * (see {@link FilenameUtils#getExtension(String)}). |
| 29 | * <p> |
| 30 | * This comparator can be used to sort lists or arrays of files |
| 31 | * by their file extension either in a case-sensitive, case-insensitive or |
| 32 | * system dependent case-sensitive way. A number of singleton instances |
| 33 | * are provided for the various case sensitivity options (using {@link IOCase}) |
| 34 | * and the reverse of those options. |
| 35 | * </p> |
| 36 | * <p> |
| 37 | * Example of a <em>case-sensitive</em> file extension sort using the |
| 38 | * {@link #EXTENSION_COMPARATOR} singleton instance: |
| 39 | * </p> |
| 40 | * <pre> |
| 41 | * List<File> list = ... |
| 42 | * ((AbstractFileComparator) ExtensionFileComparator.EXTENSION_COMPARATOR).sort(list); |
| 43 | * </pre> |
| 44 | * <p> |
| 45 | * Example of a <em>reverse case-insensitive</em> file extension sort using the |
| 46 | * {@link #EXTENSION_INSENSITIVE_REVERSE} singleton instance: |
| 47 | * </p> |
| 48 | * <pre> |
| 49 | * File[] array = ... |
| 50 | * ((AbstractFileComparator) ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE).sort(array); |
| 51 | * </pre> |
| 52 | * <h2>Deprecating Serialization</h2> |
| 53 | * <p> |
| 54 | * <em>Serialization is deprecated and will be removed in 3.0.</em> |
| 55 | * </p> |
| 56 | * |
| 57 | * @since 1.4 |
| 58 | */ |
| 59 | public class ExtensionFileComparator extends AbstractFileComparator implements Serializable { |
| 60 | |
| 61 | private static final long serialVersionUID = 1928235200184222815L; |
| 62 | |
| 63 | /** Case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */ |
| 64 | public static final Comparator<File> EXTENSION_COMPARATOR = new ExtensionFileComparator(); |
| 65 | |
| 66 | /** Reverse case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */ |
| 67 | public static final Comparator<File> EXTENSION_REVERSE = new ReverseFileComparator(EXTENSION_COMPARATOR); |
| 68 | |
| 69 | /** Case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */ |
| 70 | public static final Comparator<File> EXTENSION_INSENSITIVE_COMPARATOR |
| 71 | = new ExtensionFileComparator(IOCase.INSENSITIVE); |
| 72 | |
| 73 | /** Reverse case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */ |
| 74 | public static final Comparator<File> EXTENSION_INSENSITIVE_REVERSE |
| 75 | = new ReverseFileComparator(EXTENSION_INSENSITIVE_COMPARATOR); |
| 76 | |
| 77 | /** System sensitive extension comparator instance (see {@link IOCase#SYSTEM}) */ |
| 78 | public static final Comparator<File> EXTENSION_SYSTEM_COMPARATOR = new ExtensionFileComparator(IOCase.SYSTEM); |
| 79 | |
| 80 | /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */ |
| 81 | public static final Comparator<File> EXTENSION_SYSTEM_REVERSE = new ReverseFileComparator(EXTENSION_SYSTEM_COMPARATOR); |
| 82 | |
| 83 | /** Whether the comparison is case-sensitive. */ |
| 84 | private final IOCase ioCase; |
| 85 | |
| 86 | /** |
| 87 | * Constructs a case-sensitive file extension comparator instance. |
| 88 | */ |
| 89 | public ExtensionFileComparator() { |
| 90 | this.ioCase = IOCase.SENSITIVE; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Constructs a file extension comparator instance with the specified case-sensitivity. |
| 95 | * |
| 96 | * @param ioCase how to handle case sensitivity, null means case-sensitive |
| 97 | */ |
| 98 | public ExtensionFileComparator(final IOCase ioCase) { |
| 99 | this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Compares the extensions of two files the specified case sensitivity. |
| 104 | * |
| 105 | * @param file1 The first file to compare |
| 106 | * @param file2 The second file to compare |
| 107 | * @return a negative value if the first file's extension |
| 108 | * is less than the second, zero if the extensions are the |
| 109 | * same and a positive value if the first files extension |
| 110 | * is greater than the second file. |
| 111 | */ |
| 112 | @Override |
| 113 | public int compare(final File file1, final File file2) { |
| 114 | final String suffix1 = FilenameUtils.getExtension(file1.getName()); |
| 115 | final String suffix2 = FilenameUtils.getExtension(file2.getName()); |
| 116 | return ioCase.checkCompareTo(suffix1, suffix2); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * String representation of this file comparator. |
| 121 | * |
| 122 | * @return String representation of this file comparator |
| 123 | */ |
| 124 | @Override |
| 125 | public String toString() { |
| 126 | return super.toString() + "[ioCase=" + ioCase + "]"; |
| 127 | } |
| 128 | } |