KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > obl > ObligationSet


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2004,2005 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.ba.obl;
21
22
23 /**
24  * A multiset of obligations that must be cleaned up by
25  * error-handling code.
26  *
27  * <p>See Weimer and Necula,
28  * <a HREF="http://doi.acm.org/10.1145/1028976.1029011"
29  * >Finding and preventing run-time error handling mistakes</a>,
30  * OOPSLA 2004.</p>
31  *
32  * @author David Hovemeyer
33  */

34 public class ObligationSet {
35     private static final int INVALID_HASH_CODE = -1;
36
37     private final short[] countList;
38     private final ObligationFactory factory;
39     private int cachedHashCode;
40
41     public ObligationSet(int maxObligationTypes, ObligationFactory factory) {
42         this.countList = new short[maxObligationTypes];
43         this.factory = factory;
44         invalidate();
45     }
46     
47     public int getMaxObligationTypes() {
48         return countList.length;
49     }
50
51     public void add(Obligation obligation) {
52         invalidate();
53         countList[obligation.getId()]++;
54     }
55
56     public void remove(Obligation obligation) throws NonexistentObligationException {
57         short count = countList[obligation.getId()];
58         
59         // It is possible to remove a nonexistent obligation.
60
// Generally this indicates buggy code, e.g.
61
// InputStream in = null;
62
// try {
63
// in = new FileInputStream(...);
64
// } catch (IOException e) {
65
// in.close(); // in might be null!
66
// }
67
// if (count <= 0)
68
// throw new NonexistentObligationException(obligation);
69

70         invalidate();
71         countList[obligation.getId()] = (short)(count - 1);
72     }
73     
74     public int getCount(int id) {
75         return countList[id];
76     }
77
78 // public int getCount(Obligation obligation) {
79
// return getCount(obligation.getId());
80
// }
81

82     @Override JavaDoc
83     public boolean equals(Object JavaDoc o) {
84         if (o == null || o.getClass() != this.getClass())
85             return false;
86
87         ObligationSet other = (ObligationSet) o;
88
89         if (this.countList.length != other.countList.length)
90             return false;
91
92         for (int i = 0; i < this.countList.length; ++i) {
93             if (this.countList[i] != other.countList[i])
94                 return false;
95         }
96
97         return true;
98     }
99     
100     @Override JavaDoc
101     public String JavaDoc toString() {
102         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
103         buf.append("{");
104         int count = 0;
105         for (int i = 0; i < countList.length; ++i) {
106             if (countList[i] == 0)
107                 continue;
108             if (count > 0)
109                 buf.append(",");
110             buf.append(factory.getObligationById(i).toString());
111             buf.append("*");
112             buf.append(countList[i]);
113             ++count;
114         }
115         buf.append("}");
116         return buf.toString();
117     }
118     
119     public ObligationSet duplicate() {
120         ObligationSet dup = new ObligationSet(countList.length, factory);
121         System.arraycopy(this.countList, 0, dup.countList, 0, countList.length);
122         return dup;
123     }
124
125     @Override JavaDoc
126     public int hashCode() {
127         if (cachedHashCode == INVALID_HASH_CODE) {
128             int value = 0;
129             for (int i = 0; i < countList.length; ++i) {
130                 value += (13 * i * countList[i]);
131             }
132             cachedHashCode = value;
133         }
134         return cachedHashCode;
135     }
136
137     private void invalidate() {
138         this.cachedHashCode = INVALID_HASH_CODE;
139     }
140 }
141
142 // vim:ts=4
143
Popular Tags