View Javadoc
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    *      https://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.filefilter;
18  
19  import java.io.File;
20  import java.io.FileFilter;
21  import java.io.Serializable;
22  import java.nio.file.FileVisitResult;
23  import java.nio.file.Path;
24  import java.nio.file.attribute.BasicFileAttributes;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.Objects;
29  import java.util.stream.Stream;
30  
31  /**
32   * A {@link FileFilter} providing conditional AND logic across a list of
33   * file filters. This filter returns {@code true} if all filters in the
34   * list return {@code true}. Otherwise, it returns {@code false}.
35   * Checking of the file filter list stops when the first filter returns
36   * {@code false}.
37   * <h2>Deprecating Serialization</h2>
38   * <p>
39   * <em>Serialization is deprecated and will be removed in 3.0.</em>
40   * </p>
41   *
42   * @since 1.0
43   * @see FileFilterUtils#and(IOFileFilter...)
44   */
45  public class AndFileFilter extends AbstractFileFilter implements ConditionalFileFilter, Serializable {
46  
47      private static final long serialVersionUID = 7215974688563965257L;
48  
49      /** The list of file filters. */
50      private final List<IOFileFilter> fileFilters;
51  
52      /**
53       * Constructs a new empty instance.
54       *
55       * @since 1.1
56       */
57      public AndFileFilter() {
58          this(0);
59      }
60  
61      /**
62       * Constructs a new instance with the given initial list.
63       *
64       * @param initialList the initial list.
65       */
66      private AndFileFilter(final ArrayList<IOFileFilter> initialList) {
67          this.fileFilters = Objects.requireNonNull(initialList, "initialList");
68      }
69  
70      /**
71       * Constructs a new instance with the given initial capacity.
72       *
73       * @param initialCapacity the initial capacity.
74       */
75      private AndFileFilter(final int initialCapacity) {
76          this(new ArrayList<>(initialCapacity));
77      }
78  
79      /**
80       * Constructs a new instance for the give filters.
81       *
82       * @param fileFilters filters to OR.
83       * @since 2.9.0
84       */
85      public AndFileFilter(final IOFileFilter... fileFilters) {
86          this(Objects.requireNonNull(fileFilters, "fileFilters").length);
87          addFileFilter(fileFilters);
88      }
89  
90      /**
91       * Constructs a new file filter that ANDs the result of other filters.
92       *
93       * @param filter1  the first filter, must second be null
94       * @param filter2  the first filter, must not be null
95       * @throws IllegalArgumentException if either filter is null
96       */
97      public AndFileFilter(final IOFileFilter filter1, final IOFileFilter filter2) {
98          this(2);
99          addFileFilter(filter1);
100         addFileFilter(filter2);
101     }
102 
103     /**
104      * Constructs a new instance of {@link AndFileFilter}
105      * with the specified list of filters.
106      *
107      * @param fileFilters  a List of IOFileFilter instances, copied.
108      * @since 1.1
109      */
110     public AndFileFilter(final List<IOFileFilter> fileFilters) {
111         this(new ArrayList<>(Objects.requireNonNull(fileFilters, "fileFilters")));
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public boolean accept(final File file) {
119         return !isEmpty() && fileFilters.stream().allMatch(fileFilter -> fileFilter.accept(file));
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public boolean accept(final File file, final String name) {
127         return !isEmpty() && fileFilters.stream().allMatch(fileFilter -> fileFilter.accept(file, name));
128     }
129 
130     /**
131      * {@inheritDoc}
132      * @since 2.9.0
133      */
134     @Override
135     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
136         return isEmpty() ? FileVisitResult.TERMINATE
137                 : toDefaultFileVisitResult(fileFilters.stream().allMatch(fileFilter -> fileFilter.accept(file, attributes) == FileVisitResult.CONTINUE));
138     }
139 
140     /**
141      * {@inheritDoc}
142      */
143     @Override
144     public void addFileFilter(final IOFileFilter fileFilter) {
145         fileFilters.add(Objects.requireNonNull(fileFilter, "fileFilter"));
146     }
147 
148     /**
149      * Adds the given file filters.
150      *
151      * @param fileFilters the filters to add.
152      * @since 2.9.0
153      */
154     public void addFileFilter(final IOFileFilter... fileFilters) {
155         Stream.of(Objects.requireNonNull(fileFilters, "fileFilters")).forEach(this::addFileFilter);
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public List<IOFileFilter> getFileFilters() {
163         return Collections.unmodifiableList(fileFilters);
164     }
165 
166     private boolean isEmpty() {
167         return fileFilters.isEmpty();
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
175         return fileFilters.remove(ioFileFilter);
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
181     @Override
182     public void setFileFilters(final List<IOFileFilter> fileFilters) {
183         this.fileFilters.clear();
184         this.fileFilters.addAll(fileFilters);
185     }
186 
187     /**
188      * Builds a String representation of this file filter.
189      *
190      * @return a String representation
191      */
192     @Override
193     public String toString() {
194         final StringBuilder buffer = new StringBuilder();
195         buffer.append(super.toString());
196         buffer.append("(");
197         append(fileFilters, buffer);
198         buffer.append(")");
199         return buffer.toString();
200     }
201 
202 }