KickJava   Java API By Example, From Geeks To Geeks.

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


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 Investigator
27         implements Comparable JavaDoc<Investigator> {
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     public Investigator(
36             String JavaDoc name) {
37
38         if (name == null) {
39             throw new InternalException("name may not be null");
40         }
41
42         this.name = name;
43     }
44
45     public String JavaDoc getName() {
46
47         return this.name;
48     }
49
50     public SortedSet JavaDoc<Token> getTokens() {
51
52         if (!this.isStable) {
53             throw new InternalException("this investigator is not stable yet");
54         }
55
56         return this.tokens;
57     }
58
59     boolean isStable() {
60
61         return this.isStable;
62     }
63
64     @Override JavaDoc
65     public boolean equals(
66             Object JavaDoc obj) {
67
68         if (this == obj) {
69             return true;
70         }
71
72         if (obj == null) {
73             return false;
74         }
75
76         if (getClass() != obj.getClass()) {
77             return false;
78         }
79
80         Investigator investigator = (Investigator) obj;
81
82         return this.name.equals(investigator.name);
83     }
84
85     @Override JavaDoc
86     public int hashCode() {
87
88         return this.name.hashCode();
89     }
90
91     public int compareTo(
92             Investigator investigator) {
93
94         return this.name.compareTo(investigator.name);
95     }
96
97     void stabilize() {
98
99         if (this.isStable) {
100             throw new InternalException("this investigator is already stable");
101         }
102
103         if (this.tokens.size() < 1) {
104             throw new InternalException("there must be at least one token");
105         }
106
107         this.tokens = Collections.unmodifiableSortedSet(this.tokens);
108         this.isStable = true;
109     }
110
111     public void addToken(
112             Token token) {
113
114         if (token == null) {
115             throw new InternalException("token may not be null");
116         }
117
118         if (this.tokens.contains(token)) {
119             throw new InternalException(
120                     "the token is already included in the token set");
121         }
122
123         this.tokens.add(token);
124     }
125
126 }
127
Popular Tags