changes.
[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 Set<Location> getBaseLocationSet() {
77
78     Set<Location> baseLocationSet = new HashSet<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         baseLocationSet.addAll(((DeltaLocation) locElement).getBaseLocationSet());
87       } else {
88         baseLocationSet.add(locElement);
89       }
90     }
91     return baseLocationSet;
92   }
93
94   public int getNumofDelta() {
95
96     int result = 0;
97
98     if (locTuple.size() == 1) {
99       Location locElement = locTuple.at(0);
100       if (locElement instanceof DeltaLocation) {
101         result++;
102         result += getNumofDelta((DeltaLocation) locElement);
103       }
104     }
105     return result;
106   }
107
108   public int getNumofDelta(DeltaLocation delta) {
109     int result = 0;
110
111     if (delta.getDeltaOperandLocationVec().size() == 1) {
112       Location locElement = delta.getDeltaOperandLocationVec().at(0);
113       if (locElement instanceof DeltaLocation) {
114         result++;
115         result += getNumofDelta((DeltaLocation) locElement);
116       }
117     }
118
119     return result;
120   }
121
122   public String toString() {
123
124     // for better representation
125     // if compositeLoc has only one single location,
126     // just print out single location
127     // if(locTuple.size()==1){
128     // Location locElement=locTuple.at(0);
129     // if(locElement instanceof Location){
130     // return locElement.toString();
131     // }
132     // }
133
134     String rtr = "CompLoc[";
135
136     int tupleSize = locTuple.size();
137     for (int i = 0; i < tupleSize; i++) {
138       Location locElement = locTuple.at(i);
139       if (i != 0) {
140         rtr += ",";
141       }
142       rtr += locElement;
143     }
144     rtr += "]";
145
146     return rtr;
147   }
148
149   public boolean equals(Object o) {
150
151     if (!(o instanceof CompositeLocation)) {
152       return false;
153     }
154
155     CompositeLocation compLoc = (CompositeLocation) o;
156
157     if (compLoc.getClassDescriptor().equals(getClassDescriptor())
158         && compLoc.getTuple().equals(getTuple())) {
159       return true;
160     } else {
161       return false;
162     }
163
164   }
165
166   public int hashCode() {
167
168     int hashCode = getClassDescriptor().hashCode();
169     return hashCode + locTuple.hashCode();
170
171   }
172
173 }