Coverage Report - org.apache.commons.io.comparator.CompositeFileComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
CompositeFileComparator
100%
32/32
100%
14/14
2.75
 
 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.ArrayList;
 22  
 import java.util.Comparator;
 23  
 import java.util.List;
 24  
 
 25  
 /**
 26  
  * Compare two files using a set of delegate file {@link Comparator}.
 27  
  * <p>
 28  
  * This comparator can be used to sort lists or arrays of files
 29  
  * by combining a number other comparators.
 30  
  * <p>
 31  
  * Example of sorting a list of files by type (i.e. directory or file)
 32  
  * and then by name:
 33  
  * <pre>
 34  
  *       CompositeFileComparator comparator =
 35  
  *                       new CompositeFileComparator(
 36  
  *                                 (AbstractFileComparator) DirectoryFileComparator.DIRECTORY_COMPARATOR,
 37  
  *                                 (AbstractFileComparator) NameFileComparator.NAME_COMPARATOR);
 38  
  *       List&lt;File&gt; list = ...
 39  
  *       comparator.sort(list);
 40  
  * </pre>
 41  
  *
 42  
  * @version $Id: CompositeFileComparator.java 1469107 2013-04-17 23:45:53Z sebb $
 43  
  * @since 2.0
 44  
  */
 45  50
 public class CompositeFileComparator extends AbstractFileComparator implements Serializable {
 46  
 
 47  2
     private static final Comparator<?>[] NO_COMPARATORS = {};
 48  
     private final Comparator<File>[] delegates;
 49  
 
 50  
     /**
 51  
      * Create a composite comparator for the set of delegate comparators.
 52  
      *
 53  
      * @param delegates The delegate file comparators
 54  
      */
 55  
     @SuppressWarnings("unchecked") // casts 1 & 2 must be OK because types are already correct
 56  22
     public CompositeFileComparator(final Comparator<File>... delegates) {
 57  22
         if (delegates == null) {
 58  2
             this.delegates = (Comparator<File>[]) NO_COMPARATORS;//1
 59  
         } else {
 60  20
             this.delegates = (Comparator<File>[]) new Comparator<?>[delegates.length];//2
 61  20
             System.arraycopy(delegates, 0, this.delegates, 0, delegates.length);
 62  
         }
 63  22
     }
 64  
 
 65  
     /**
 66  
      * Create a composite comparator for the set of delegate comparators.
 67  
      *
 68  
      * @param delegates The delegate file comparators
 69  
      */
 70  
     @SuppressWarnings("unchecked") // casts 1 & 2 must be OK because types are already correct
 71  4
     public CompositeFileComparator(final Iterable<Comparator<File>> delegates) {
 72  4
         if (delegates == null) {
 73  2
             this.delegates = (Comparator<File>[]) NO_COMPARATORS; //1
 74  
         } else {
 75  2
             final List<Comparator<File>> list = new ArrayList<Comparator<File>>();
 76  2
             for (final Comparator<File> comparator : delegates) {
 77  4
                 list.add(comparator);
 78  4
             }
 79  2
             this.delegates = (Comparator<File>[]) list.toArray(new Comparator<?>[list.size()]); //2
 80  
         }
 81  4
     }
 82  
 
 83  
     /**
 84  
      * Compare the two files using delegate comparators.
 85  
      * 
 86  
      * @param file1 The first file to compare
 87  
      * @param file2 The second file to compare
 88  
      * @return the first non-zero result returned from
 89  
      * the delegate comparators or zero.
 90  
      */
 91  
     public int compare(final File file1, final File file2) {
 92  42
         int result = 0;
 93  58
         for (final Comparator<File> delegate : delegates) {
 94  44
             result = delegate.compare(file1, file2);
 95  44
             if (result != 0) {
 96  28
                 break;
 97  
             }
 98  
         }
 99  42
         return result;
 100  
     }
 101  
 
 102  
     /**
 103  
      * String representation of this file comparator.
 104  
      *
 105  
      * @return String representation of this file comparator
 106  
      */
 107  
     @Override
 108  
     public String toString() {
 109  8
         final StringBuilder builder = new StringBuilder();
 110  8
         builder.append(super.toString());
 111  8
         builder.append('{');
 112  16
         for (int i = 0; i < delegates.length; i++) {
 113  8
             if (i > 0) {
 114  4
                 builder.append(',');
 115  
             }
 116  8
             builder.append(delegates[i]);
 117  
         }
 118  8
         builder.append('}');
 119  8
         return builder.toString();
 120  
     }
 121  
 }