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.filefilter; 018 019import java.io.File; 020import java.io.Serializable; 021import java.nio.file.FileVisitResult; 022import java.nio.file.Path; 023import java.nio.file.attribute.BasicFileAttributes; 024import java.util.List; 025import java.util.Objects; 026import java.util.stream.Stream; 027 028import org.apache.commons.io.IOCase; 029import org.apache.commons.io.file.PathUtils; 030 031/** 032 * Filters files based on the suffix (what the file name ends with). 033 * This is used in retrieving all the files of a particular type. 034 * <p> 035 * For example, to retrieve and print all {@code *.java} files 036 * in the current directory: 037 * </p> 038 * <h2>Using Classic IO</h2> 039 * <pre> 040 * File dir = FileUtils.current(); 041 * String[] files = dir.list(new SuffixFileFilter(".java")); 042 * for (String file : files) { 043 * System.out.println(file); 044 * } 045 * </pre> 046 * 047 * <h2>Using NIO</h2> 048 * <pre> 049 * final Path dir = PathUtils.current(); 050 * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new SuffixFileFilter(".java")); 051 * // 052 * // Walk one directory 053 * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor); 054 * System.out.println(visitor.getPathCounters()); 055 * System.out.println(visitor.getFileList()); 056 * // 057 * visitor.getPathCounters().reset(); 058 * // 059 * // Walk directory tree 060 * Files.<strong>walkFileTree</strong>(dir, visitor); 061 * System.out.println(visitor.getPathCounters()); 062 * System.out.println(visitor.getDirList()); 063 * System.out.println(visitor.getFileList()); 064 * </pre> 065 * <h2>Deprecating Serialization</h2> 066 * <p> 067 * <em>Serialization is deprecated and will be removed in 3.0.</em> 068 * </p> 069 * 070 * @since 1.0 071 * @see FileFilterUtils#suffixFileFilter(String) 072 * @see FileFilterUtils#suffixFileFilter(String, IOCase) 073 */ 074public class SuffixFileFilter extends AbstractFileFilter implements Serializable { 075 076 private static final long serialVersionUID = -3389157631240246157L; 077 078 /** The file name suffixes to search for */ 079 private final String[] suffixes; 080 081 /** Whether the comparison is case-sensitive. */ 082 private final IOCase ioCase; 083 084 /** 085 * Constructs a new Suffix file filter for a list of suffixes. 086 * 087 * @param suffixes the suffixes to allow, must not be null 088 * @throws IllegalArgumentException if the suffix list is null 089 * @throws ClassCastException if the list does not contain Strings 090 */ 091 public SuffixFileFilter(final List<String> suffixes) { 092 this(suffixes, IOCase.SENSITIVE); 093 } 094 095 /** 096 * Constructs a new Suffix file filter for a list of suffixes 097 * specifying case-sensitivity. 098 * 099 * @param suffixes the suffixes to allow, must not be null 100 * @param ioCase how to handle case sensitivity, null means case-sensitive 101 * @throws IllegalArgumentException if the suffix list is null 102 * @throws ClassCastException if the list does not contain Strings 103 * @since 1.4 104 */ 105 public SuffixFileFilter(final List<String> suffixes, final IOCase ioCase) { 106 Objects.requireNonNull(suffixes, "suffixes"); 107 this.suffixes = suffixes.toArray(EMPTY_STRING_ARRAY); 108 this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE); 109 } 110 111 /** 112 * Constructs a new Suffix file filter for a single extension. 113 * 114 * @param suffix the suffix to allow, must not be null 115 * @throws IllegalArgumentException if the suffix is null 116 */ 117 public SuffixFileFilter(final String suffix) { 118 this(suffix, IOCase.SENSITIVE); 119 } 120 121 /** 122 * Constructs a new Suffix file filter for an array of suffixes. 123 * <p> 124 * The array is not cloned, so could be changed after constructing the 125 * instance. This would be inadvisable however. 126 * </p> 127 * 128 * @param suffixes the suffixes to allow, must not be null 129 * @throws NullPointerException if the suffix array is null 130 */ 131 public SuffixFileFilter(final String... suffixes) { 132 this(suffixes, IOCase.SENSITIVE); 133 } 134 135 /** 136 * Constructs a new Suffix file filter for a single extension 137 * specifying case-sensitivity. 138 * 139 * @param suffix the suffix to allow, must not be null 140 * @param ioCase how to handle case sensitivity, null means case-sensitive 141 * @throws NullPointerException if the suffix is null 142 * @since 1.4 143 */ 144 public SuffixFileFilter(final String suffix, final IOCase ioCase) { 145 Objects.requireNonNull(suffix, "suffix"); 146 this.suffixes = new String[] {suffix}; 147 this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE); 148 } 149 150 /** 151 * Constructs a new Suffix file filter for an array of suffixes 152 * specifying case-sensitivity. 153 * 154 * @param suffixes the suffixes to allow, must not be null 155 * @param ioCase how to handle case sensitivity, null means case-sensitive 156 * @throws NullPointerException if the suffix array is null 157 * @since 1.4 158 */ 159 public SuffixFileFilter(final String[] suffixes, final IOCase ioCase) { 160 Objects.requireNonNull(suffixes, "suffixes"); 161 this.suffixes = suffixes.clone(); 162 this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE); 163 } 164 165 /** 166 * Checks to see if the file name ends with the suffix. 167 * 168 * @param file the File to check 169 * @return true if the file name ends with one of our suffixes 170 */ 171 @Override 172 public boolean accept(final File file) { 173 return accept(file.getName()); 174 } 175 176 /** 177 * Checks to see if the file name ends with the suffix. 178 * 179 * @param file the File directory 180 * @param name the file name 181 * @return true if the file name ends with one of our suffixes 182 */ 183 @Override 184 public boolean accept(final File file, final String name) { 185 return accept(name); 186 } 187 188 /** 189 * Checks to see if the file name ends with the suffix. 190 * 191 * @param path the File to check 192 * @param attributes the path's basic attributes (may be null). 193 * @return true if the file name ends with one of our suffixes 194 * @since 2.9.0 195 */ 196 @Override 197 public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) { 198 return toFileVisitResult(accept(PathUtils.getFileNameString(path))); 199 } 200 201 private boolean accept(final String name) { 202 return Stream.of(suffixes).anyMatch(suffix -> ioCase.checkEndsWith(name, suffix)); 203 } 204 205 /** 206 * Provide a String representation of this file filter. 207 * 208 * @return a String representation 209 */ 210 @Override 211 public String toString() { 212 final StringBuilder buffer = new StringBuilder(); 213 buffer.append(super.toString()); 214 buffer.append("("); 215 append(suffixes, buffer); 216 buffer.append(")"); 217 return buffer.toString(); 218 } 219 220}