changes: make location an extension of type descriptor and have an additional mapping...
[IRC.git] / Robust / src / Analysis / SSJava / CompositeLocation.java
1 package Analysis.SSJava;
2
3 import java.util.HashSet;
4 import java.util.Hashtable;
5 import java.util.Iterator;
6 import java.util.Map;
7 import java.util.Set;
8
9 import IR.ClassDescriptor;
10
11 public class CompositeLocation extends Location{
12
13   protected NTuple<Location> locTuple;
14
15   public CompositeLocation(ClassDescriptor cd) {
16     super(cd);
17     locTuple = new NTuple<Location>();
18   }
19
20   public NTuple<Location> getTuple() {
21     return locTuple;
22   }
23
24   public int getBaseLocationSize() {
25     return getBaseLocationSet().size();
26   }
27
28   public void addLocation(Location loc) {
29
30     if (loc instanceof DeltaLocation) {
31       type = Location.DELTA;
32     }
33
34     locTuple.addElement(loc);
35
36   }
37
38   public void addLocationSet(Set<Location> set) {
39
40     for (Iterator iterator = set.iterator(); iterator.hasNext();) {
41       Location location = (Location) iterator.next();
42       locTuple.addElement(location);
43     }
44
45   }
46
47   public Location getLocation(ClassDescriptor cd) {
48
49     // need to get more optimization version later
50     Set<Location> locSet = getBaseLocationSet();
51     for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
52       Location location = (Location) iterator.next();
53       if (location.getClassDescriptor().equals(cd)) {
54         return location;
55       }
56     }
57
58     return null;
59
60   }
61
62   public Map<ClassDescriptor, Location> getCd2Loc() {
63
64     Map<ClassDescriptor, Location> cd2loc = new Hashtable<ClassDescriptor, Location>();
65
66     Set<Location> baseLocSet = getBaseLocationSet();
67     for (Iterator iterator = baseLocSet.iterator(); iterator.hasNext();) {
68       Location location = (Location) iterator.next();
69       cd2loc.put(location.getClassDescriptor(), location);
70     }
71
72     return cd2loc;
73
74   }
75
76   public NTuple<Location> getBaseLocationTuple() {
77
78     NTuple<Location> baseLocationTuple = new NTuple<Location>();
79     int tupleSize = locTuple.size();
80     for (int i = 0; i < tupleSize; i++) {
81       Location locElement = locTuple.at(i);
82
83       if (locElement instanceof DeltaLocation) {
84         // baseLocationSet.addAll(((DeltaLocation)
85         // locElement).getDeltaOperandLocationVec());
86         baseLocationTuple.addAll(((DeltaLocation) locElement).getBaseLocationTuple());
87       } else {
88         baseLocationTuple.addElement(locElement);
89       }
90     }
91     return baseLocationTuple;
92
93   }
94
95   public Set<Location> getBaseLocationSet() {
96
97     Set<Location> baseLocationSet = new HashSet<Location>();
98     int tupleSize = locTuple.size();
99     for (int i = 0; i < tupleSize; i++) {
100       Location locElement = locTuple.at(i);
101
102       if (locElement instanceof DeltaLocation) {
103         // baseLocationSet.addAll(((DeltaLocation)
104         // locElement).getDeltaOperandLocationVec());
105         baseLocationSet.addAll(((DeltaLocation) locElement).getBaseLocationSet());
106       } else {
107         baseLocationSet.add(locElement);
108       }
109     }
110     return baseLocationSet;
111   }
112
113   public int getNumofDelta() {
114
115     int result = 0;
116
117     if (locTuple.size() == 1) {
118       Location locElement = locTuple.at(0);
119       if (locElement instanceof DeltaLocation) {
120         result++;
121         result += getNumofDelta((DeltaLocation) locElement);
122       }
123     }
124     return result;
125   }
126
127   public int getNumofDelta(DeltaLocation delta) {
128     int result = 0;
129
130     if (delta.getDeltaOperandLocationVec().size() == 1) {
131       Location locElement = delta.getDeltaOperandLocationVec().at(0);
132       if (locElement instanceof DeltaLocation) {
133         result++;
134         result += getNumofDelta((DeltaLocation) locElement);
135       }
136     }
137
138     return result;
139   }
140
141   public String toString() {
142
143     // for better representation
144     // if compositeLoc has only one single location,
145     // just print out single location
146     // if(locTuple.size()==1){
147     // Location locElement=locTuple.at(0);
148     // if(locElement instanceof Location){
149     // return locElement.toString();
150     // }
151     // }
152
153     String rtr = "CompLoc[";
154
155     int tupleSize = locTuple.size();
156     for (int i = 0; i < tupleSize; i++) {
157       Location locElement = locTuple.at(i);
158       if (i != 0) {
159         rtr += ",";
160       }
161       rtr += locElement;
162     }
163     rtr += "]";
164
165     return rtr;
166   }
167
168   public boolean equals(Object o) {
169
170     if (!(o instanceof CompositeLocation)) {
171       return false;
172     }
173
174     CompositeLocation compLoc = (CompositeLocation) o;
175
176     if (compLoc.getClassDescriptor().equals(getClassDescriptor())
177         && compLoc.getTuple().equals(getTuple())) {
178       return true;
179     } else {
180       return false;
181     }
182
183   }
184
185   public int hashCode() {
186
187     int hashCode = getClassDescriptor().hashCode();
188     return hashCode + locTuple.hashCode();
189
190   }
191
192 }