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;
33 locTuple.addElement(loc);
37 public void addLocationSet(Set<Location> set) {
39 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
40 Location location = (Location) iterator.next();
41 locTuple.addElement(location);
46 public Location getLocation(ClassDescriptor cd) {
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)) {
61 public Map<ClassDescriptor, Location> getCd2Loc() {
63 Map<ClassDescriptor, Location> cd2loc = new Hashtable<ClassDescriptor, Location>();
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);
75 public NTuple<Location> getBaseLocationTuple() {
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);
82 if (locElement instanceof DeltaLocation) {
83 // baseLocationSet.addAll(((DeltaLocation)
84 // locElement).getDeltaOperandLocationVec());
85 baseLocationTuple.addAll(((DeltaLocation) locElement).getBaseLocationTuple());
87 baseLocationTuple.addElement(locElement);
90 return baseLocationTuple;
94 public Set<Location> getBaseLocationSet() {
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);
101 if (locElement instanceof DeltaLocation) {
102 // baseLocationSet.addAll(((DeltaLocation)
103 // locElement).getDeltaOperandLocationVec());
104 baseLocationSet.addAll(((DeltaLocation) locElement).getBaseLocationSet());
106 baseLocationSet.add(locElement);
109 return baseLocationSet;
112 public int getNumofDelta() {
116 if (locTuple.size() == 1) {
117 Location locElement = locTuple.at(0);
118 if (locElement instanceof DeltaLocation) {
120 result += getNumofDelta((DeltaLocation) locElement);
126 public int getNumofDelta(DeltaLocation delta) {
129 if (delta.getDeltaOperandLocationVec().size() == 1) {
130 Location locElement = delta.getDeltaOperandLocationVec().at(0);
131 if (locElement instanceof DeltaLocation) {
133 result += getNumofDelta((DeltaLocation) locElement);
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);
149 public String toString() {
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();
161 String rtr = "CompLoc[";
163 int tupleSize = locTuple.size();
164 for (int i = 0; i < tupleSize; i++) {
165 Location locElement = locTuple.at(i);
176 public boolean equals(Object o) {
178 if (!(o instanceof CompositeLocation)) {
182 CompositeLocation compLoc = (CompositeLocation) o;
184 if (compLoc.getClassDescriptor().equals(getClassDescriptor())
185 && compLoc.getTuple().equals(getTuple())) {
193 public int hashCode() {
195 int hashCode = getClassDescriptor().hashCode();
196 return hashCode + locTuple.hashCode();