Coverage Report - org.apache.commons.io.comparator.DirectoryFileComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
DirectoryFileComparator
100%
7/7
100%
2/2
2
 
 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  
 /**
 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  
  * Example of sorting a list of files/directories using the
 29  
  * {@link #DIRECTORY_COMPARATOR} singleton instance:
 30  
  * <pre>
 31  
  *       List&lt;File&gt; list = ...
 32  
  *       ((AbstractFileComparator) DirectoryFileComparator.DIRECTORY_COMPARATOR).sort(list);
 33  
  * </pre>
 34  
  * <p>
 35  
  * Example of doing a <i>reverse</i> sort of an array of files/directories using the
 36  
  * {@link #DIRECTORY_REVERSE} singleton instance:
 37  
  * <pre>
 38  
  *       File[] array = ...
 39  
  *       ((AbstractFileComparator) DirectoryFileComparator.DIRECTORY_REVERSE).sort(array);
 40  
  * </pre>
 41  
  * <p>
 42  
  *
 43  
  * @version $Id: DirectoryFileComparator.java 1469107 2013-04-17 23:45:53Z sebb $
 44  
  * @since 2.0
 45  
  */
 46  22
 public class DirectoryFileComparator extends AbstractFileComparator implements Serializable {
 47  
 
 48  
     /** Singleton default comparator instance */
 49  2
     public static final Comparator<File> DIRECTORY_COMPARATOR = new DirectoryFileComparator();
 50  
 
 51  
     /** Singleton reverse default comparator instance */
 52  2
     public static final Comparator<File> DIRECTORY_REVERSE = new ReverseComparator(DIRECTORY_COMPARATOR);
 53  
 
 54  
     /**
 55  
      * Compare the two files using the {@link File#isDirectory()} method.
 56  
      * 
 57  
      * @param file1 The first file to compare
 58  
      * @param file2 The second file to compare
 59  
      * @return the result of calling file1's
 60  
      * {@link File#compareTo(File)} with file2 as the parameter.
 61  
      */
 62  
     public int compare(final File file1, final File file2) {
 63  12
         return getType(file1) - getType(file2);
 64  
     }
 65  
 
 66  
     /**
 67  
      * Convert type to numeric value.
 68  
      *
 69  
      * @param file The file
 70  
      * @return 1 for directories and 2 for files
 71  
      */
 72  
     private int getType(final File file) {
 73  24
         if (file.isDirectory()) {
 74  16
             return 1;
 75  
         } else {
 76  8
             return 2;
 77  
         }
 78  
     }
 79  
 }