KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > alphabet > SymbolPair


1 /* This file is part of SableCC ( http://sablecc.org ).
2  *
3  * Copyright 2007 Etienne M. Gagnon <egagnon@j-meg.com>
4  * Copyright 2007 Patrick Pelletier <pp.pelletier@gmail.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.sablecc.sablecc.alphabet;
20
21 /**
22  * A symbol pair is a pair <code>(x,y)</code> where each of <code>x</code>
23  * and <code>y</code> is a symbol or <code>null</code>.
24  */

25 final class SymbolPair<T extends Comparable JavaDoc<? super T>> {
26
27     /** The first symbol of the pair, possibly <code>null</code>. */
28     private final Symbol<T> symbol1;
29
30     /** The second symbol of the pair, possibly <code>null</code>. */
31     private final Symbol<T> symbol2;
32
33     /** Cached hashcode. Is <code>null</code> when not yet computed. */
34     private Integer JavaDoc hashCode;
35
36     /**
37      * Cached string representation. Is <code>null</code> when not yet
38      * computed.
39      */

40     private String JavaDoc toString;
41
42     /**
43      * Constructs a symbol pair with two provided symbols.
44      *
45      * @param symbol1
46      * the first symbol of the pair.
47      * @param symbol2
48      * the second symbol of the pair.
49      */

50     SymbolPair(
51             Symbol<T> symbol1,
52             Symbol<T> symbol2) {
53
54         this.symbol1 = symbol1;
55         this.symbol2 = symbol2;
56     }
57
58     /**
59      * Returns the first symbol of the pair.
60      *
61      * @return the first symbol of the pair.
62      */

63     Symbol<T> getSymbol1() {
64
65         return this.symbol1;
66     }
67
68     /**
69      * Returns the second symbol of the pair.
70      *
71      * @return the second symbol of the pair.
72      */

73     Symbol<T> getSymbol2() {
74
75         return this.symbol2;
76     }
77
78     /**
79      * Returns whether this instance is equal to the provided object. They are
80      * equal if they contain identical symbols.
81      *
82      * @param obj
83      * the object to compare with.
84      * @return <code>true</code> if this symbol pair and the object are equal;
85      * <code>false</code> otherwise.
86      */

87     @Override JavaDoc
88     public boolean equals(
89             Object JavaDoc obj) {
90
91         if (this == obj) {
92             return true;
93         }
94
95         if (obj == null) {
96             return false;
97         }
98
99         if (getClass() != obj.getClass()) {
100             return false;
101         }
102
103         SymbolPair symbolPair = (SymbolPair) obj;
104
105         if (this.symbol1 == null && symbolPair.symbol1 != null) {
106             return false;
107         }
108
109         if (this.symbol2 == null && symbolPair.symbol2 != null) {
110             return false;
111         }
112
113         return (this.symbol1 == null || this.symbol1.equals(symbolPair.symbol1))
114                 && (this.symbol2 == null || this.symbol2
115                         .equals(symbolPair.symbol2));
116     }
117
118     /**
119      * Returns the hash code of this symbol pair.
120      *
121      * @return the hash code.
122      */

123     @Override JavaDoc
124     public int hashCode() {
125
126         if (this.hashCode == null) {
127
128             int hashCode = 0;
129
130             if (this.symbol1 != null) {
131                 hashCode += this.symbol1.hashCode();
132             }
133
134             if (this.symbol2 != null) {
135                 hashCode += 17 * this.symbol2.hashCode();
136             }
137
138             this.hashCode = hashCode;
139         }
140
141         return this.hashCode;
142     }
143
144     /**
145      * Returns the string representation of this symbol pair.
146      *
147      * @return the string representation.
148      */

149     @Override JavaDoc
150     public String JavaDoc toString() {
151
152         if (this.toString == null) {
153             this.toString = "(" + this.symbol1 + "," + this.symbol2 + ")";
154         }
155
156         return this.toString;
157     }
158 }
159
Popular Tags