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;
28 * Creates a new flagstate with all flags set to false.
29 * @param cd ClassDescriptor
31 public FlagState(ClassDescriptor cd) {
32 this.flagstate=new HashSet();
34 this.tags=new Hashtable<TagDescriptor,Integer>();
35 this.uid=FlagState.nodeid++;
36 this.issourcenode=false;
40 * Creates a new flagstate with flags set according to the HashSet.
41 * If the flag exists in the hashset, it's set to true else set to false.
42 * @param cd ClassDescriptor
43 * @param flagstate a <CODE>HashSet</CODE> containing FlagDescriptors
45 private FlagState(HashSet flagstate, ClassDescriptor cd,Hashtable<TagDescriptor,Integer> tags) {
46 this.flagstate=flagstate;
49 this.uid=FlagState.nodeid++;
50 this.issourcenode=false;
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);
66 /** Checks if the flagstate is a source node.
67 * @return true if the flagstate is a sourcenode(i.e. Is the product of an allocation site).
70 public boolean isSourceNode(){
74 /** Sets the flagstate as a source node.
76 public void setAsSourceNode(){
79 this.tasks=new Vector();
83 public void addAllocatingTask(TaskDescriptor task){
87 public Vector getAllocatingTasks(){
92 public String toString() {
93 return cd.toString()+getTextLabel();
96 /** @return Iterator over the flags in the flagstate.
99 public Iterator getFlags() {
100 return flagstate.iterator();
103 public int numFlags(){
104 return flagstate.size();
107 public FlagState[] setTag(TagDescriptor tag){
108 HashSet newset1=(HashSet)flagstate.clone();
109 Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
111 if (tags.containsKey(tag)){
112 //Code could try to remove flag that doesn't exist
114 switch (tags.get(tag).intValue()){
116 newtags1.put(tag,new Integer(MULTITAGS));
117 return new FlagState[] {this, new FlagState(newset1, cd, newtags1)};
119 return new FlagState[] {this};
124 newtags1.put(tag,new Integer(ONETAG));
125 return new FlagState[] {new FlagState(newset1,cd,newtags1)};
129 public int getTagCount(TagDescriptor tag) {
130 if (tags.containsKey(tag))
131 return tags.get(tag).intValue();
135 public int getTagCount(String tagtype){
136 return getTagCount(new TagDescriptor(tagtype));
139 public FlagState[] clearTag(TagDescriptor tag){
140 if (tags.containsKey(tag)){
141 switch(tags.get(tag).intValue()){
143 HashSet newset=(HashSet)flagstate.clone();
144 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
146 return new FlagState[]{new FlagState(newset,cd,newtags)};
149 //two possibilities - count remains 2 or becomes 1
151 HashSet newset1=(HashSet)flagstate.clone();
152 Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
155 HashSet newset2=(HashSet)flagstate.clone();
156 Hashtable<TagDescriptor,Integer> newtags2=(Hashtable<TagDescriptor,Integer>)tags.clone();
157 newtags1.put(tag,new Integer(ONETAG));
158 return new FlagState[] {new FlagState(newset1, cd, newtags2),
159 new FlagState(newset2, cd, newtags2)};
164 throw new Error("Invalid Operation: Can not clear a tag that doesn't exist.");
168 /** Creates a string description of the flagstate
169 * e.g. a flagstate with five flags could look like 01001
170 * @param flags an array of flagdescriptors.
171 * @return string representation of the flagstate.
173 public String toString(FlagDescriptor[] flags)
175 StringBuffer sb = new StringBuffer(flagstate.size());
176 for(int i=0;i < flags.length; i++)
184 return new String(sb);
188 * @return returns the classdescriptor of the flagstate.
191 public ClassDescriptor getClassDescriptor(){
195 /** Sets the status of a specific flag in a flagstate after cloning it.
196 * @param fd FlagDescriptor of the flag whose status is being set.
197 * @param status boolean value
198 * @return the new flagstate with <CODE>fd</CODE> set to <CODE>status</CODE>.
201 public FlagState setFlag(FlagDescriptor fd, boolean status) {
202 HashSet newset=(HashSet) flagstate.clone();
203 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
206 else if (newset.contains(fd)){
210 return new FlagState(newset, cd, newtags);
213 /** Tests for equality of two flagstate objects.
216 public boolean equals(Object o) {
217 if (o instanceof FlagState) {
218 FlagState fs=(FlagState)o;
221 return (fs.flagstate.equals(flagstate) & fs.tags.equals(tags));
226 public int hashCode() {
227 return cd.hashCode()^flagstate.hashCode()^tags.hashCode();
230 public String getLabel() {
234 public String getTextLabel() {
236 for(Iterator it=getFlags();it.hasNext();) {
237 FlagDescriptor fd=(FlagDescriptor) it.next();
241 label+=", "+fd.toString();
243 for (Enumeration en_tags=getTags();en_tags.hasMoreElements();){
244 TagDescriptor td=(TagDescriptor)en_tags.nextElement();
245 switch (tags.get(td).intValue()){
248 label=td.toString()+"(1)";
250 label+=", "+td.toString()+"(1)";
254 label=td.toString()+"(n)";
256 label+=", "+td.toString()+"(n)";
267 public Enumeration getTags(){