01: /*
02: * xtc - The eXTensible Compiler
03: * Copyright (C) 2004-2007 Robert Grimm
04: *
05: * This program is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU General Public License
07: * version 2 as published by the Free Software Foundation.
08: *
09: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program; if not, write to the Free Software
16: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17: * USA.
18: */
19: package xtc.parser;
20:
21: import java.util.ArrayList;
22: import java.util.List;
23:
24: /**
25: * An ordered choice of grammar elements.
26: *
27: * @author Robert Grimm
28: * @version $Revision: 1.14 $
29: */
30: public class OrderedChoice extends Element {
31:
32: /** The ordered list of sequences. */
33: public List<Sequence> alternatives;
34:
35: /** Create a new ordered choice with no choices. */
36: public OrderedChoice() {
37: this .alternatives = new ArrayList<Sequence>();
38: }
39:
40: /**
41: * Create a new ordered choice.
42: *
43: * @param alternatives The list of alternatives.
44: */
45: public OrderedChoice(List<Sequence> alternatives) {
46: this .alternatives = alternatives;
47: }
48:
49: /**
50: * Create a new ordered choice with the specified element as its
51: * only alternative. The constructor {@link
52: * Sequence#ensure(Element) ensures} that the element is a sequence.
53: * It also copies the element's location into the newly created
54: * choice.
55: *
56: * @param element The element.
57: */
58: public OrderedChoice(Element element) {
59: alternatives = new ArrayList<Sequence>(1);
60: alternatives.add(Sequence.ensure(element));
61: setLocation(element);
62: }
63:
64: public Tag tag() {
65: return Tag.CHOICE;
66: }
67:
68: public int hashCode() {
69: return alternatives.hashCode();
70: }
71:
72: public boolean equals(Object o) {
73: if (this == o)
74: return true;
75: if (!(o instanceof OrderedChoice))
76: return false;
77: return alternatives.equals(((OrderedChoice) o).alternatives);
78: }
79:
80: }
|