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.util.ArrayList;
023: import java.util.Iterator;
024:
025: import edu.umd.cs.findbugs.filter.Matcher;
026:
027: /**
028: * These are the branches in our tree, each branch forms a complete query that could be sent
029: * to the main bugset to return all the bugs it contains
030: * For example, a single bugAspects could be <priority,high> or it could be <priority,high>,
031: * <designation,must fix>,<class,fishpond>,<package,default>
032: *
033: * In this implementation, <priority,high>,<designation,unclassified> is different from
034: * <designation,unclassified>,<priority,high>. (I'm not talking about the fact we use the .equals
035: * from ArrayList, I'm talking about what a query would return, though both are true)
036: * For a speed boost, this class could be rewritten to make these equal, BugSet could be rewritten
037: * to cache full queries off the main BugSet, (instead of caching each part of the query separately
038: * in the BugSets created) and resetData could be rewritten to work more like Swing's validate, only
039: * clearing data if the data is wrong. This would save time after changing certain aspects of the tree.
040: * Just an idea, I wouldn't suggest it unless its absolutely necessary. -Dan
041: *
042: *
043: * @author All of us
044: */
045: public class BugAspects implements Iterable<BugAspects.SortableValue> {
046: private static final long serialVersionUID = -5503915081879996968L;
047: private int count = -1;
048: private ArrayList<BugAspects.SortableValue> lst = new ArrayList<BugAspects.SortableValue>();
049:
050: public SortableValue last() {
051: return lst.get(lst.size() - 1);
052: }
053:
054: public int size() {
055: return lst.size();
056: }
057:
058: @Override
059: public String toString() {
060: if (lst.isEmpty())
061: return edu.umd.cs.findbugs.L10N.getLocalString("tree.bugs",
062: "Bugs")
063: + " (" + count + ")";
064: else {
065: if (count == -1)
066: return last().value;
067: else
068: return last().key.formatValue(last().value) + " ("
069: + count + ")";
070: }
071: }
072:
073: /**
074: * This is how the numbers after the branches contain the number of bugs in them, even if they aren't the final branch
075: * @param count
076: */
077: public void setCount(int count) {
078: this .count = count;
079: }
080:
081: public int getCount() {
082: return count;
083: }
084:
085: public BugAspects() {
086: super ();
087: }
088:
089: public BugAspects(BugAspects a) {
090: lst = new ArrayList<SortableValue>(a.lst);
091: count = a.count;
092: }
093:
094: public void add(SortableValue sp) {
095: lst.add(sp);
096: }
097:
098: public BugAspects addToNew(SortableValue sp) {
099: BugAspects result = new BugAspects(this );
100: result.lst.add(sp);
101: return result;
102: }
103:
104: public Matcher getMatcher() {
105: return FilterFactory.makeAndMatcher(lst);
106: }
107:
108: public StackedFilterMatcher getStackedFilterMatcher() {
109: FilterMatcher[] filters = new FilterMatcher[lst.size()];
110: for (int i = 0; i < filters.length; i++)
111: filters[i] = new FilterMatcher(lst.get(i));
112: StackedFilterMatcher sfm = new StackedFilterMatcher(filters);
113: return sfm;
114: }
115:
116: public BugSet getMatchingBugs(BugSet theSet) {
117: return theSet.getBugsMatchingFilter(this
118: .getStackedFilterMatcher());
119: }
120:
121: static class SortableValue {
122: final public Sortables key;
123: final public String value;
124:
125: public SortableValue(Sortables key, String value) {
126: this .key = key;
127: this .value = value;
128: }
129:
130: @Override
131: public int hashCode() {
132: return key.hashCode() + value.hashCode();
133: }
134:
135: @Override
136: public boolean equals(Object that) {
137: if (!(that instanceof SortableValue))
138: return false;
139: SortableValue thatStringPair = ((SortableValue) that);
140: return this .key.equals(thatStringPair.key)
141: && this .value.equals(thatStringPair.value);
142: }
143:
144: @Override
145: public String toString() {
146: return key + ":" + value;
147: }
148: }
149:
150: public Iterator<SortableValue> iterator() {
151: return lst.iterator();
152: }
153: }
|