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