KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > automaton > GroupPair


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.automaton;
20
21 import org.sablecc.sablecc.exception.InternalException;
22
23 /**
24  * A group pair is a pair <code>(x,y)</code> where each of <code>x</code>
25  * and <code>y</code> is a group.
26  */

27 class GroupPair<T extends Comparable JavaDoc<? super T>> {
28
29     /** The first group of this pair. */
30     private final Group<T> group1;
31
32     /** The second group of this pair. */
33     private final Group<T> group2;
34
35     /** Cached hashcode. Is <code>null</code> when not yet computed. */
36     private Integer JavaDoc hashcode;
37
38     /**
39      * Cached string representation. Is <code>null</code> when not yet
40      * computed.
41      */

42     private String JavaDoc toString;
43
44     /**
45      * Constructs a group pair with the two provided groups.
46      *
47      * @param group1
48      * the first group.
49      * @param group2
50      * the second group.
51      * @throws InternalException
52      * if one of the provided groups is <code>null</code>.
53      */

54     GroupPair(
55             final Group<T> group1,
56             final Group<T> group2) {
57
58         if (group1 == null) {
59             throw new InternalException("group1 may not be null");
60         }
61
62         if (group2 == null) {
63             throw new InternalException("group2 may not be null");
64         }
65
66         this.group1 = group1;
67         this.group2 = group2;
68     }
69
70     /**
71      * Returns the first group of this group pair.
72      *
73      * @return the first group.
74      */

75     Group<T> getGroup1() {
76
77         return this.group1;
78     }
79
80     /**
81      * Returns the second group of this group pair.
82      *
83      * @return the second group.
84      */

85     Group<T> getGroup2() {
86
87         return this.group2;
88     }
89
90     /**
91      * Returns whether this instance is equal to the provided object. They are
92      * equal if they both have equal groups.
93      *
94      * @param obj
95      * the object to compare with.
96      * @return <code>true</code> if this group pair and the object are equal;
97      * <code>false</code> otherwise.
98      */

99     @Override JavaDoc
100     public boolean equals(
101             Object JavaDoc obj) {
102
103         if (this == obj) {
104             return true;
105         }
106
107         if (obj == null) {
108             return false;
109         }
110
111         if (getClass() != obj.getClass()) {
112             return false;
113         }
114
115         GroupPair groupPair = (GroupPair) obj;
116
117         return this.group1.equals(groupPair.group1)
118                 && this.group2.equals(groupPair.group2);
119     }
120
121     /**
122      * Returns the hash code of this group pair.
123      *
124      * @return the hash code.
125      */

126     @Override JavaDoc
127     public int hashCode() {
128
129         if (this.hashcode == null) {
130             this.hashcode = this.group1.hashCode() + 29
131                     * this.group2.hashCode();
132         }
133
134         return this.hashcode;
135     }
136
137     /**
138      * Returns the string representation of this group pair.
139      *
140      * @return the string representation.
141      */

142     @Override JavaDoc
143     public String JavaDoc toString() {
144
145         if (this.toString == null) {
146             this.toString = "(" + this.group1 + "," + this.group2 + ")";
147         }
148
149         return this.toString;
150     }
151 }
152
Popular Tags