1 package de.fhdw.wtf.common.ast;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.Iterator;
6
7 import de.fhdw.wtf.common.ast.visitor.GroupElementExceptionVisitor;
8 import de.fhdw.wtf.common.ast.visitor.GroupElementReturnVisitor;
9 import de.fhdw.wtf.common.ast.visitor.GroupElementVisitor;
10 import de.fhdw.wtf.common.constants.parser.AstDescriptionConstants;
11 import de.fhdw.wtf.common.token.Token;
12
13
14
15
16
17
18 public final class Group extends SyntaxObject implements GroupElement {
19
20
21
22
23 private static final long serialVersionUID = 1603342875497690969L;
24
25
26
27
28 private static final String BREAK = " \n";
29
30
31
32
33 private final Collection<GroupElement> groupElements;
34
35
36
37
38 private final Name name;
39
40
41
42
43
44
45
46
47
48
49
50
51
52 private Group(final Name name, final Collection<GroupElement> groups, final Token firstToken, final Token lastToken) {
53 super(firstToken, lastToken);
54 this.groupElements = groups;
55 this.name = name;
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public static Group create(final Name name,
72 final Collection<GroupElement> groups,
73 final Token firstToken,
74 final Token lastToken) {
75 return new Group(name, groups, firstToken, lastToken);
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89 public static Group create(final Name name, final Collection<GroupElement> groups, final Token firstToken) {
90 return new Group(name, groups, firstToken, null);
91 }
92
93 @Override
94 public boolean equals(final Object o) {
95 if (o instanceof Group) {
96 boolean result = true;
97 final Group other = (Group) o;
98 result =
99 result && this.getName().equals(other.getName())
100 && this.getGroupElements().size() == other.getGroupElements().size();
101 if (result) {
102 final GroupElement[] thisArray =
103 this.getGroupElements().toArray(new GroupElement[this.getGroupElements().size()]);
104 final GroupElement[] otherArray =
105 other.getGroupElements().toArray(new GroupElement[other.getGroupElements().size()]);
106 for (int i = 0; i < this.getGroupElements().size(); i++) {
107 result = result && thisArray[i].equals(otherArray[i]);
108 }
109 return result;
110 }
111 }
112 return false;
113 }
114
115 @Override
116 public int hashCode() {
117 int result = this.getName().hashCode();
118 for (final GroupElement element : this.getGroupElements()) {
119 result ^= element.hashCode();
120 }
121 return result;
122 }
123
124 @Override
125 public String toString() {
126 final StringBuilder result = new StringBuilder();
127 result.append(this.getName().toString()).append(AstDescriptionConstants.COLON_TOKEN)
128 .append(AstDescriptionConstants.GROUP_TOKEN).append(AstDescriptionConstants.EQUAL_TOKEN)
129 .append(AstDescriptionConstants.SQUARE_BRACKET_OPEN).append(BREAK);
130 final Iterator<GroupElement> i = this.getGroupElements().iterator();
131 while (i.hasNext()) {
132 final GroupElement current = i.next();
133 result.append(current.toString()).append(BREAK);
134 }
135 result.append(AstDescriptionConstants.SQUARE_BRACKET_CLOSE).append(AstDescriptionConstants.SEMICOLON_TOKEN);
136 return result.toString();
137 }
138
139 @Override
140 public void accept(final GroupElementVisitor visitor) {
141 visitor.handle(this);
142 }
143
144 @Override
145 public <X extends Exception> void accept(final GroupElementExceptionVisitor<X> visitor) throws X {
146 visitor.handle(this);
147 }
148
149 @Override
150 public <X> X accept(final GroupElementReturnVisitor<X> visitor) {
151 return visitor.handle(this);
152 }
153
154
155
156
157
158
159
160
161 public Collection<GroupElement> getGroupElements() {
162 return Collections.unmodifiableCollection(this.groupElements);
163 }
164
165 @Override
166 public Name getName() {
167 return this.name;
168 }
169
170 }