~ubuntu-branches/ubuntu/karmic/commons-io/karmic

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2007-04-13 08:20:49 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070413082049-2hd3s5ixtxsgvnvm
Tags: 1.3.1.dfsg.1-1
* New upstream (closes: #418973)
* java-gcj-compat-dev and cdbs transition
* Removed junit at the moment (closes: #397567)
* No javadoc at the moment

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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
 
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
7
8
 * 
8
9
 *      http://www.apache.org/licenses/LICENSE-2.0
9
10
 * 
16
17
package org.apache.commons.io.filefilter;
17
18
 
18
19
import java.io.File;
 
20
import java.util.ArrayList;
 
21
import java.util.Collections;
 
22
import java.util.Iterator;
 
23
import java.util.List;
19
24
 
20
25
/**
21
 
 * This filter produces a logical AND of the two filters specified.
 
26
 * A {@link java.io.FileFilter} providing conditional AND logic across a list of
 
27
 * file filters. This filter returns <code>true</code> if all filters in the
 
28
 * list return <code>true</code>. Otherwise, it returns <code>false</code>.
 
29
 * Checking of the file filter list stops when the first filter returns
 
30
 * <code>false</code>.
22
31
 *
23
32
 * @since Commons IO 1.0
24
 
 * @version $Revision: 1.8 $ $Date: 2004/02/23 04:37:57 $
25
 
 * 
26
 
 * @author Stephen Colebourne
 
33
 * @version $Revision: 490425 $ $Date: 2006-12-26 17:25:43 -0800 (Tue, 26 Dec 2006) $
 
34
 *
 
35
 * @author Steven Caswell
27
36
 */
28
 
public class AndFileFilter extends AbstractFileFilter {
29
 
    
30
 
    /** The first filter */
31
 
    private IOFileFilter filter1;
32
 
    /** The second filter */
33
 
    private IOFileFilter filter2;
 
37
public class AndFileFilter
 
38
        extends AbstractFileFilter
 
39
        implements ConditionalFileFilter {
 
40
 
 
41
    /** The list of file filters. */
 
42
    private List fileFilters;
 
43
 
 
44
    /**
 
45
     * Constructs a new instance of <code>AndFileFilter</code>.
 
46
     *
 
47
     * @since Commons IO 1.1
 
48
     */
 
49
    public AndFileFilter() {
 
50
        this.fileFilters = new ArrayList();
 
51
    }
 
52
 
 
53
    /**
 
54
     * Constructs a new instance of <code>AndFileFilter</code>
 
55
     * with the specified list of filters.
 
56
     *
 
57
     * @param fileFilters  a List of IOFileFilter instances, copied, null ignored
 
58
     * @since Commons IO 1.1
 
59
     */
 
60
    public AndFileFilter(final List fileFilters) {
 
61
        if (fileFilters == null) {
 
62
            this.fileFilters = new ArrayList();
 
63
        } else {
 
64
            this.fileFilters = new ArrayList(fileFilters);
 
65
        }
 
66
    }
34
67
 
35
68
    /**
36
69
     * Constructs a new file filter that ANDs the result of two other filters.
37
 
     * 
 
70
     *
38
71
     * @param filter1  the first filter, must not be null
39
72
     * @param filter2  the second filter, must not be null
40
73
     * @throws IllegalArgumentException if either filter is null
43
76
        if (filter1 == null || filter2 == null) {
44
77
            throw new IllegalArgumentException("The filters must not be null");
45
78
        }
46
 
        this.filter1 = filter1;
47
 
        this.filter2 = filter2;
48
 
    }
49
 
 
50
 
    /**
51
 
     * Checks to see if both filters are true.
52
 
     * 
53
 
     * @param file  the File to check
54
 
     * @return true if both filters are true
55
 
     */
56
 
    public boolean accept(File file) {
57
 
        return filter1.accept(file) && filter2.accept(file);
58
 
    }
59
 
    
60
 
    /**
61
 
     * Checks to see if both filters are true.
62
 
     * 
63
 
     * @param file  the File directory
64
 
     * @param name  the filename
65
 
     * @return true if both filters are true
66
 
     */
67
 
    public boolean accept(File file, String name) {
68
 
        return filter1.accept(file, name) && filter2.accept(file, name);
69
 
    }
70
 
    
 
79
        this.fileFilters = new ArrayList();
 
80
        addFileFilter(filter1);
 
81
        addFileFilter(filter2);
 
82
    }
 
83
 
 
84
    /**
 
85
     * {@inheritDoc}
 
86
     */
 
87
    public void addFileFilter(final IOFileFilter ioFileFilter) {
 
88
        this.fileFilters.add(ioFileFilter);
 
89
    }
 
90
 
 
91
    /**
 
92
     * {@inheritDoc}
 
93
     */
 
94
    public List getFileFilters() {
 
95
        return Collections.unmodifiableList(this.fileFilters);
 
96
    }
 
97
 
 
98
    /**
 
99
     * {@inheritDoc}
 
100
     */
 
101
    public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
 
102
        return this.fileFilters.remove(ioFileFilter);
 
103
    }
 
104
 
 
105
    /**
 
106
     * {@inheritDoc}
 
107
     */
 
108
    public void setFileFilters(final List fileFilters) {
 
109
        this.fileFilters = new ArrayList(fileFilters);
 
110
    }
 
111
 
 
112
    /**
 
113
     * {@inheritDoc}
 
114
     */
 
115
    public boolean accept(final File file) {
 
116
        if (this.fileFilters.size() == 0) {
 
117
            return false;
 
118
        }
 
119
        for (Iterator iter = this.fileFilters.iterator(); iter.hasNext();) {
 
120
            IOFileFilter fileFilter = (IOFileFilter) iter.next();
 
121
            if (!fileFilter.accept(file)) {
 
122
                return false;
 
123
            }
 
124
        }
 
125
        return true;
 
126
    }
 
127
 
 
128
    /**
 
129
     * {@inheritDoc}
 
130
     */
 
131
    public boolean accept(final File file, final String name) {
 
132
        if (this.fileFilters.size() == 0) {
 
133
            return false;
 
134
        }
 
135
        for (Iterator iter = this.fileFilters.iterator(); iter.hasNext();) {
 
136
            IOFileFilter fileFilter = (IOFileFilter) iter.next();
 
137
            if (!fileFilter.accept(file, name)) {
 
138
                return false;
 
139
            }
 
140
        }
 
141
        return true;
 
142
    }
 
143
 
71
144
}