01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
23: /**
24: * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
25: *
26: * @since Commons IO 1.0
27: * @version $Revision: 471628 $ $Date: 2006-11-06 05:06:45 +0100 (Mo, 06 Nov 2006) $
28: *
29: * @author Stephen Colebourne
30: */
31: public class DelegateFileFilter extends AbstractFileFilter {
32:
33: /** The Filename filter */
34: private FilenameFilter filenameFilter;
35: /** The File filter */
36: private FileFilter fileFilter;
37:
38: /**
39: * Constructs a delegate file filter around an existing FilenameFilter.
40: *
41: * @param filter the filter to decorate
42: */
43: public DelegateFileFilter(FilenameFilter filter) {
44: if (filter == null) {
45: throw new IllegalArgumentException(
46: "The FilenameFilter must not be null");
47: }
48: this .filenameFilter = filter;
49: }
50:
51: /**
52: * Constructs a delegate file filter around an existing FileFilter.
53: *
54: * @param filter the filter to decorate
55: */
56: public DelegateFileFilter(FileFilter filter) {
57: if (filter == null) {
58: throw new IllegalArgumentException(
59: "The FileFilter must not be null");
60: }
61: this .fileFilter = filter;
62: }
63:
64: /**
65: * Checks the filter.
66: *
67: * @param file the file to check
68: * @return true if the filter matches
69: */
70: public boolean accept(File file) {
71: if (fileFilter != null) {
72: return fileFilter.accept(file);
73: } else {
74: return super .accept(file);
75: }
76: }
77:
78: /**
79: * Checks the filter.
80: *
81: * @param dir the directory
82: * @param name the filename in the directory
83: * @return true if the filter matches
84: */
85: public boolean accept(File dir, String name) {
86: if (filenameFilter != null) {
87: return filenameFilter.accept(dir, name);
88: } else {
89: return super.accept(dir, name);
90: }
91: }
92:
93: }
|