99c4faed5c7141274130b9e4280857a5b93c0d6b
[IRC.git] / Robust / src / Analysis / OwnershipAnalysis / TokenTuple.java
1 package Analysis.OwnershipAnalysis;
2
3 import IR.*;
4 import IR.Flat.*;
5 import java.util.*;
6 import java.io.*;
7
8
9 // a token touple is a pair that indicates a
10 // heap region node and an arity
11
12 // THIS CLASS IS IMMUTABLE!
13
14 public class TokenTuple extends Canonical {
15
16     private Integer token;
17     private boolean isNewSummary;
18
19
20     // only summary tokens should have ARITY_MANY?
21     public static final int ARITY_ONE  = 1;
22     public static final int ARITY_MANY = 2;
23     private int arity;
24
25
26     public TokenTuple( HeapRegionNode hrn ) {
27         assert hrn != null;
28
29         token        = hrn.getID();
30         isNewSummary = hrn.isNewSummary();
31         arity        = ARITY_ONE;
32     }
33
34     public TokenTuple( Integer token,
35                        boolean isNewSummary,
36                        int     arity ) {
37         assert token != null;
38
39         this.token        = token;
40         this.isNewSummary = isNewSummary;
41         this.arity        = arity;
42     }
43
44
45     public TokenTuple makeCanonical() {
46         return (TokenTuple) Canonical.makeCanonical( this );
47     }
48
49
50     public Integer getToken() { return token; }
51     public int     getArity() { return arity; }
52
53
54     public TokenTuple increaseArity() {
55         if( isNewSummary ) {
56             return (TokenTuple) Canonical.makeCanonical( 
57               new TokenTuple( token, isNewSummary, ARITY_MANY )
58                                                          );
59         }
60         return this;
61     }
62
63
64     public TokenTuple changeTokenTo( Integer tokenToChangeTo ) {
65         assert tokenToChangeTo != null;
66         assert isNewSummary    == false;
67
68         return new TokenTuple( tokenToChangeTo,
69                                isNewSummary,
70                                arity ).makeCanonical();
71     }
72
73
74     public boolean equals( Object o ) {
75         if( o == null ) {
76             return false;
77         }
78
79         if( !(o instanceof TokenTuple) ) {
80             return false;
81         }
82
83         TokenTuple tt = (TokenTuple) o;
84
85         return token.equals( tt.getToken() ) &&
86                arity ==      tt.getArity();
87     }
88
89     public int hashCode() {
90         return token.intValue()*31 + arity;
91     }
92
93
94     public String toString() {
95         String s = "";
96         if( isNewSummary ) {
97             s = "S";
98         }
99
100         String t = "";
101         if( arity == ARITY_MANY ) {
102             t = "*";
103         }
104
105         return new String( token+s+t );
106     }
107 }