CompositeFileComparator.java

  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. import java.io.File;
  19. import java.io.Serializable;
  20. import java.util.Comparator;
  21. import java.util.function.IntFunction;
  22. import java.util.stream.Stream;
  23. import java.util.stream.StreamSupport;

  24. /**
  25.  * Compare two files using a set of delegate file {@link Comparator}.
  26.  * <p>
  27.  * This comparator can be used to sort lists or arrays of files by combining a number of other comparators.
  28.  * <p>
  29.  * Example of sorting a list of files by type (directory or file) and then by name:
  30.  *
  31.  * <pre>
  32.  *       CompositeFileComparator comparator = new CompositeFileComparator(
  33.  *           DirectoryFileComparator.DIRECTORY_COMPARATOR,
  34.  *           NameFileComparator.NAME_COMPARATOR);
  35.  *       List&lt;File&gt; list = ...
  36.  *       comparator.sort(list);
  37.  * </pre>
  38.  * <h2>Deprecating Serialization</h2>
  39.  * <p>
  40.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  41.  * </p>
  42.  *
  43.  * @since 2.0
  44.  */
  45. public class CompositeFileComparator extends AbstractFileComparator implements Serializable {

  46.     private static final Comparator<?>[] EMPTY_COMPARATOR_ARRAY = {};
  47.     private static final long serialVersionUID = -2224170307287243428L;

  48.     /**
  49.      * Delegates.
  50.      */
  51.     private final Comparator<File>[] delegates;

  52.     /**
  53.      * Constructs a composite comparator for the set of delegate comparators.
  54.      *
  55.      * @param delegates The delegate file comparators
  56.      */
  57.     public CompositeFileComparator(@SuppressWarnings("unchecked") final Comparator<File>... delegates) {
  58.         this.delegates = delegates == null ? emptyArray() : delegates.clone();
  59.     }

  60.     /**
  61.      * Constructs a composite comparator for the set of delegate comparators.
  62.      *
  63.      * @param delegates The delegate file comparators
  64.      */
  65.     public CompositeFileComparator(final Iterable<Comparator<File>> delegates) {
  66.         this.delegates = delegates == null ? emptyArray()
  67.                 : StreamSupport.stream(delegates.spliterator(), false).toArray((IntFunction<Comparator<File>[]>) Comparator[]::new);
  68.     }

  69.     /**
  70.      * Compares the two files using delegate comparators.
  71.      *
  72.      * @param file1 The first file to compare
  73.      * @param file2 The second file to compare
  74.      * @return the first non-zero result returned from the delegate comparators or zero.
  75.      */
  76.     @Override
  77.     public int compare(final File file1, final File file2) {
  78.         return Stream.of(delegates).map(delegate -> delegate.compare(file1, file2)).filter(r -> r != 0).findFirst().orElse(0);
  79.     }

  80.     @SuppressWarnings("unchecked") // types are already correct
  81.     private Comparator<File>[] emptyArray() {
  82.         return (Comparator<File>[]) EMPTY_COMPARATOR_ARRAY;
  83.     }

  84.     /**
  85.      * String representation of this file comparator.
  86.      *
  87.      * @return String representation of this file comparator
  88.      */
  89.     @Override
  90.     public String toString() {
  91.         final StringBuilder builder = new StringBuilder();
  92.         builder.append(super.toString());
  93.         builder.append('{');
  94.         for (int i = 0; i < delegates.length; i++) {
  95.             if (i > 0) {
  96.                 builder.append(',');
  97.             }
  98.             builder.append(delegates[i]);
  99.         }
  100.         builder.append('}');
  101.         return builder.toString();
  102.     }
  103. }