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.vfs2.filter;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Objects;
25  
26  import org.apache.commons.vfs2.FileFilter;
27  import org.apache.commons.vfs2.FileSelectInfo;
28  import org.apache.commons.vfs2.FileSystemException;
29  
30  /**
31   * A {@link FileFilter} providing conditional OR logic across a list of
32   * file filters. This filter returns {@code true} if any filters in the list
33   * return {@code true}. Otherwise, it returns {@code false}. Checking of the
34   * file filter list stops when the first filter returns {@code true}.
35   *
36   * @author This code was originally ported from Apache Commons IO File Filter
37   * @see "https://commons.apache.org/proper/commons-io/"
38   * @since 2.4
39   */
40  public class OrFileFilter implements FileFilter, ConditionalFileFilter, Serializable {
41  
42      private static final long serialVersionUID = 1L;
43  
44      /** The list of file filters. */
45      private final List<FileFilter> fileFilters;
46  
47      /**
48       * Default constructor.
49       */
50      public OrFileFilter() {
51          fileFilters = new ArrayList<>();
52      }
53  
54      /**
55       * Constructs a new file filter that ORs the result of other filters.
56       *
57       * @param filters array of filters, must not be null or empty
58       */
59      public OrFileFilter(final FileFilter... filters) {
60          if (filters == null || filters.length == 0) {
61              throw new IllegalArgumentException("The filters must not be null or empty");
62          }
63          for (final FileFilter filter : filters) {
64              if (filter == null) {
65                  throw new IllegalArgumentException("Null filters are not allowed");
66              }
67          }
68          fileFilters = new ArrayList<>(Arrays.asList(filters));
69      }
70  
71      /**
72       * Constructs a new instance of {@code OrFileFilter} with the specified
73       * filters.
74       *
75       * @param fileFilters the file filters for this filter, copied, null ignored
76       */
77      public OrFileFilter(final List<FileFilter> fileFilters) {
78          if (fileFilters == null) {
79              this.fileFilters = new ArrayList<>();
80          } else {
81              this.fileFilters = new ArrayList<>(fileFilters);
82          }
83      }
84  
85      @Override
86      public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException {
87          for (final FileFilter fileFilter : fileFilters) {
88              if (fileFilter.accept(fileSelectInfo)) {
89                  return true;
90              }
91          }
92          return false;
93      }
94  
95      @Override
96      public void addFileFilter(final FileFilter fileFilter) {
97          fileFilters.add(fileFilter);
98      }
99  
100     @Override
101     public List<FileFilter> getFileFilters() {
102         return Collections.unmodifiableList(fileFilters);
103     }
104 
105     @Override
106     public boolean removeFileFilter(final FileFilter fileFilter) {
107         return fileFilters.remove(fileFilter);
108     }
109 
110     @Override
111     public void setFileFilters(final List<FileFilter> fileFilters) {
112         this.fileFilters.clear();
113         this.fileFilters.addAll(fileFilters);
114     }
115 
116     /**
117      * Provide a String representation of this file filter.
118      *
119      * @return a String representation
120      */
121     @Override
122     public String toString() {
123         final StringBuilder buffer = new StringBuilder();
124         buffer.append(super.toString());
125         buffer.append("(");
126         if (fileFilters != null) {
127             for (int i = 0; i < fileFilters.size(); i++) {
128                 if (i > 0) {
129                     buffer.append(",");
130                 }
131                 buffer.append(Objects.toString(fileFilters.get(i)));
132             }
133         }
134         buffer.append(")");
135         return buffer.toString();
136     }
137 
138 }