    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.io.comparator;
    018
    019import java.io.File;
    020import java.io.Serializable;
    021import java.util.Arrays;
    022import java.util.Comparator;
    023import java.util.function.IntFunction;
    024import java.util.stream.Stream;
    025import java.util.stream.StreamSupport;
    026
    027/**
    028 * Compare two files using a set of delegate file {@link Comparator}.
    029 * <p>
    030 * This comparator can be used to sort lists or arrays of files by combining a number of other comparators.
    031 * <p>
    032 * Example of sorting a list of files by type (directory or file) and then by name:
    033 *
    034 * <pre>
    035 *       CompositeFileComparator comparator = new CompositeFileComparator(
    036 *           DirectoryFileComparator.DIRECTORY_COMPARATOR,
    037 *           NameFileComparator.NAME_COMPARATOR);
    038 *       List&lt;File&gt; list = ...
    039 *       comparator.sort(list);
    040 * </pre>
    041 * <h2>Deprecating Serialization</h2>
    042 * <p>
    043 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    044 * </p>
    045 *
    046 * @since 2.0
    047 */
    048public class CompositeFileComparator extends AbstractFileComparator implements Serializable {
    049
    050    private static final Comparator<?>[] EMPTY_COMPARATOR_ARRAY = {};
    051    private static final long serialVersionUID = -2224170307287243428L;
    052
    053    /**
    054     * Delegates.
    055     */
    056    private final Comparator<File>[] delegates;
    057
    058    /**
    059     * Constructs a composite comparator for the set of delegate comparators.
    060     *
    061     * @param delegates The delegate file comparators
    062     */
    063    public CompositeFileComparator(@SuppressWarnings("unchecked") final Comparator<File>... delegates) {
    064        this.delegates = delegates == null ? emptyArray() : delegates.clone();
    065    }
    066
    067    /**
    068     * Constructs a composite comparator for the set of delegate comparators.
    069     *
    070     * @param delegates The delegate file comparators
    071     */
    072    public CompositeFileComparator(final Iterable<Comparator<File>> delegates) {
    073        this.delegates = delegates == null ? emptyArray()
    074                : StreamSupport.stream(delegates.spliterator(), false).toArray((IntFunction<Comparator<File>[]>) Comparator[]::new);
    075    }
    076
    077    /**
    078     * Compares the two files using delegate comparators.
    079     *
    080     * @param file1 The first file to compare
    081     * @param file2 The second file to compare
    082     * @return the first non-zero result returned from the delegate comparators or zero.
    083     */
    084    @Override
    085    public int compare(final File file1, final File file2) {
    086        return Stream.of(delegates).map(delegate -> delegate.compare(file1, file2)).filter(r -> r != 0).findFirst().orElse(0);
    087    }
    088
    089    @SuppressWarnings("unchecked") // types are already correct
    090    private Comparator<File>[] emptyArray() {
    091        return (Comparator<File>[]) EMPTY_COMPARATOR_ARRAY;
    092    }
    093
    094    /**
    095     * String representation of this file comparator.
    096     *
    097     * @return String representation of this file comparator
    098     */
    099    @Override
    100    public String toString() {
    101        final StringBuilder builder = new StringBuilder(super.toString());
    102        builder.append(Arrays.toString(delegates));
    103        return builder.toString();
    104    }
    105}