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;
25 public void setOption(String option) {
26 this.nodeoption=","+option;
29 public void setMerge() {
34 * Creates a new flagstate with all flags set to false.
35 * @param cd ClassDescriptor
37 public FlagState(ClassDescriptor cd) {
38 this.flagstate=new HashSet();
40 this.tags=new Hashtable<TagDescriptor,Integer>();
41 this.uid=FlagState.nodeid++;
45 * Creates a new flagstate with flags set according to the HashSet.
46 * If the flag exists in the hashset, it's set to true else set to false.
47 * @param cd ClassDescriptor
48 * @param flagstate a <CODE>HashSet</CODE> containing FlagDescriptors
50 private FlagState(HashSet flagstate, ClassDescriptor cd,Hashtable<TagDescriptor,Integer> tags) {
51 this.flagstate=flagstate;
54 this.uid=FlagState.nodeid++;
59 * @param fd FlagDescriptor
60 * @return true if the flagstate contains fd else false.
62 public boolean get(FlagDescriptor fd) {
63 return flagstate.contains(fd);
67 public String toString() {
68 return cd.toString()+getTextLabel();
71 /** @return Iterator over the flags in the flagstate.
74 public Iterator getFlags() {
75 return flagstate.iterator();
78 public FlagState setTag(TagDescriptor tag){
80 HashSet newset=(HashSet)flagstate.clone();
81 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
83 if (newtags.containsKey(tag)){
84 switch (newtags.get(tag).intValue()){
86 newtags.put(tag,new Integer(MULTITAGS));
89 newtags.put(tag,new Integer(MULTITAGS));
94 newtags.put(tag,new Integer(ONETAG));
97 return new FlagState(newset,cd,newtags);
101 public int getTagCount(String tagtype){
102 for (Enumeration en=getTags();en.hasMoreElements();){
103 TagDescriptor td=(TagDescriptor)en.nextElement();
104 if (tagtype.equals(td.getSymbol()))
105 return tags.get(td).intValue(); //returns either ONETAG or MULTITAG
111 public FlagState[] clearTag(TagDescriptor tag){
112 FlagState[] retstates;
114 if (tags.containsKey(tag)){
115 switch(tags.get(tag).intValue()){
117 HashSet newset=(HashSet)flagstate.clone();
118 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
120 retstates=new FlagState[]{new FlagState(newset,cd,newtags)};
124 //when tagcount is more than 2, COUNT stays at MULTITAGS
125 retstates=new FlagState[2];
126 HashSet newset1=(HashSet)flagstate.clone();
127 Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
128 retstates[1]=new FlagState(newset1,cd,newtags1);
129 //when tagcount is 2, COUNT changes to ONETAG
130 HashSet newset2=(HashSet)flagstate.clone();
131 Hashtable<TagDescriptor,Integer> newtags2=(Hashtable<TagDescriptor,Integer>)tags.clone();
132 newtags1.put(tag,new Integer(ONETAG));
133 retstates[1]=new FlagState(newset2,cd,newtags2);
139 throw new Error("Invalid Operation: Can not clear a tag that doesn't exist.");
145 /** Creates a string description of the flagstate
146 * e.g. a flagstate with five flags could look like 01001
147 * @param flags an array of flagdescriptors.
148 * @return string representation of the flagstate.
150 public String toString(FlagDescriptor[] flags)
152 StringBuffer sb = new StringBuffer(flagstate.size());
153 for(int i=0;i < flags.length; i++)
161 return new String(sb);
165 * @return returns the classdescriptor of the flagstate.
168 public ClassDescriptor getClassDescriptor(){
172 /** Sets the status of a specific flag in a flagstate after cloning it.
173 * @param fd FlagDescriptor of the flag whose status is being set.
174 * @param status boolean value
175 * @return the new flagstate with <CODE>fd</CODE> set to <CODE>status</CODE>.
178 public FlagState setFlag(FlagDescriptor fd, boolean status) {
179 HashSet newset=(HashSet) flagstate.clone();
180 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
183 else if (newset.contains(fd)){
187 return new FlagState(newset, cd,newtags);
190 /** Tests for equality of two flagstate objects.
193 public boolean equals(Object o) {
194 if (o instanceof FlagState) {
195 FlagState fs=(FlagState)o;
198 return (fs.flagstate.equals(flagstate) & fs.tags.equals(tags));
203 public int hashCode() {
204 return cd.hashCode()^flagstate.hashCode()^tags.hashCode();
207 public String getLabel() {
211 public String getTextLabel() {
213 for(Iterator it=getFlags();it.hasNext();) {
214 FlagDescriptor fd=(FlagDescriptor) it.next();
218 label+=", "+fd.toString();
220 for (Enumeration en_tags=getTags();en_tags.hasMoreElements();){
221 TagDescriptor td=(TagDescriptor)en_tags.nextElement();
222 switch (tags.get(td).intValue()){
224 label+=", "+td.toString()+"(1)";
227 label+=", "+td.toString()+"(n)";
236 public Enumeration getTags(){