changes: now, the annotated SSJava class library passes the flow-down rule checking.
[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     locTuple.addElement(loc);
34
35   }
36
37   public void addLocationSet(Set<Location> set) {
38
39     for (Iterator iterator = set.iterator(); iterator.hasNext();) {
40       Location location = (Location) iterator.next();
41       locTuple.addElement(location);
42     }
43
44   }
45
46   public Location getLocation(ClassDescriptor cd) {
47
48     // need to get more optimization version later
49     Set<Location> locSet = getBaseLocationSet();
50     for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
51       Location location = (Location) iterator.next();
52       if (location.getClassDescriptor().equals(cd)) {
53         return location;
54       }
55     }
56
57     return null;
58
59   }
60
61   public Map<ClassDescriptor, Location> getCd2Loc() {
62
63     Map<ClassDescriptor, Location> cd2loc = new Hashtable<ClassDescriptor, Location>();
64
65     Set<Location> baseLocSet = getBaseLocationSet();
66     for (Iterator iterator = baseLocSet.iterator(); iterator.hasNext();) {
67       Location location = (Location) iterator.next();
68       cd2loc.put(location.getClassDescriptor(), location);
69     }
70
71     return cd2loc;
72
73   }
74
75   public NTuple<Location> getBaseLocationTuple() {
76
77     NTuple<Location> baseLocationTuple = new NTuple<Location>();
78     int tupleSize = locTuple.size();
79     for (int i = 0; i < tupleSize; i++) {
80       Location locElement = locTuple.at(i);
81
82       if (locElement instanceof DeltaLocation) {
83         // baseLocationSet.addAll(((DeltaLocation)
84         // locElement).getDeltaOperandLocationVec());
85         baseLocationTuple.addAll(((DeltaLocation) locElement).getBaseLocationTuple());
86       } else {
87         baseLocationTuple.addElement(locElement);
88       }
89     }
90     return baseLocationTuple;
91
92   }
93
94   public Set<Location> getBaseLocationSet() {
95
96     Set<Location> baseLocationSet = new HashSet<Location>();
97     int tupleSize = locTuple.size();
98     for (int i = 0; i < tupleSize; i++) {
99       Location locElement = locTuple.at(i);
100
101       if (locElement instanceof DeltaLocation) {
102         // baseLocationSet.addAll(((DeltaLocation)
103         // locElement).getDeltaOperandLocationVec());
104         baseLocationSet.addAll(((DeltaLocation) locElement).getBaseLocationSet());
105       } else {
106         baseLocationSet.add(locElement);
107       }
108     }
109     return baseLocationSet;
110   }
111
112   public int getNumofDelta() {
113
114     int result = 0;
115
116     if (locTuple.size() == 1) {
117       Location locElement = locTuple.at(0);
118       if (locElement instanceof DeltaLocation) {
119         result++;
120         result += getNumofDelta((DeltaLocation) locElement);
121       }
122     }
123     return result;
124   }
125
126   public int getNumofDelta(DeltaLocation delta) {
127     int result = 0;
128
129     if (delta.getDeltaOperandLocationVec().size() == 1) {
130       Location locElement = delta.getDeltaOperandLocationVec().at(0);
131       if (locElement instanceof DeltaLocation) {
132         result++;
133         result += getNumofDelta((DeltaLocation) locElement);
134       }
135     }
136
137     return result;
138   }
139
140   public void removieLocation(ClassDescriptor cd) {
141     for (int i = 0; i < locTuple.size(); i++) {
142       if (locTuple.at(i).getClassDescriptor().equals(cd)) {
143         locTuple.removeAt(i);
144         return;
145       }
146     }
147   }
148
149   public String toString() {
150
151     // for better representation
152     // if compositeLoc has only one single location,
153     // just print out single location
154     // if(locTuple.size()==1){
155     // Location locElement=locTuple.at(0);
156     // if(locElement instanceof Location){
157     // return locElement.toString();
158     // }
159     // }
160
161     String rtr = "CompLoc[";
162
163     int tupleSize = locTuple.size();
164     for (int i = 0; i < tupleSize; i++) {
165       Location locElement = locTuple.at(i);
166       if (i != 0) {
167         rtr += ",";
168       }
169       rtr += locElement;
170     }
171     rtr += "]";
172
173     return rtr;
174   }
175
176   public boolean equals(Object o) {
177
178     if (!(o instanceof CompositeLocation)) {
179       return false;
180     }
181
182     CompositeLocation compLoc = (CompositeLocation) o;
183
184     if (compLoc.getClassDescriptor().equals(getClassDescriptor())
185         && compLoc.getTuple().equals(getTuple())) {
186       return true;
187     } else {
188       return false;
189     }
190
191   }
192
193   public int hashCode() {
194
195     int hashCode = getClassDescriptor().hashCode();
196     return hashCode + locTuple.hashCode();
197
198   }
199
200 }