DelegateFileFilter.java

  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. import java.io.File;
  19. import java.io.FileFilter;
  20. import java.io.FilenameFilter;
  21. import java.io.Serializable;
  22. import java.util.Objects;

  23. /**
  24.  * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
  25.  * <h2>Deprecating Serialization</h2>
  26.  * <p>
  27.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  28.  * </p>
  29.  *
  30.  * @since 1.0
  31.  * @see FileFilterUtils#asFileFilter(FileFilter)
  32.  * @see FileFilterUtils#asFileFilter(FilenameFilter)
  33.  */
  34. public class DelegateFileFilter extends AbstractFileFilter implements Serializable {

  35.     private static final long serialVersionUID = -8723373124984771318L;

  36.     /** The File filter */
  37.     private final transient FileFilter fileFilter;
  38.     /** The Filename filter */
  39.     private final transient FilenameFilter fileNameFilter;

  40.     /**
  41.      * Constructs a delegate file filter around an existing FileFilter.
  42.      *
  43.      * @param fileFilter  the filter to decorate
  44.      */
  45.     public DelegateFileFilter(final FileFilter fileFilter) {
  46.         Objects.requireNonNull(fileFilter, "filter");
  47.         this.fileFilter = fileFilter;
  48.         this.fileNameFilter = null;
  49.     }

  50.     /**
  51.      * Constructs a delegate file filter around an existing FilenameFilter.
  52.      *
  53.      * @param fileNameFilter  the filter to decorate
  54.      */
  55.     public DelegateFileFilter(final FilenameFilter fileNameFilter) {
  56.         Objects.requireNonNull(fileNameFilter, "filter");
  57.         this.fileNameFilter = fileNameFilter;
  58.         this.fileFilter = null;
  59.     }

  60.     /**
  61.      * Checks the filter.
  62.      *
  63.      * @param file  the file to check
  64.      * @return true if the filter matches
  65.      */
  66.     @Override
  67.     public boolean accept(final File file) {
  68.         if (fileFilter != null) {
  69.             return fileFilter.accept(file);
  70.         }
  71.         return super.accept(file);
  72.     }

  73.     /**
  74.      * Checks the filter.
  75.      *
  76.      * @param dir  the directory
  77.      * @param name  the file name in the directory
  78.      * @return true if the filter matches
  79.      */
  80.     @Override
  81.     public boolean accept(final File dir, final String name) {
  82.         if (fileNameFilter != null) {
  83.             return fileNameFilter.accept(dir, name);
  84.         }
  85.         return super.accept(dir, name);
  86.     }

  87.     /**
  88.      * Provide a String representation of this file filter.
  89.      *
  90.      * @return a String representation
  91.      */
  92.     @Override
  93.     public String toString() {
  94.         final String delegate = Objects.toString(fileFilter, Objects.toString(fileNameFilter, null));
  95.         return super.toString() + "(" + delegate + ")";
  96.     }

  97. }