Skip to content

Method: getGroupElements()

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: * This class represents a {@link Group} in an abstract syntax tree of the <code>WTF-Language</code>. A {@link Group} is
15: * either contained by a {@link Model} or by another {@link Group} and can contain multiple {@link GroupElement}s by
16: * itself.
17: */
18: public final class Group extends SyntaxObject implements GroupElement {
19:         
20:         /**
21:          * generated.
22:          */
23:         private static final long serialVersionUID = 1603342875497690969L;
24:         
25:         /**
26:          * Constant for Line-Break.
27:          */
28:         private static final String BREAK = " \n";
29:         
30:         /**
31:          * The Elements of the group.
32:          */
33:         private final Collection<GroupElement> groupElements;
34:         
35:         /**
36:          * The name of the group.
37:          */
38:         private final Name name;
39:         
40:         /**
41:          * Private-Constructor of {@link Group}.
42:          *
43:          * @param name
44:          * name
45:          * @param groups
46:          * Elements of the group
47:          * @param firstToken
48:          * firstToken
49:          * @param lastToken
50:          * lastToken
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:          * Creates a {@link Group}-Object.
60:          *
61:          * @param name
62:          * name
63:          * @param groups
64:          * Elements of the group
65:          * @param firstToken
66:          * firstToken
67:          * @param lastToken
68:          * lastToken
69:          * @return The Group-Object.
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:          * Creates a {@link Group}-Object.
80:          *
81:          * @param name
82:          * name
83:          * @param groups
84:          * Elements of the group
85:          * @param firstToken
86:          * firstToken
87:          * @return The Group-Object.
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:         // ///// PROJECTIONS ///////////
155:         
156:         /**
157:          * Returns a collection of {@link GroupElement}. The collection can't be modified.
158:          *
159:          * @return the groups
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: }