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 * http://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.ArrayList; 022import java.util.Comparator; 023import java.util.List; 024 025/** 026 * Compare two files using a set of delegate file {@link Comparator}. 027 * <p> 028 * This comparator can be used to sort lists or arrays of files 029 * by combining a number other comparators. 030 * <p> 031 * Example of sorting a list of files by type (i.e. directory or file) 032 * and then by name: 033 * <pre> 034 * CompositeFileComparator comparator = 035 * new CompositeFileComparator( 036 * (AbstractFileComparator) DirectoryFileComparator.DIRECTORY_COMPARATOR, 037 * (AbstractFileComparator) NameFileComparator.NAME_COMPARATOR); 038 * List<File> list = ... 039 * comparator.sort(list); 040 * </pre> 041 * 042 * @since 2.0 043 */ 044public class CompositeFileComparator extends AbstractFileComparator implements Serializable { 045 046 private static final Comparator<?>[] EMPTY_COMPARATOR_ARRAY = new Comparator<?>[0]; 047 private static final long serialVersionUID = -2224170307287243428L; 048 private static final Comparator<?>[] NO_COMPARATORS = {}; 049 private final Comparator<File>[] delegates; 050 051 /** 052 * Create a composite comparator for the set of delegate comparators. 053 * 054 * @param delegates The delegate file comparators 055 */ 056 @SuppressWarnings("unchecked") // casts 1 & 2 must be OK because types are already correct 057 public CompositeFileComparator(final Comparator<File>... delegates) { 058 if (delegates == null) { 059 this.delegates = (Comparator<File>[]) NO_COMPARATORS;//1 060 } else { 061 this.delegates = (Comparator<File>[]) new Comparator<?>[delegates.length];//2 062 System.arraycopy(delegates, 0, this.delegates, 0, delegates.length); 063 } 064 } 065 066 /** 067 * Create a composite comparator for the set of delegate comparators. 068 * 069 * @param delegates The delegate file comparators 070 */ 071 @SuppressWarnings("unchecked") // casts 1 & 2 must be OK because types are already correct 072 public CompositeFileComparator(final Iterable<Comparator<File>> delegates) { 073 if (delegates == null) { 074 this.delegates = (Comparator<File>[]) NO_COMPARATORS; //1 075 } else { 076 final List<Comparator<File>> list = new ArrayList<>(); 077 for (final Comparator<File> comparator : delegates) { 078 list.add(comparator); 079 } 080 this.delegates = (Comparator<File>[]) list.toArray(EMPTY_COMPARATOR_ARRAY); //2 081 } 082 } 083 084 /** 085 * Compare the two files using delegate comparators. 086 * 087 * @param file1 The first file to compare 088 * @param file2 The second file to compare 089 * @return the first non-zero result returned from 090 * the delegate comparators or zero. 091 */ 092 @Override 093 public int compare(final File file1, final File file2) { 094 int result = 0; 095 for (final Comparator<File> delegate : delegates) { 096 result = delegate.compare(file1, file2); 097 if (result != 0) { 098 break; 099 } 100 } 101 return result; 102 } 103 104 /** 105 * String representation of this file comparator. 106 * 107 * @return String representation of this file comparator 108 */ 109 @Override 110 public String toString() { 111 final StringBuilder builder = new StringBuilder(); 112 builder.append(super.toString()); 113 builder.append('{'); 114 for (int i = 0; i < delegates.length; i++) { 115 if (i > 0) { 116 builder.append(','); 117 } 118 builder.append(delegates[i]); 119 } 120 builder.append('}'); 121 return builder.toString(); 122 } 123}