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 filter providing conditional AND logic across a list of file filters. This
31   * filter returns {@code true} if all filters in the list return {@code true}.
32   * Otherwise, it returns {@code false}. Checking of the file filter list stops
33   * when the first filter returns {@code false}.
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 AndFileFilter 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 AndFileFilter() {
50          this.fileFilters = new ArrayList<>();
51      }
52  
53      /**
54       * Constructs a new file filter that ANDs the result of other filters.
55       *
56       * @param filters array of filters, must not be null or empty
57       */
58      public AndFileFilter(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>AndFileFilter</code> with the specified
72       * list of filters.
73       *
74       * @param fileFilters a List of FileFilter instances, copied, null ignored
75       */
76      public AndFileFilter(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          if (this.fileFilters.isEmpty()) {
87              return false;
88          }
89          for (final FileFilter fileFilter : fileFilters) {
90              if (!fileFilter.accept(fileInfo)) {
91                  return false;
92              }
93          }
94          return true;
95      }
96  
97      @Override
98      public void addFileFilter(final FileFilter fileFilter) {
99          this.fileFilters.add(fileFilter);
100     }
101 
102     @Override
103     public List<FileFilter> getFileFilters() {
104         return Collections.unmodifiableList(this.fileFilters);
105     }
106 
107     @Override
108     public boolean removeFileFilter(final FileFilter fileFilter) {
109         return this.fileFilters.remove(fileFilter);
110     }
111 
112     @Override
113     public void setFileFilters(final List<FileFilter> fileFilters) {
114         this.fileFilters.clear();
115         this.fileFilters.addAll(fileFilters);
116     }
117 
118     /**
119      * Provide a String representation of this file filter.
120      *
121      * @return a String representation
122      */
123     @Override
124     public String toString() {
125         final StringBuilder buffer = new StringBuilder();
126         buffer.append(super.toString());
127         buffer.append("(");
128         if (fileFilters != null) {
129             for (int i = 0; i < fileFilters.size(); i++) {
130                 if (i > 0) {
131                     buffer.append(",");
132                 }
133                 final Object filter = fileFilters.get(i);
134                 buffer.append(filter == null ? "null" : filter.toString());
135             }
136         }
137         buffer.append(")");
138         return buffer.toString();
139     }
140 
141 }