1 package Analysis.SSJava;
3 import java.util.HashSet;
4 import java.util.Hashtable;
5 import java.util.Iterator;
9 import IR.ClassDescriptor;
11 public class CompositeLocation extends Location{
13 protected NTuple<Location> locTuple;
15 public CompositeLocation(ClassDescriptor cd) {
17 locTuple = new NTuple<Location>();
20 public NTuple<Location> getTuple() {
24 public int getBaseLocationSize() {
25 return getBaseLocationSet().size();
28 public void addLocation(Location loc) {
30 if (loc instanceof DeltaLocation) {
31 type = Location.DELTA;
34 locTuple.addElement(loc);
38 public void addLocationSet(Set<Location> set) {
40 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
41 Location location = (Location) iterator.next();
42 locTuple.addElement(location);
47 public Location getLocation(ClassDescriptor cd) {
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)) {
62 public Map<ClassDescriptor, Location> getCd2Loc() {
64 Map<ClassDescriptor, Location> cd2loc = new Hashtable<ClassDescriptor, Location>();
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);
76 public NTuple<Location> getBaseLocationTuple() {
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);
83 if (locElement instanceof DeltaLocation) {
84 // baseLocationSet.addAll(((DeltaLocation)
85 // locElement).getDeltaOperandLocationVec());
86 baseLocationTuple.addAll(((DeltaLocation) locElement).getBaseLocationTuple());
88 baseLocationTuple.addElement(locElement);
91 return baseLocationTuple;
95 public Set<Location> getBaseLocationSet() {
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);
102 if (locElement instanceof DeltaLocation) {
103 // baseLocationSet.addAll(((DeltaLocation)
104 // locElement).getDeltaOperandLocationVec());
105 baseLocationSet.addAll(((DeltaLocation) locElement).getBaseLocationSet());
107 baseLocationSet.add(locElement);
110 return baseLocationSet;
113 public int getNumofDelta() {
117 if (locTuple.size() == 1) {
118 Location locElement = locTuple.at(0);
119 if (locElement instanceof DeltaLocation) {
121 result += getNumofDelta((DeltaLocation) locElement);
127 public int getNumofDelta(DeltaLocation delta) {
130 if (delta.getDeltaOperandLocationVec().size() == 1) {
131 Location locElement = delta.getDeltaOperandLocationVec().at(0);
132 if (locElement instanceof DeltaLocation) {
134 result += getNumofDelta((DeltaLocation) locElement);
141 public String toString() {
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();
153 String rtr = "CompLoc[";
155 int tupleSize = locTuple.size();
156 for (int i = 0; i < tupleSize; i++) {
157 Location locElement = locTuple.at(i);
168 public boolean equals(Object o) {
170 if (!(o instanceof CompositeLocation)) {
174 CompositeLocation compLoc = (CompositeLocation) o;
176 if (compLoc.getClassDescriptor().equals(getClassDescriptor())
177 && compLoc.getTuple().equals(getTuple())) {
185 public int hashCode() {
187 int hashCode = getClassDescriptor().hashCode();
188 return hashCode + locTuple.hashCode();