1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.io.filefilter;
  17. import java.io.File;
  18. import java.io.FileFilter;
  19. import java.io.FilenameFilter;
  20. /**
  21. * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
  22. *
  23. * @since Commons IO 1.0
  24. * @version $Revision: 1.8 $ $Date: 2004/02/23 04:37:57 $
  25. *
  26. * @author Henri Yandell
  27. * @author Stephen Colebourne
  28. */
  29. public class DelegateFileFilter extends AbstractFileFilter {
  30. /** The Filename filter */
  31. private FilenameFilter filenameFilter;
  32. /** The File filter */
  33. private FileFilter fileFilter;
  34. /**
  35. * Constructs a delegate file filter around an existing FilenameFilter.
  36. *
  37. * @param filter the filter to decorate
  38. */
  39. public DelegateFileFilter(FilenameFilter filter) {
  40. if (filter == null) {
  41. throw new IllegalArgumentException("The FilenameFilter must not be null");
  42. }
  43. this.filenameFilter = filter;
  44. }
  45. /**
  46. * Constructs a delegate file filter around an existing FileFilter.
  47. *
  48. * @param filter the filter to decorate
  49. */
  50. public DelegateFileFilter(FileFilter filter) {
  51. if (filter == null) {
  52. throw new IllegalArgumentException("The FileFilter must not be null");
  53. }
  54. this.fileFilter = filter;
  55. }
  56. /**
  57. * Checks the filter.
  58. *
  59. * @param file the file to check
  60. * @return true if the filter matches
  61. */
  62. public boolean accept(File file) {
  63. if (fileFilter != null) {
  64. return fileFilter.accept(file);
  65. } else {
  66. return super.accept(file);
  67. }
  68. }
  69. /**
  70. * Checks the filter.
  71. *
  72. * @param dir the directory
  73. * @param name the filename in the directory
  74. * @return true if the filter matches
  75. */
  76. public boolean accept(File dir, String name) {
  77. if (filenameFilter != null) {
  78. return filenameFilter.accept(dir, name);
  79. } else {
  80. return super.accept(dir, name);
  81. }
  82. }
  83. }