001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2006, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
018: */
019:
020: package edu.umd.cs.findbugs.gui2;
021:
022: import java.io.IOException;
023: import java.io.Serializable;
024:
025: import edu.umd.cs.findbugs.BugInstance;
026: import edu.umd.cs.findbugs.filter.Matcher;
027: import edu.umd.cs.findbugs.gui2.BugAspects.SortableValue;
028: import edu.umd.cs.findbugs.xml.XMLOutput;
029:
030: /**
031: * Why this wasn't just called Filter is still somewhat of a mystery.
032: * FilterMatchers are Filters, pass in a StringPair like Priority, High
033: * and all the high priority bugs disappear, Its that easy.
034: */
035: @Deprecated
036: public class FilterMatcher implements Matcher, Serializable,
037: Comparable<FilterMatcher> {
038: enum FilterWhere {
039: FILTER_EXACTLY, FILTER_AT_OR_AFTER, FILTER_AT_OR_BEFORE, FILTER_ALL_BUT
040: };
041:
042: private static final long serialVersionUID = -4859486064351510016L;
043:
044: private Sortables filterBy;
045: private String value;
046: private FilterWhere mode;
047: protected boolean active;
048:
049: public FilterMatcher(SortableValue sp) {
050: this (sp.key, sp.value);
051: }
052:
053: Sortables getFilterBy() {
054: return filterBy;
055: }
056:
057: String getValue() {
058: return value;
059: }
060:
061: public FilterMatcher(Sortables filterBy, String value,
062: FilterWhere mode) //0 = exactly; 1 = at or after; 2 = at or before; 3 = not at
063: {
064: this .filterBy = filterBy;
065: this .value = value;
066: this .mode = mode;
067: this .active = true;
068: }
069:
070: public FilterMatcher(Sortables filterBy, String value) {
071: this .filterBy = filterBy;
072: this .value = value;
073: this .mode = FilterWhere.FILTER_EXACTLY;
074: this .active = true;
075: }
076:
077: public void setActive(boolean active) {
078: if (active != this .active) {
079: this .active = active;
080: if (active == true)
081: FilterActivity.notifyListeners(
082: FilterListener.Action.FILTERING, null);
083: else
084: FilterActivity.notifyListeners(
085: FilterListener.Action.UNFILTERING, null);
086: }
087: }
088:
089: public boolean isActive() {
090: return active;
091: }
092:
093: public boolean match(BugInstance bugInstance) {
094: if (!active)
095: return true;
096:
097: SortableStringComparator ssc = new SortableStringComparator(
098: filterBy);
099: switch (mode) {
100: case FILTER_EXACTLY:
101: return (ssc.compare(filterBy.getFrom(bugInstance), value) != 0);
102: case FILTER_AT_OR_AFTER:
103: return (ssc.compare(filterBy.getFrom(bugInstance), value) < 0);
104: case FILTER_AT_OR_BEFORE:
105: return (ssc.compare(filterBy.getFrom(bugInstance), value) > 0);
106: case FILTER_ALL_BUT:
107: return (ssc.compare(filterBy.getFrom(bugInstance), value) == 0);
108: default:
109: return true;
110: }
111: }
112:
113: @Override
114: public String toString() {
115: switch (mode) {
116: case FILTER_EXACTLY:
117: return filterBy.toString()
118: + " "
119: + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is",
120: "is")
121: + " "
122: + edu.umd.cs.findbugs.L10N.getLocalString(
123: "mode.equal_to", "equal to") + " "
124: + filterBy.formatValue(value);
125: case FILTER_AT_OR_AFTER:
126: return filterBy.toString()
127: + " "
128: + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is",
129: "is")
130: + " "
131: + edu.umd.cs.findbugs.L10N.getLocalString(
132: "mode.at_or_after", "at or after") + " "
133: + filterBy.formatValue(value);
134: case FILTER_AT_OR_BEFORE:
135: return filterBy.toString()
136: + " "
137: + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is",
138: "is")
139: + " "
140: + edu.umd.cs.findbugs.L10N.getLocalString(
141: "mode.at_or_before", "at or before") + " "
142: + filterBy.formatValue(value);
143: case FILTER_ALL_BUT:
144: return filterBy.toString()
145: + " "
146: + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is",
147: "is")
148: + " "
149: + edu.umd.cs.findbugs.L10N.getLocalString(
150: "mode.not_equal_to", "not equal to") + " "
151: + filterBy.formatValue(value);
152: default:
153: throw new RuntimeException();
154: }
155: }
156:
157: @Override
158: public boolean equals(Object o) {
159: if (o == null)
160: return false;
161: if (getClass() != o.getClass())
162: return false;
163:
164: if (filterBy.equals(((FilterMatcher) o).filterBy)
165: && value.equals(((FilterMatcher) o).value))
166: return true;
167: return false;
168: }
169:
170: @Override
171: public int hashCode() {
172: return value.hashCode() + filterBy.hashCode();
173: }
174:
175: public int compareTo(FilterMatcher that) {
176: if (this .filterBy != that.filterBy)
177: return (this .filterBy.ordinal() < that.filterBy.ordinal() ? -1
178: : 1);
179:
180: return this .value.compareTo(that.value);
181: }
182:
183: public void writeXML(XMLOutput xmlOutput, boolean disabled)
184: throws IOException {
185: throw new UnsupportedOperationException();
186: }
187: }
|