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