KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui2 > FilterMatcher


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
18  */

19
20 package edu.umd.cs.findbugs.gui2;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.lang.RuntimeException JavaDoc;
25 import javax.swing.tree.TreePath JavaDoc;
26
27 import edu.umd.cs.findbugs.BugInstance;
28 import edu.umd.cs.findbugs.filter.Matcher;
29 import edu.umd.cs.findbugs.gui2.BugAspects.StringPair;
30
31 /**
32  * Why this wasn't just called Filter is still somewhat of a mystery.
33  * FilterMatchers are Filters, pass in a StringPair like Priority, High
34  * and all the high priority bugs disappear, Its that easy.
35  */

36 public class FilterMatcher implements Matcher, Serializable JavaDoc, Comparable JavaDoc<FilterMatcher>
37 {
38     private static final long serialVersionUID = -4859486064351510016L;
39     public static final int FILTER_EXACTLY = 0;
40     public static final int FILTER_AT_OR_AFTER = 1;
41     public static final int FILTER_AT_OR_BEFORE = 2;
42     private Sortables filterBy;
43     private String JavaDoc value;
44     private int mode;
45     protected boolean active;
46     private static HashSet JavaDoc<FilterListener> listeners = new HashSet JavaDoc<FilterListener>();
47     
48     public FilterMatcher(StringPair sp)
49     {
50         this(sp.key, sp.value);
51     }
52     
53     Sortables getFilterBy()
54     {
55         return filterBy;
56     }
57     
58     String JavaDoc getValue()
59     {
60         return value;
61     }
62     
63     public FilterMatcher(Sortables filterBy, String JavaDoc value, int mode) //0 = exactly; 1 = at or after; 2 = at or before
64
{
65         this.filterBy = filterBy;
66         this.value = value;
67         this.mode = mode;
68         this.active = true;
69     }
70     
71     public FilterMatcher(Sortables filterBy, String JavaDoc value)
72     {
73         this.filterBy = filterBy;
74         this.value = value;
75         this.mode = FILTER_EXACTLY;
76         this.active = true;
77     }
78     public void setActive(boolean active)
79     {
80         if (active != this.active)
81         {
82             this.active = active;
83             if (active==true)
84                 notifyListeners(FilterListener.FILTERING, null);
85             else
86                 notifyListeners(FilterListener.UNFILTERING, null);
87         }
88     }
89     
90     public boolean isActive()
91     {
92         return active;
93     }
94     
95     public boolean match(BugInstance bugInstance)
96     {
97         if (!active)
98             return true;
99         
100         SortableStringComparator ssc = new SortableStringComparator(filterBy);
101         switch(mode)
102         {
103         case FILTER_EXACTLY: return (ssc.compare(filterBy.getFrom(bugInstance), value) != 0);
104         case FILTER_AT_OR_AFTER: return (ssc.compare(filterBy.getFrom(bugInstance), value) == -1);
105         case FILTER_AT_OR_BEFORE: return (ssc.compare(filterBy.getFrom(bugInstance), value) == 1);
106         default: return true;
107         }
108     }
109     
110     public String JavaDoc toString()
111     {
112         switch(mode)
113         {
114         case FILTER_EXACTLY: return filterBy.toString() + " " + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is", "is") + " " + edu.umd.cs.findbugs.L10N.getLocalString("mode.equal_to", "equal to") + " " + filterBy.formatValue(value);
115         case FILTER_AT_OR_AFTER: return filterBy.toString() + " " + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is", "is") + " " + edu.umd.cs.findbugs.L10N.getLocalString("mode.at_or_after", "at or after") + " " + filterBy.formatValue(value);
116         case FILTER_AT_OR_BEFORE: return filterBy.toString() + " " + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is", "is") + " " + edu.umd.cs.findbugs.L10N.getLocalString("mode.at_or_before", "at or before") + " " + filterBy.formatValue(value);
117         default: throw new RuntimeException JavaDoc();
118         }
119     }
120     
121     public static boolean addFilterListener(FilterListener newListener)
122     {
123         return listeners.add(newListener);
124     }
125     
126     public static void removeFilterListener(FilterListener toRemove)
127     {
128         listeners.remove(toRemove);
129     }
130     
131     public static void notifyListeners(int whatsGoingOnCode, TreePath JavaDoc optionalPath)
132     {
133         HashSet JavaDoc<FilterListener> listeners = (HashSet JavaDoc<FilterListener>)FilterMatcher.listeners.clone();
134         if (whatsGoingOnCode==FilterListener.FILTERING || whatsGoingOnCode== FilterListener.UNFILTERING)
135             for (FilterListener i : listeners)
136                 i.clearCache();
137         else if (whatsGoingOnCode==FilterListener.SUPPRESSING && optionalPath!=null)
138             for (FilterListener i : listeners)
139                 i.suppressBug(optionalPath);
140         else if (whatsGoingOnCode==FilterListener.UNSUPPRESSING && optionalPath!=null)
141             for (FilterListener i : listeners)
142                 i.unsuppressBug(optionalPath);
143     }
144     
145     public boolean equals(Object JavaDoc o)
146     {
147         if (!(o instanceof FilterMatcher))
148             return false;
149         if (filterBy.equals(((FilterMatcher)o).filterBy) && value.equals(((FilterMatcher)o).value))
150             return true;
151         return false;
152     }
153     
154     public int hashCode()
155     {
156         return value.hashCode() + filterBy.hashCode();
157     }
158
159     public int compareTo(FilterMatcher that)
160     {
161         if (this.filterBy != that.filterBy)
162             return (this.filterBy.ordinal() < that.filterBy.ordinal() ? -1 : 1);
163         
164         return this.value.compareTo(that.value);
165     }
166 }
167
Popular Tags