~ubuntu-branches/ubuntu/vivid/commons-io/vivid

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/io/filefilter/DelegateFileFilter.java

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2013-05-18 10:40:54 UTC
  • mfrom: (1.1.4) (3.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20130518104054-wqbtjam30uyseu9j
Tags: 2.4-2
* Team upload.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
24
/**
 
25
 * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
 
26
 * 
 
27
 * @since 1.0
 
28
 * @version $Id: DelegateFileFilter.java 1304052 2012-03-22 20:55:29Z ggregory $
 
29
 * 
 
30
 * @see FileFilterUtils#asFileFilter(FileFilter)
 
31
 * @see FileFilterUtils#asFileFilter(FilenameFilter)
 
32
 */
 
33
public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
 
34
 
 
35
    /** The Filename filter */
 
36
    private final FilenameFilter filenameFilter;
 
37
    /** The File filter */
 
38
    private final FileFilter fileFilter;
 
39
 
 
40
    /**
 
41
     * Constructs a delegate file filter around an existing FilenameFilter.
 
42
     * 
 
43
     * @param filter  the filter to decorate
 
44
     */
 
45
    public DelegateFileFilter(FilenameFilter filter) {
 
46
        if (filter == null) {
 
47
            throw new IllegalArgumentException("The FilenameFilter must not be null");
 
48
        }
 
49
        this.filenameFilter = filter;
 
50
        this.fileFilter = null;
 
51
    }
 
52
 
 
53
    /**
 
54
     * Constructs a delegate file filter around an existing FileFilter.
 
55
     * 
 
56
     * @param filter  the filter to decorate
 
57
     */
 
58
    public DelegateFileFilter(FileFilter filter) {
 
59
        if (filter == null) {
 
60
            throw new IllegalArgumentException("The FileFilter must not be null");
 
61
        }
 
62
        this.fileFilter = filter;
 
63
        this.filenameFilter = null;
 
64
    }
 
65
 
 
66
    /**
 
67
     * Checks the filter.
 
68
     * 
 
69
     * @param file  the file to check
 
70
     * @return true if the filter matches
 
71
     */
 
72
    @Override
 
73
    public boolean accept(File file) {
 
74
        if (fileFilter != null) {
 
75
            return fileFilter.accept(file);
 
76
        } else {
 
77
            return super.accept(file);
 
78
        }
 
79
    }
 
80
 
 
81
    /**
 
82
     * Checks the filter.
 
83
     * 
 
84
     * @param dir  the directory
 
85
     * @param name  the filename in the directory
 
86
     * @return true if the filter matches
 
87
     */
 
88
    @Override
 
89
    public boolean accept(File dir, String name) {
 
90
        if (filenameFilter != null) {
 
91
            return filenameFilter.accept(dir, name);
 
92
        } else {
 
93
            return super.accept(dir, name);
 
94
        }
 
95
    }
 
96
 
 
97
    /**
 
98
     * Provide a String representaion of this file filter.
 
99
     *
 
100
     * @return a String representaion
 
101
     */
 
102
    @Override
 
103
    public String toString() {
 
104
        String delegate = fileFilter != null ? fileFilter.toString() : filenameFilter.toString(); 
 
105
        return super.toString() + "(" + delegate + ")";
 
106
    }
 
107
    
 
108
}