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 
17 package com.android.mkstubs;
18 
19 import 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  */
32 class Filter {
33     private TreeSet<String> mIncludePrefix = new TreeSet<>();
34     private TreeSet<String> mIncludeFull   = new TreeSet<>();
35     private TreeSet<String> mExcludePrefix = new TreeSet<>();
36     private TreeSet<String> mExcludeFull   = new TreeSet<>();
37 
38     /**
39      * Returns the set of all full patterns to be included.
40      */
getIncludeFull()41     public TreeSet<String> getIncludeFull() {
42         return mIncludeFull;
43     }
44 
45     /**
46      * Returns the set of all prefix patterns to be included.
47      */
getIncludePrefix()48     public TreeSet<String> getIncludePrefix() {
49         return mIncludePrefix;
50     }
51 
52     /**
53      * Returns the set of all full patterns to be excluded.
54      */
getExcludeFull()55     public TreeSet<String> getExcludeFull() {
56         return mExcludeFull;
57     }
58 
59     /**
60      * Returns the set of all prefix patterns to be excluded.
61      */
getExcludePrefix()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      */
accept(String s)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