Coverage Report - org.apache.commons.io.comparator.NameFileComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
NameFileComparator
100%
15/15
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.IOCase;
 24  
 
 25  
 /**
 26  
  * Compare the <b>names</b> of two files for order (see {@link File#getName()}).
 27  
  * <p>
 28  
  * This comparator can be used to sort lists or arrays of files
 29  
  * by their name either in a case-sensitive, case-insensitive or
 30  
  * system dependent case sensitive way. A number of singleton instances
 31  
  * are provided for the various case sensitivity options (using {@link IOCase})
 32  
  * and the reverse of those options.
 33  
  * <p>
 34  
  * Example of a <i>case-sensitive</i> file name sort using the
 35  
  * {@link #NAME_COMPARATOR} singleton instance:
 36  
  * <pre>
 37  
  *       List&lt;File&gt; list = ...
 38  
  *       ((AbstractFileComparator) NameFileComparator.NAME_COMPARATOR).sort(list);
 39  
  * </pre>
 40  
  * <p>
 41  
  * Example of a <i>reverse case-insensitive</i> file name sort using the
 42  
  * {@link #NAME_INSENSITIVE_REVERSE} singleton instance:
 43  
  * <pre>
 44  
  *       File[] array = ...
 45  
  *       ((AbstractFileComparator) NameFileComparator.NAME_INSENSITIVE_REVERSE).sort(array);
 46  
  * </pre>
 47  
  * <p>
 48  
  *
 49  
  * @version $Id: NameFileComparator.java 1469107 2013-04-17 23:45:53Z sebb $
 50  
  * @since 1.4
 51  
  */
 52  462
 public class NameFileComparator extends AbstractFileComparator implements Serializable {
 53  
 
 54  
     /** Case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */
 55  6
     public static final Comparator<File> NAME_COMPARATOR = new NameFileComparator();
 56  
 
 57  
     /** Reverse case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */
 58  6
     public static final Comparator<File> NAME_REVERSE = new ReverseComparator(NAME_COMPARATOR);
 59  
 
 60  
     /** Case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */
 61  6
     public static final Comparator<File> NAME_INSENSITIVE_COMPARATOR = new NameFileComparator(IOCase.INSENSITIVE);
 62  
 
 63  
     /** Reverse case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */
 64  6
     public static final Comparator<File> NAME_INSENSITIVE_REVERSE = new ReverseComparator(NAME_INSENSITIVE_COMPARATOR);
 65  
 
 66  
     /** System sensitive name comparator instance (see {@link IOCase#SYSTEM}) */
 67  6
     public static final Comparator<File> NAME_SYSTEM_COMPARATOR = new NameFileComparator(IOCase.SYSTEM);
 68  
 
 69  
     /** Reverse system sensitive name comparator instance (see {@link IOCase#SYSTEM}) */
 70  6
     public static final Comparator<File> NAME_SYSTEM_REVERSE = new ReverseComparator(NAME_SYSTEM_COMPARATOR);
 71  
 
 72  
     /** Whether the comparison is case sensitive. */
 73  
     private final IOCase caseSensitivity;
 74  
 
 75  
     /**
 76  
      * Construct a case sensitive file name comparator instance.
 77  
      */
 78  6
     public NameFileComparator() {
 79  6
         this.caseSensitivity = IOCase.SENSITIVE;
 80  6
     }
 81  
 
 82  
     /**
 83  
      * Construct a file name comparator instance with the specified case-sensitivity.
 84  
      *
 85  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 86  
      */
 87  14
     public NameFileComparator(final IOCase caseSensitivity) {
 88  14
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 89  14
     }
 90  
 
 91  
     /**
 92  
      * Compare the names of two files with the specified case sensitivity.
 93  
      * 
 94  
      * @param file1 The first file to compare
 95  
      * @param file2 The second file to compare
 96  
      * @return a negative value if the first file's name
 97  
      * is less than the second, zero if the names are the
 98  
      * same and a positive value if the first files name
 99  
      * is greater than the second file.
 100  
      */
 101  
     public int compare(final File file1, final File file2) {
 102  454
         return caseSensitivity.checkCompareTo(file1.getName(), file2.getName());
 103  
     }
 104  
 
 105  
     /**
 106  
      * String representation of this file comparator.
 107  
      *
 108  
      * @return String representation of this file comparator
 109  
      */
 110  
     @Override
 111  
     public String toString() {
 112  4
         return super.toString() + "[caseSensitivity=" + caseSensitivity + "]";
 113  
     }
 114  
 }