1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.io;
22
23import java.io.IOException;
24
25
26/**
27 * This DataEntryReader delegates to one of two other DataEntryReader instances,
28 * depending on whether the data entry passes through a given data entry filter
29 * or not.
30 *
31 * @author Eric Lafortune
32 */
33public class FilteredDataEntryReader implements DataEntryReader
34{
35    private final DataEntryFilter dataEntryFilter;
36    private final DataEntryReader acceptedDataEntryReader;
37    private final DataEntryReader rejectedDataEntryReader;
38
39
40    /**
41     * Creates a new FilteredDataEntryReader with only a reader for accepted
42     * data entries.
43     * @param dataEntryFilter         the data entry filter.
44     * @param acceptedDataEntryReader the DataEntryReader to which the reading
45     *                                will be delegated if the filter accepts
46     *                                the data entry. May be <code>null</code>.
47     */
48    public FilteredDataEntryReader(DataEntryFilter dataEntryFilter,
49                                   DataEntryReader acceptedDataEntryReader)
50    {
51        this(dataEntryFilter, acceptedDataEntryReader, null);
52    }
53
54
55    /**
56     * Creates a new FilteredDataEntryReader.
57     * @param dataEntryFilter         the data entry filter.
58     * @param acceptedDataEntryReader the DataEntryReader to which the reading
59     *                                will be delegated if the filter accepts
60     *                                the data entry. May be <code>null</code>.
61     * @param rejectedDataEntryReader the DataEntryReader to which the reading
62     *                                will be delegated if the filter does not
63     *                                accept the data entry. May be
64     *                                <code>null</code>.
65     */
66    public FilteredDataEntryReader(DataEntryFilter dataEntryFilter,
67                                   DataEntryReader acceptedDataEntryReader,
68                                   DataEntryReader rejectedDataEntryReader)
69    {
70        this.dataEntryFilter         = dataEntryFilter;
71        this.acceptedDataEntryReader = acceptedDataEntryReader;
72        this.rejectedDataEntryReader = rejectedDataEntryReader;
73    }
74
75
76    // Implementations for DataEntryReader.
77
78    public void read(DataEntry dataEntry)
79    throws IOException
80    {
81        DataEntryReader dataEntryReader = dataEntryFilter.accepts(dataEntry) ?
82            acceptedDataEntryReader :
83            rejectedDataEntryReader;
84
85        if (dataEntryReader != null)
86        {
87            dataEntryReader.read(dataEntry);
88        }
89    }
90}
91