1 package Analysis.TaskStateAnalysis;
2 import Analysis.TaskStateAnalysis.*;
10 /** This class is used to hold the flag states that a class in the Bristlecone
11 * program can exist in, during runtime.
13 public class FlagState extends GraphNode {
14 public static final int ONETAG=1;
15 public static final int NOTAGS=0;
16 public static final int MULTITAGS=-1;
19 private static int nodeid=0;
21 private final HashSet flagstate;
22 private final ClassDescriptor cd;
23 private final Hashtable<TagDescriptor,Integer> tags;
26 * Creates a new flagstate with all flags set to false.
27 * @param cd ClassDescriptor
29 public FlagState(ClassDescriptor cd) {
30 this.flagstate=new HashSet();
32 this.tags=new Hashtable<TagDescriptor,Integer>();
33 this.uid=FlagState.nodeid++;
37 * Creates a new flagstate with flags set according to the HashSet.
38 * If the flag exists in the hashset, it's set to true else set to false.
39 * @param cd ClassDescriptor
40 * @param flagstate a <CODE>HashSet</CODE> containing FlagDescriptors
42 private FlagState(HashSet flagstate, ClassDescriptor cd,Hashtable<TagDescriptor,Integer> tags) {
43 this.flagstate=flagstate;
46 this.uid=FlagState.nodeid++;
51 * @param fd FlagDescriptor
52 * @return true if the flagstate contains fd else false.
54 public boolean get(FlagDescriptor fd) {
55 return flagstate.contains(fd);
59 public String toString() {
60 return cd.toString()+getTextLabel();
63 /** @return Iterator over the flags in the flagstate.
66 public Iterator getFlags() {
67 return flagstate.iterator();
70 public FlagState setTag(TagDescriptor tag){
72 HashSet newset=(HashSet)flagstate.clone();
73 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
75 if (newtags.containsKey(tag)){
76 switch (newtags.get(tag).intValue()){
78 newtags.put(tag,new Integer(MULTITAGS));
81 newtags.put(tag,new Integer(MULTITAGS));
86 newtags.put(tag,new Integer(ONETAG));
89 return new FlagState(newset,cd,newtags);
93 public int getTagCount(String tagtype){
94 for (Enumeration en=getTags();en.hasMoreElements();){
95 TagDescriptor td=(TagDescriptor)en.nextElement();
96 if (tagtype.equals(td.getSymbol()))
97 return tags.get(td).intValue(); //returns either ONETAG or MULTITAG
103 public FlagState[] clearTag(TagDescriptor tag){
104 FlagState[] retstates;
106 if (tags.containsKey(tag)){
107 switch(tags.get(tag).intValue()){
109 HashSet newset=(HashSet)flagstate.clone();
110 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
112 retstates=new FlagState[]{new FlagState(newset,cd,newtags)};
116 //when tagcount is more than 2, COUNT stays at MULTITAGS
117 retstates=new FlagState[2];
118 HashSet newset1=(HashSet)flagstate.clone();
119 Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
120 retstates[1]=new FlagState(newset1,cd,newtags1);
121 //when tagcount is 2, COUNT changes to ONETAG
122 HashSet newset2=(HashSet)flagstate.clone();
123 Hashtable<TagDescriptor,Integer> newtags2=(Hashtable<TagDescriptor,Integer>)tags.clone();
124 newtags1.put(tag,new Integer(ONETAG));
125 retstates[1]=new FlagState(newset2,cd,newtags2);
131 throw new Error("Invalid Operation: Can not clear a tag that doesn't exist.");
137 /** Creates a string description of the flagstate
138 * e.g. a flagstate with five flags could look like 01001
139 * @param flags an array of flagdescriptors.
140 * @return string representation of the flagstate.
142 public String toString(FlagDescriptor[] flags)
144 StringBuffer sb = new StringBuffer(flagstate.size());
145 for(int i=0;i < flags.length; i++)
153 return new String(sb);
157 * @return returns the classdescriptor of the flagstate.
160 public ClassDescriptor getClassDescriptor(){
164 /** Sets the status of a specific flag in a flagstate after cloning it.
165 * @param fd FlagDescriptor of the flag whose status is being set.
166 * @param status boolean value
167 * @return the new flagstate with <CODE>fd</CODE> set to <CODE>status</CODE>.
170 public FlagState setFlag(FlagDescriptor fd, boolean status) {
171 HashSet newset=(HashSet) flagstate.clone();
172 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
175 else if (newset.contains(fd)){
179 return new FlagState(newset, cd,newtags);
182 /** Tests for equality of two flagstate objects.
185 public boolean equals(Object o) {
186 if (o instanceof FlagState) {
187 FlagState fs=(FlagState)o;
190 return (fs.flagstate.equals(flagstate) & fs.tags.equals(tags));
195 public int hashCode() {
196 return cd.hashCode()^flagstate.hashCode()^tags.hashCode();
199 public String getLabel() {
203 public String getTextLabel() {
205 for(Iterator it=getFlags();it.hasNext();) {
206 FlagDescriptor fd=(FlagDescriptor) it.next();
210 label+=", "+fd.toString();
212 for (Enumeration en_tags=getTags();en_tags.hasMoreElements();){
213 TagDescriptor td=(TagDescriptor)en_tags.nextElement();
214 switch (tags.get(td).intValue()){
217 label=td.toString()+"(1)";
219 label+=", "+td.toString()+"(1)";
223 label=td.toString()+"(n)";
225 label+=", "+td.toString()+"(n)";
236 public Enumeration getTags(){