KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > filefilter > DelegateFileFilter


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
18 import java.io.File JavaDoc;
19 import java.io.FileFilter JavaDoc;
20 import java.io.FilenameFilter JavaDoc;
21
22 /**
23  * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
24  *
25  * @since Commons IO 1.0
26  * @version $Revision: 1.8 $ $Date: 2004/02/23 04:37:57 $
27  *
28  * @author Henri Yandell
29  * @author Stephen Colebourne
30  */

31 public class DelegateFileFilter extends AbstractFileFilter {
32
33     /** The Filename filter */
34     private FilenameFilter JavaDoc filenameFilter;
35     /** The File filter */
36     private FileFilter JavaDoc 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 JavaDoc filter) {
44         if (filter == null) {
45             throw new IllegalArgumentException JavaDoc("The FilenameFilter must not be null");
46         }
47         this.filenameFilter = filter;
48     }
49
50     /**
51      * Constructs a delegate file filter around an existing FileFilter.
52      *
53      * @param filter the filter to decorate
54      */

55     public DelegateFileFilter(FileFilter JavaDoc filter) {
56         if (filter == null) {
57             throw new IllegalArgumentException JavaDoc("The FileFilter must not be null");
58         }
59         this.fileFilter = filter;
60     }
61
62     /**
63      * Checks the filter.
64      *
65      * @param file the file to check
66      * @return true if the filter matches
67      */

68     public boolean accept(File JavaDoc file) {
69         if (fileFilter != null) {
70             return fileFilter.accept(file);
71         } else {
72             return super.accept(file);
73         }
74     }
75
76     /**
77      * Checks the filter.
78      *
79      * @param dir the directory
80      * @param name the filename in the directory
81      * @return true if the filter matches
82      */

83     public boolean accept(File JavaDoc dir, String JavaDoc name) {
84         if (filenameFilter != null) {
85             return filenameFilter.accept(dir, name);
86         } else {
87             return super.accept(dir, name);
88         }
89     }
90     
91 }
92
Popular Tags