PageRenderTime 30ms CodeModel.GetById 13ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/testability-explorer/src/main/java/com/google/test/metric/RegExpWhiteList.java

http://testability-explorer.googlecode.com/
Java | 63 lines | 48 code | 15 blank | 0 comment | 7 complexity | 2f7ec80817b305fcde2d8d80f99b6294 MD5 | raw file
 1package com.google.test.metric;
 2
 3import static java.util.regex.Pattern.compile;
 4
 5import java.util.ArrayList;
 6import java.util.List;
 7import java.util.regex.Matcher;
 8import java.util.regex.Pattern;
 9
10public class RegExpWhiteList implements WhiteList {
11
12  class Predicate {
13    private final Pattern pattern;
14
15    public Predicate(String regExp) {
16      pattern = compile(regExp);
17    }
18
19    boolean isClassWhitelisted(String className){
20      Matcher matcher = pattern.matcher(className);
21      return matcher.find() && matcher.start() == 0;
22    }
23  }
24
25  class NotPredicate extends Predicate{
26
27    public NotPredicate(String regExp) {
28      super(regExp);
29    }
30
31    @Override
32    boolean isClassWhitelisted(String className) {
33      return ! super.isClassWhitelisted(className);
34    }
35
36  }
37
38  private final List<Predicate> patterns = new ArrayList<Predicate>();
39
40  public RegExpWhiteList(String... regexps) {
41    for (String regExp : regexps) {
42      addPackage(regExp);
43    }
44  }
45
46  public boolean isClassWhiteListed(String className) {
47    for (Predicate predicate : patterns) {
48      if (predicate.isClassWhitelisted(className)) {
49        return true;
50      }
51    }
52    return false;
53  }
54
55  public void addPackage(String regExp) {
56    if (regExp.startsWith("!")) {
57      patterns.add(new NotPredicate(regExp.substring(1)));
58    } else {
59      patterns.add(new Predicate(regExp));
60    }
61  }
62
63}