1 19 20 package edu.umd.cs.findbugs.ba.obl; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 41 public class StateSet { 42 public interface StateCallback { 43 public void apply(State state) throws NonexistentObligationException; 44 } 45 46 private boolean isTop; 47 private boolean isBottom; 48 private Map <ObligationSet, State> stateMap; 49 private ObligationFactory factory; 50 51 public StateSet(ObligationFactory factory) { 52 this.isTop = this.isBottom = false; 53 this.stateMap = new HashMap <ObligationSet, State>(); 54 this.factory = factory; 55 } 56 57 public void setTop() { 58 this.isTop = true; 59 this.isBottom = false; 60 } 61 62 public boolean isTop() { 63 return isTop; 64 } 65 66 public void setBottom() { 67 this.isBottom = true; 68 this.isTop = false; 69 } 70 71 public boolean isBottom() { 72 return this.isBottom; 73 } 74 75 public boolean isValid() { 76 return !this.isTop && !this.isBottom; 77 } 78 79 84 public Iterator <State> stateIterator() { 85 return stateMap.values().iterator(); 86 } 87 88 95 public State getStateWithObligationSet(ObligationSet obligationSet) { 96 return stateMap.get(obligationSet); 97 } 98 99 105 public void initEntryFact(ObligationFactory factory) { 106 this.isTop = this.isBottom = false; 107 this.stateMap.clear(); 108 109 State initState = new State(factory.getMaxObligationTypes(), factory); 111 this.stateMap.put(initState.getObligationSet(), initState); 112 } 113 114 119 public void copyFrom(StateSet other) { 120 this.isTop = other.isTop; 121 this.isBottom = other.isBottom; 122 this.stateMap.clear(); 123 for (State state : other.stateMap.values()) { 124 State dup = state.duplicate(); 125 this.stateMap.put(dup.getObligationSet(), dup); 126 } 127 } 128 129 134 public StateSet duplicate() { 135 StateSet dup = new StateSet(factory); 136 dup.copyFrom(this); 137 return dup; 138 } 139 140 145 public void addObligation(final Obligation obligation) { 146 final Map <ObligationSet, State> updatedStateMap = 147 new HashMap <ObligationSet, State>(); 148 149 try { 150 applyToAllStatesAndUpdateMap(new StateCallback() { 151 public void apply(State state) { 152 state.getObligationSet().add(obligation); 153 updatedStateMap.put(state.getObligationSet(), state); 154 } 155 }, updatedStateMap); 156 } catch (NonexistentObligationException e) { 157 } 159 } 160 161 167 public void deleteObligation(final Obligation obligation) 168 throws NonexistentObligationException { 169 final Map <ObligationSet, State> updatedStateMap = 170 new HashMap <ObligationSet, State>(); 171 172 applyToAllStatesAndUpdateMap(new StateCallback() { 173 176 public void apply(State state) 177 throws NonexistentObligationException { 178 state.getObligationSet().remove(obligation); 179 updatedStateMap.put(state.getObligationSet(), state); 180 } 181 }, updatedStateMap); 182 } 183 184 @Override 185 public boolean equals(Object o) { 186 if (o == null || o.getClass() != this.getClass()) 187 return false; 188 StateSet other = (StateSet) o; 189 return this.isTop == other.isTop 190 && this.isBottom == other.isBottom 191 && this.stateMap.equals(other.stateMap); 192 } 193 194 @Override 195 public int hashCode() { 196 throw new UnsupportedOperationException (); 197 } 198 199 @Override 200 public String toString() { 201 if (isTop) 202 return "TOP"; 203 else if (isBottom) 204 return "BOTTOM"; 205 else { 206 StringBuffer buf = new StringBuffer (); 207 boolean first = true; 208 for (Iterator <State> i = stateIterator(); i.hasNext();) { 209 State state = i.next(); 210 if (first) 211 first = false; 212 else 213 buf.append(","); 214 buf.append(state.toString()); 215 } 216 return buf.toString(); 217 } 218 } 219 220 224 public Map <ObligationSet, State> createEmptyMap() { 225 return new HashMap <ObligationSet, State>(); 226 } 227 228 236 public void applyToAllStatesAndUpdateMap(StateCallback callback, 237 Map <ObligationSet, State> updatedStateMap) 238 throws NonexistentObligationException { 239 applyToAllStates(callback); 240 this.stateMap = updatedStateMap; 241 } 242 243 248 public void applyToAllStates(StateCallback callback) 249 throws NonexistentObligationException { 250 for (State state : stateMap.values()) { 251 callback.apply(state); 252 } 253 } 254 } 255 256 | Popular Tags |