Home | History | Annotate | Download | only in apicoverage
      1 /*
      2  * Copyright (C) 2015 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.cts.apicoverage;
     18 
     19 import java.util.ArrayList;
     20 import java.util.List;
     21 
     22 /**
     23  * Util class to support package filtering logic
     24  * <p>
     25  * A list of package prefixes can be added to the filter, and {{@link #accept(String)} method will
     26  * decide if the provided package name matches any of the prefixes.
     27  */
     28 public class PackageFilter {
     29 
     30     private List<String> mFilters = new ArrayList<>();
     31 
     32     /**
     33      * Check if a particular package name matches any of the package prefixes configured in filter.
     34      * If no filters are configured, any package names will be accepted
     35      * @param packageName
     36      * @return
     37      */
     38     public boolean accept(String packageName) {
     39         if (mFilters.isEmpty()) {
     40             return true;
     41         }
     42         for (String filter : mFilters) {
     43             if (packageName.startsWith(filter)) {
     44                 return true;
     45             }
     46         }
     47         return false;
     48     }
     49 
     50     public void addPrefixToFilter(String prefix) {
     51         mFilters.add(prefix);
     52     }
     53 
     54     public void clearFilter() {
     55         mFilters.clear();
     56     }
     57 }
     58