Coverage Report - org.apache.commons.io.comparator.ExtensionFileComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
ExtensionFileComparator
100%
17/17
100%
2/2
1.25
 
 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  
  *      http://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 <b>extensions</b> 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  
  * Example of a <i>case-sensitive</i> file extension sort using the
 37  
  * {@link #EXTENSION_COMPARATOR} singleton instance:
 38  
  * <pre>
 39  
  *       List&lt;File&gt; list = ...
 40  
  *       ((AbstractFileComparator) ExtensionFileComparator.EXTENSION_COMPARATOR).sort(list);
 41  
  * </pre>
 42  
  * <p>
 43  
  * Example of a <i>reverse case-insensitive</i> file extension sort using the
 44  
  * {@link #EXTENSION_INSENSITIVE_REVERSE} singleton instance:
 45  
  * <pre>
 46  
  *       File[] array = ...
 47  
  *       ((AbstractFileComparator) ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE).sort(array);
 48  
  * </pre>
 49  
  * <p>
 50  
  *
 51  
  * @version $Id: ExtensionFileComparator.java 1469107 2013-04-17 23:45:53Z sebb $
 52  
  * @since 1.4
 53  
  */
 54  60
 public class ExtensionFileComparator extends AbstractFileComparator implements Serializable {
 55  
 
 56  
     /** Case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
 57  4
     public static final Comparator<File> EXTENSION_COMPARATOR = new ExtensionFileComparator();
 58  
 
 59  
     /** Reverse case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
 60  4
     public static final Comparator<File> EXTENSION_REVERSE = new ReverseComparator(EXTENSION_COMPARATOR);
 61  
 
 62  
     /** Case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
 63  4
     public static final Comparator<File> EXTENSION_INSENSITIVE_COMPARATOR
 64  
                                                 = new ExtensionFileComparator(IOCase.INSENSITIVE);
 65  
 
 66  
     /** Reverse case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
 67  4
     public static final Comparator<File> EXTENSION_INSENSITIVE_REVERSE
 68  
                                                 = new ReverseComparator(EXTENSION_INSENSITIVE_COMPARATOR);
 69  
 
 70  
     /** System sensitive extension comparator instance (see {@link IOCase#SYSTEM}) */
 71  4
     public static final Comparator<File> EXTENSION_SYSTEM_COMPARATOR = new ExtensionFileComparator(IOCase.SYSTEM);
 72  
 
 73  
     /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
 74  4
     public static final Comparator<File> EXTENSION_SYSTEM_REVERSE = new ReverseComparator(EXTENSION_SYSTEM_COMPARATOR);
 75  
 
 76  
     /** Whether the comparison is case sensitive. */
 77  
     private final IOCase caseSensitivity;
 78  
 
 79  
     /**
 80  
      * Construct a case sensitive file extension comparator instance.
 81  
      */
 82  4
     public ExtensionFileComparator() {
 83  4
         this.caseSensitivity = IOCase.SENSITIVE;
 84  4
     }
 85  
 
 86  
     /**
 87  
      * Construct a file extension comparator instance with the specified case-sensitivity.
 88  
      *
 89  
      * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
 90  
      */
 91  10
     public ExtensionFileComparator(final IOCase caseSensitivity) {
 92  10
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 93  10
     }
 94  
 
 95  
     /**
 96  
      * Compare the extensions of two files the specified case sensitivity.
 97  
      * 
 98  
      * @param file1 The first file to compare
 99  
      * @param file2 The second file to compare
 100  
      * @return a negative value if the first file's extension
 101  
      * is less than the second, zero if the extensions are the
 102  
      * same and a positive value if the first files extension
 103  
      * is greater than the second file.
 104  
      * 
 105  
      */
 106  
     public int compare(final File file1, final File file2) {
 107  52
         final String suffix1 = FilenameUtils.getExtension(file1.getName());
 108  52
         final String suffix2 = FilenameUtils.getExtension(file2.getName());
 109  52
         return caseSensitivity.checkCompareTo(suffix1, suffix2);
 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  8
         return super.toString() + "[caseSensitivity=" + caseSensitivity + "]";
 120  
     }
 121  
 }