Filter.java revision adf4543e57086070c95b3ef439dbb6679b0bd562
1/*
2 * Copyright (C) 2009 The Android Open Source Project
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
17package com.android.mkstubs;
18
19import java.util.TreeSet;
20
21/**
22 * A "filter" holds the various patterns that MkStubs should accept (include)
23 * or reject (exclude). Patterns can be of two kind:
24 * <ul>
25 * <li>Full patterns are simple string matches, similar to a "^pattern$" regex.
26 * <li>Prefix patterns are partial string matches, similar to a "^pattern.*" regex.
27 * </ul>
28 * <p/>
29 * The {@link #accept(String)} method examines a given string against the known
30 * pattern to decide if it should be included.
31 */
32class Filter {
33    private TreeSet<String> mIncludePrefix = new TreeSet<String>();
34    private TreeSet<String> mIncludeFull   = new TreeSet<String>();
35    private TreeSet<String> mExcludePrefix = new TreeSet<String>();
36    private TreeSet<String> mExcludeFull   = new TreeSet<String>();
37
38    /**
39     * Returns the set of all full patterns to be included.
40     */
41    public TreeSet<String> getIncludeFull() {
42        return mIncludeFull;
43    }
44
45    /**
46     * Returns the set of all prefix patterns to be included.
47     */
48    public TreeSet<String> getIncludePrefix() {
49        return mIncludePrefix;
50    }
51
52    /**
53     * Returns the set of all full patterns to be excluded.
54     */
55    public TreeSet<String> getExcludeFull() {
56        return mExcludeFull;
57    }
58
59    /**
60     * Returns the set of all prefix patterns to be excluded.
61     */
62    public TreeSet<String> getExcludePrefix() {
63        return mExcludePrefix;
64    }
65
66    /**
67     * Checks if the given string passes the various include/exclude rules.
68     * The matching is done as follows:
69     * <ul>
70     * <li> The string must match either a full include or a prefix include.
71     * <li> The string must not match any full exclude nor any prefix exclude.
72     * </ul>
73     * @param s The string to accept or reject.
74     * @return True if the string can be accepted, false if it must be rejected.
75     */
76    public boolean accept(String s) {
77
78        // Check if it can be included.
79        boolean accept = mIncludeFull.contains(s);
80        if (!accept) {
81            // Check for a prefix inclusion
82            for (String prefix : mIncludePrefix) {
83                if (s.startsWith(prefix)) {
84                    accept = true;
85                    break;
86                }
87            }
88        }
89
90        if (accept) {
91            // check for a full exclusion
92            accept = !mExcludeFull.contains(s);
93        }
94        if (accept) {
95            // or check for prefix exclusion
96            for (String prefix : mExcludePrefix) {
97                if (s.startsWith(prefix)) {
98                    accept = false;
99                    break;
100                }
101            }
102        }
103
104        return accept;
105    }
106}
107