Changed license to LGPLv3.
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / structure / Filter.java
1 /*
2  * JavaInspect - Utility to visualize java software
3  * Copyright (C) 2013-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.inspector.java.structure;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import eu.svjatoslav.commons.string.WildCardMatcher;
16
17 public class Filter {
18
19         /**
20          * This class implements filter of classes that will be included or excluded
21          * from resulting graph.
22          * 
23          * Filtering is done by lists of whitelist and blacklist patterns using
24          * wildcards.
25          * 
26          * Filtering logic is such that if at least single whitelist entry is
27          * defined then every class that is not whitelisted is automatically
28          * excluded from graph.
29          * 
30          * Otherwise every class in included in graph that is not blacklisted.
31          */
32
33         private final List<String> blacklistClassPatterns = new ArrayList<String>();
34
35         private final List<String> whitelistClassPatterns = new ArrayList<String>();
36
37         public void blacklistClassPattern(final String pattern) {
38                 blacklistClassPatterns.add(pattern);
39         }
40
41         public boolean isClassShown(final String className) {
42                 for (final String pattern : blacklistClassPatterns)
43                         if (WildCardMatcher.match(className, pattern))
44                                 return false;
45
46                 if (!whitelistClassPatterns.isEmpty()) {
47                         for (final String pattern : whitelistClassPatterns)
48                                 if (WildCardMatcher.match(className, pattern))
49                                         return true;
50                         return false;
51                 }
52
53                 return true;
54         }
55
56         public void whitelistClassPattern(final String pattern) {
57                 whitelistClassPatterns.add(pattern);
58         }
59
60 }