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;
24 private boolean issourcenode;
27 * Creates a new flagstate with all flags set to false.
28 * @param cd ClassDescriptor
30 public FlagState(ClassDescriptor cd) {
31 this.flagstate=new HashSet();
33 this.tags=new Hashtable<TagDescriptor,Integer>();
34 this.uid=FlagState.nodeid++;
35 this.issourcenode=false;
39 * Creates a new flagstate with flags set according to the HashSet.
40 * If the flag exists in the hashset, it's set to true else set to false.
41 * @param cd ClassDescriptor
42 * @param flagstate a <CODE>HashSet</CODE> containing FlagDescriptors
44 private FlagState(HashSet flagstate, ClassDescriptor cd,Hashtable<TagDescriptor,Integer> tags) {
45 this.flagstate=flagstate;
48 this.uid=FlagState.nodeid++;
49 this.issourcenode=false;
54 * @param fd FlagDescriptor
55 * @return true if the flagstate contains fd else false.
57 public boolean get(FlagDescriptor fd) {
58 return flagstate.contains(fd);
61 /** Checks if the flagstate is a source node.
62 * @return true if the flagstate is a sourcenode(i.e. Is the product of an allocation site).
65 public boolean isSourceNode(){
69 /** Sets the flagstate as a source node.
71 public void setAsSourceNode(){
76 public String toString() {
77 return cd.toString()+getTextLabel();
80 /** @return Iterator over the flags in the flagstate.
83 public Iterator getFlags() {
84 return flagstate.iterator();
87 public FlagState setTag(TagDescriptor tag){
89 HashSet newset=(HashSet)flagstate.clone();
90 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
92 if (newtags.containsKey(tag)){
93 switch (newtags.get(tag).intValue()){
95 newtags.put(tag,new Integer(MULTITAGS));
98 newtags.put(tag,new Integer(MULTITAGS));
103 newtags.put(tag,new Integer(ONETAG));
106 return new FlagState(newset,cd,newtags);
110 public int getTagCount(String tagtype){
111 for (Enumeration en=getTags();en.hasMoreElements();){
112 TagDescriptor td=(TagDescriptor)en.nextElement();
113 if (tagtype.equals(td.getSymbol()))
114 return tags.get(td).intValue(); //returns either ONETAG or MULTITAG
120 public FlagState[] clearTag(TagDescriptor tag){
121 FlagState[] retstates;
123 if (tags.containsKey(tag)){
124 switch(tags.get(tag).intValue()){
126 HashSet newset=(HashSet)flagstate.clone();
127 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
129 retstates=new FlagState[]{new FlagState(newset,cd,newtags)};
133 //when tagcount is more than 2, COUNT stays at MULTITAGS
134 retstates=new FlagState[2];
135 HashSet newset1=(HashSet)flagstate.clone();
136 Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
137 retstates[1]=new FlagState(newset1,cd,newtags1);
138 //when tagcount is 2, COUNT changes to ONETAG
139 HashSet newset2=(HashSet)flagstate.clone();
140 Hashtable<TagDescriptor,Integer> newtags2=(Hashtable<TagDescriptor,Integer>)tags.clone();
141 newtags1.put(tag,new Integer(ONETAG));
142 retstates[1]=new FlagState(newset2,cd,newtags2);
148 throw new Error("Invalid Operation: Can not clear a tag that doesn't exist.");
154 /** Creates a string description of the flagstate
155 * e.g. a flagstate with five flags could look like 01001
156 * @param flags an array of flagdescriptors.
157 * @return string representation of the flagstate.
159 public String toString(FlagDescriptor[] flags)
161 StringBuffer sb = new StringBuffer(flagstate.size());
162 for(int i=0;i < flags.length; i++)
170 return new String(sb);
174 * @return returns the classdescriptor of the flagstate.
177 public ClassDescriptor getClassDescriptor(){
181 /** Sets the status of a specific flag in a flagstate after cloning it.
182 * @param fd FlagDescriptor of the flag whose status is being set.
183 * @param status boolean value
184 * @return the new flagstate with <CODE>fd</CODE> set to <CODE>status</CODE>.
187 public FlagState setFlag(FlagDescriptor fd, boolean status) {
188 HashSet newset=(HashSet) flagstate.clone();
189 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
192 else if (newset.contains(fd)){
196 return new FlagState(newset, cd,newtags);
199 /** Tests for equality of two flagstate objects.
202 public boolean equals(Object o) {
203 if (o instanceof FlagState) {
204 FlagState fs=(FlagState)o;
207 return (fs.flagstate.equals(flagstate) & fs.tags.equals(tags));
212 public int hashCode() {
213 return cd.hashCode()^flagstate.hashCode()^tags.hashCode();
216 public String getLabel() {
220 public String getTextLabel() {
222 for(Iterator it=getFlags();it.hasNext();) {
223 FlagDescriptor fd=(FlagDescriptor) it.next();
227 label+=", "+fd.toString();
229 for (Enumeration en_tags=getTags();en_tags.hasMoreElements();){
230 TagDescriptor td=(TagDescriptor)en_tags.nextElement();
231 switch (tags.get(td).intValue()){
234 label=td.toString()+"(1)";
236 label+=", "+td.toString()+"(1)";
240 label=td.toString()+"(n)";
242 label+=", "+td.toString()+"(n)";
253 public Enumeration getTags(){