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