KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > structure > Group


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

17
18 package org.sablecc.sablecc.structure;
19
20 import java.util.Collections JavaDoc;
21 import java.util.SortedSet JavaDoc;
22 import java.util.TreeSet JavaDoc;
23
24 import org.sablecc.sablecc.exception.InternalException;
25
26 public class Group
27         implements Comparable JavaDoc<Group> {
28
29     private final String JavaDoc name;
30
31     private SortedSet JavaDoc<Token> tokens = new TreeSet JavaDoc<Token>();
32
33     private boolean isStable = false;
34
35     Group(
36             String JavaDoc name) {
37
38         this.name = name;
39     }
40
41     public boolean hasName() {
42
43         return this.name != null;
44     }
45
46     public String JavaDoc getName() {
47
48         if (this.name == null) {
49             throw new InternalException("this group does not have a name");
50         }
51
52         return this.name;
53     }
54
55     public SortedSet JavaDoc<Token> getTokens() {
56
57         if (!this.isStable) {
58             throw new InternalException("this group is not stable yet");
59         }
60
61         return this.tokens;
62     }
63
64     boolean isStable() {
65
66         return this.isStable;
67     }
68
69     @Override JavaDoc
70     public boolean equals(
71             Object JavaDoc obj) {
72
73         if (this == obj) {
74             return true;
75         }
76
77         if (obj == null) {
78             return false;
79         }
80
81         if (getClass() != obj.getClass()) {
82             return false;
83         }
84
85         Group group = (Group) obj;
86
87         if (this.name == null) {
88             return this.name == group.name;
89         }
90
91         return this.name.equals(group.name);
92     }
93
94     @Override JavaDoc
95     public int hashCode() {
96
97         if (this.name == null) {
98             return 0;
99         }
100
101         return this.name.hashCode();
102     }
103
104     public int compareTo(
105             Group group) {
106
107         if (this.name == null) {
108             if (group.name == null) {
109                 return 0;
110             }
111
112             return -1;
113         }
114         else if (group == null) {
115             return 1;
116         }
117
118         return this.name.compareTo(group.name);
119     }
120
121     void stabilize() {
122
123         if (this.isStable) {
124             throw new InternalException("this group is already stable");
125         }
126
127         this.tokens = Collections.unmodifiableSortedSet(this.tokens);
128         this.isStable = true;
129     }
130
131     void addToken(
132             Token token) {
133
134         if (this.isStable) {
135             throw new InternalException("a stable group may not be modified");
136         }
137
138         if (this.tokens.contains(token)) {
139             throw new InternalException(
140                     "the token is already included in this group");
141         }
142
143         if (token.getGroup() != this) {
144             throw new InternalException(
145                     "the token does not belong to this group");
146         }
147
148         this.tokens.add(token);
149     }
150 }
151
Popular Tags