| 1 19 20 package edu.umd.cs.findbugs.ba.obl; 21 22 23 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 70 invalidate(); 71 countList[obligation.getId()] = (short)(count - 1); 72 } 73 74 public int getCount(int id) { 75 return countList[id]; 76 } 77 78 82 @Override  83 public boolean equals(Object 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  101 public String toString() { 102 StringBuffer buf = new StringBuffer (); 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  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 | Popular Tags |