KickJava   Java API By Example, From Geeks To Geeks.

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


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.alphabet.Symbol;
22 import org.sablecc.sablecc.exception.InternalException;
23
24 /**
25  * An element represents a state. It is especially used in methods related to
26  * transitions since they return groups.
27  */

28 class Element<T extends Comparable JavaDoc<? super T>> {
29
30     /** The partition of this element. */
31     private final Partition<T> partition;
32
33     /** The state of this element. */
34     private final DfaState<T> state;
35
36     /** The group of this element. */
37     private Group<T> group;
38
39     /**
40      * Constructs an element with the provided partition and state.
41      *
42      * @param partition
43      * the partition.
44      * @param state
45      * the state.
46      * @throws InternalException
47      * if the provided partition or state is <code>null</code> or
48      * if the state is not from same <code>Dfa</code> as the
49      * partition.
50      */

51     Element(
52             final Partition<T> partition,
53             final DfaState<T> state) {
54
55         if (partition == null) {
56             throw new InternalException("partition may not be null");
57         }
58
59         if (state == null) {
60             throw new InternalException("state may not be null");
61         }
62
63         if (state.getDfa() != partition.getDfa()) {
64             throw new InternalException("invalid state");
65         }
66
67         this.partition = partition;
68         this.state = state;
69         this.group = null;
70
71         partition.addElement(this);
72     }
73
74     /**
75      * Returns the partition of this element.
76      *
77      * @return the partition.
78      */

79     Partition<T> getPartition() {
80
81         return this.partition;
82     }
83
84     /**
85      * Returns the state of this element.
86      *
87      * @return the state.
88      */

89     DfaState<T> getState() {
90
91         return this.state;
92     }
93
94     /**
95      * Returns the group of this element.
96      *
97      * @return the group.
98      */

99     Group<T> getGroup() {
100
101         return this.group;
102     }
103
104     /**
105      * Sets the group to wich this instance belongs.
106      *
107      * @param group
108      * the group.
109      * @throws InternalException
110      * if the provided group is <code>null</code> or if it has a
111      * different partition as the one of this instance.
112      */

113     void setGroup(
114             Group<T> group) {
115
116         if (group == null) {
117             throw new InternalException("group may not be null");
118         }
119
120         if (group.getPartition() != this.partition) {
121             throw new InternalException("invalid group");
122         }
123
124         // Don't forget that this.group is initially null!
125
if (this.group != null) {
126             this.group.removeElement(this);
127         }
128
129         this.group = group;
130         group.addElement(this);
131     }
132
133     /**
134      * Returns the target group of the provided symbol.
135      *
136      * @param symbol
137      * the symbol.
138      * @return the target group.
139      * @throws InternalException
140      * if the provided symbol is <code>null</code> or if it not
141      * part of this instance's partition.
142      */

143     Group<T> getTarget(
144             Symbol<T> symbol) {
145
146         if (symbol == null) {
147             throw new InternalException("symbol may not be null");
148         }
149
150         if (!this.partition.getDfa().getAlphabet().getSymbols()
151                 .contains(symbol)) {
152             throw new InternalException("invalid symbol");
153         }
154
155         return this.partition.getElement(this.state.getTarget(symbol)).group;
156     }
157 }
158
Popular Tags