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