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.FileFilter;
21  import java.io.FilenameFilter;
22  import java.io.Serializable;
23  import java.util.Objects;
24  
25  /**
26   * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
27   * <h2>Deprecating Serialization</h2>
28   * <p>
29   * <em>Serialization is deprecated and will be removed in 3.0.</em>
30   * </p>
31   *
32   * @since 1.0
33   * @see FileFilterUtils#asFileFilter(FileFilter)
34   * @see FileFilterUtils#asFileFilter(FilenameFilter)
35   */
36  public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
37  
38      private static final long serialVersionUID = -8723373124984771318L;
39  
40      /** The File filter */
41      private transient final FileFilter fileFilter;
42      /** The Filename filter */
43      private transient final FilenameFilter fileNameFilter;
44  
45      /**
46       * Constructs a delegate file filter around an existing FileFilter.
47       *
48       * @param fileFilter  the filter to decorate
49       */
50      public DelegateFileFilter(final FileFilter fileFilter) {
51          Objects.requireNonNull(fileFilter, "filter");
52          this.fileFilter = fileFilter;
53          this.fileNameFilter = null;
54      }
55  
56      /**
57       * Constructs a delegate file filter around an existing FilenameFilter.
58       *
59       * @param fileNameFilter  the filter to decorate
60       */
61      public DelegateFileFilter(final FilenameFilter fileNameFilter) {
62          Objects.requireNonNull(fileNameFilter, "filter");
63          this.fileNameFilter = fileNameFilter;
64          this.fileFilter = null;
65      }
66  
67      /**
68       * Checks the filter.
69       *
70       * @param file  the file to check
71       * @return true if the filter matches
72       */
73      @Override
74      public boolean accept(final File file) {
75          if (fileFilter != null) {
76              return fileFilter.accept(file);
77          }
78          return super.accept(file);
79      }
80  
81      /**
82       * Checks the filter.
83       *
84       * @param dir  the directory
85       * @param name  the file name in the directory
86       * @return true if the filter matches
87       */
88      @Override
89      public boolean accept(final File dir, final String name) {
90          if (fileNameFilter != null) {
91              return fileNameFilter.accept(dir, name);
92          }
93          return super.accept(dir, name);
94      }
95  
96      /**
97       * Provide a String representation of this file filter.
98       *
99       * @return a String representation
100      */
101     @Override
102     public String toString() {
103         final String delegate = fileFilter != null ? fileFilter.toString() : fileNameFilter.toString();
104         return super.toString() + "(" + delegate + ")";
105     }
106 
107 }