bug fix on the glb function of the lattice + working on the checking with the composi...
[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   private Hashtable<ClassDescriptor, Location> cd2loc;
15   private int size;
16
17   public CompositeLocation(ClassDescriptor cd) {
18     super(cd);
19     locTuple = new NTuple<Location>();
20     cd2loc = new Hashtable<ClassDescriptor, Location>();
21     size = 0;
22   }
23
24   public NTuple<Location> getTuple() {
25     return locTuple;
26   }
27
28   public int getTupleSize() {
29     return size;
30   }
31
32   public void addLocation(Location loc) {
33     locTuple.addElement(loc);
34
35     if (loc instanceof DeltaLocation) {
36       DeltaLocation deltaLoc = (DeltaLocation) loc;
37       for (Iterator iterator = deltaLoc.getDeltaOperandLocationVec().iterator(); iterator.hasNext();) {
38         Location opLoc = (Location) iterator.next();
39         cd2loc.put(opLoc.getClassDescriptor(), opLoc);
40         size++;
41       }
42     } else {
43       cd2loc.put(loc.getClassDescriptor(), loc);
44       size += 1;
45     }
46   }
47
48   public Map<ClassDescriptor, Location> getCd2Loc() {
49     return cd2loc;
50   }
51
52   public Location getLocation(ClassDescriptor cd) {
53     return cd2loc.get(cd);
54   }
55
56   public Set<Location> getBaseLocationSet() {
57
58     Set<Location> baseLocationSet = new HashSet<Location>();
59
60     int tupleSize = locTuple.size();
61     for (int i = 0; i < tupleSize; i++) {
62       Location locElement = locTuple.at(i);
63
64       if (locElement instanceof DeltaLocation) {
65         baseLocationSet.addAll(((DeltaLocation) locElement).getDeltaOperandLocationVec());
66       } else {
67         baseLocationSet.add(locElement);
68       }
69     }
70     return baseLocationSet;
71   }
72
73   public int getNumofDelta() {
74
75     int result = 0;
76
77     if (locTuple.size() == 1) {
78       Location locElement = locTuple.at(0);
79       if (locElement instanceof DeltaLocation) {
80         result++;
81         result += getNumofDelta((DeltaLocation) locElement);
82       }
83     }
84     return result;
85   }
86
87   public int getNumofDelta(DeltaLocation delta) {
88     int result = 0;
89
90     if (delta.getDeltaOperandLocationVec().size() == 1) {
91       Location locElement = delta.getDeltaOperandLocationVec().get(0);
92       if (locElement instanceof DeltaLocation) {
93         result++;
94         result += getNumofDelta((DeltaLocation) locElement);
95       }
96     }
97
98     return result;
99   }
100
101   public String toString() {
102
103     // for better representation
104     // if compositeLoc has only one single location,
105     // just print out single location
106     // if(locTuple.size()==1){
107     // Location locElement=locTuple.at(0);
108     // if(locElement instanceof Location){
109     // return locElement.toString();
110     // }
111     // }
112
113     String rtr = "CompLoc[";
114
115     int tupleSize = locTuple.size();
116     for (int i = 0; i < tupleSize; i++) {
117       Location locElement = locTuple.at(i);
118       if (i != 0) {
119         rtr += ",";
120       }
121       rtr += locElement;
122     }
123     rtr += "]";
124
125     return rtr;
126   }
127
128   public boolean equals(Object o) {
129
130     if (!(o instanceof CompositeLocation)) {
131       return false;
132     }
133
134     CompositeLocation compLoc = (CompositeLocation) o;
135
136     if (compLoc.getClassDescriptor().equals(getClassDescriptor())
137         && compLoc.getTuple().equals(getTuple())) {
138       return true;
139     } else {
140       return false;
141     }
142
143   }
144
145   public int hashCode() {
146
147     int hashCode = getClassDescriptor().hashCode();
148     return hashCode + locTuple.hashCode();
149
150   }
151
152 }