Skip to content

Method: accept(ConstructorOrOperationExceptionVisitor)

1: package de.fhdw.wtf.common.ast;
2:
3: import java.util.Collection;
4: import java.util.Iterator;
5:
6: import de.fhdw.wtf.common.ast.type.ClassType;
7: import de.fhdw.wtf.common.ast.type.ProductType;
8: import de.fhdw.wtf.common.ast.type.Type;
9: import de.fhdw.wtf.common.ast.visitor.ConstructorOrOperationExceptionVisitor;
10: import de.fhdw.wtf.common.ast.visitor.OperationModifierVisitorReturn;
11: import de.fhdw.wtf.common.constants.parser.AstDescriptionConstants;
12: import de.fhdw.wtf.common.exception.walker.TaskException;
13: import de.fhdw.wtf.common.token.Token;
14:
15: /**
16: * This class represents a operation of a class. It is held by a <ClassType>, can contain multiple
17: * {@link OperationModifier}, has a name, returnType and parameters.
18: */
19: public final class Operation extends ConstructorOrOperation {
20:         
21:         /**
22:          * generated.
23:          */
24:         private static final long serialVersionUID = 8835914994800497991L;
25:         
26:         /**
27:          * The modifiers of the operation.
28:          *
29:          */
30:         private final Collection<OperationModifier> modifiers;
31:         
32:         /**
33:          * Private-Constructor of {@link Operation}.
34:          *
35:          * @param name
36:          * name
37:          * @param modifiers
38:          * The modifiers of the operation.
39:          * @param parameters
40:          * The parameters of the operation.
41:          * @param containingType
42:          * The classType of the class, that contains the operation.
43:          * @param returnType
44:          * The return-type of the Operation.
45:          * @param firstToken
46:          * firstToken
47:          * @param lastToken
48:          * lastToken
49:          */
50:         private Operation(final String name,
51:                         final Collection<OperationModifier> modifiers,
52:                         final ProductType parameters,
53:                         final ClassType containingType,
54:                         final Type returnType,
55:                         final Token firstToken,
56:                         final Token lastToken) {
57:                 super(name, parameters, containingType, returnType, firstToken, lastToken);
58:                 this.modifiers = modifiers;
59:         }
60:         
61:         /**
62:          * Creates a {@link Operation}-Object.
63:          *
64:          * @param name
65:          * name
66:          * @param modifiers
67:          * The modifiers of the operation.
68:          * @param parameters
69:          * The params of the operation.
70:          * @param containingType
71:          * The classType of the class, that contains the constructor or operation.
72:          * @param returnType
73:          * The Returntype of the Operation.
74:          * @param firstToken
75:          * firstToken
76:          * @param lastToken
77:          * lastToken
78:          * @return The Operation-Object
79:          */
80:         public static Operation create(final String name,
81:                         final Collection<OperationModifier> modifiers,
82:                         final ProductType parameters,
83:                         final ClassType containingType,
84:                         final Type returnType,
85:                         final Token firstToken,
86:                         final Token lastToken) {
87:                 return new Operation(name, modifiers, parameters, containingType, returnType, firstToken, lastToken);
88:         }
89:         
90:         /**
91:          * Creates a {@link Operation}-Object.
92:          *
93:          * @param name
94:          * name
95:          * @param modifiers
96:          * The modifiers of the operation.
97:          * @param parameters
98:          * The params of the operation.
99:          * @param containingType
100:          * The classType of the class, that contains the constructor or operation.
101:          * @param returnType
102:          * The Returntype of the Operation.
103:          * @param firstToken
104:          * firstToken
105:          * @return The Operation-Object
106:          */
107:         public static Operation create(final String name,
108:                         final Collection<OperationModifier> modifiers,
109:                         final ProductType parameters,
110:                         final ClassType containingType,
111:                         final Type returnType,
112:                         final Token firstToken) {
113:                 return new Operation(name, modifiers, parameters, containingType, returnType, firstToken, null);
114:         }
115:         
116:         /**
117:          * Returns true if <code>this</code> is an abstract operation. False otherwise.
118:          *
119:          * @return boolean.
120:          */
121:         public boolean isAbstract() {
122:                 final OperationModifierVisitorReturn<Boolean> visitor = new OperationModifierVisitorReturn<Boolean>() {
123:                         @Override
124:                         public Boolean handle(final OperationModifierAbstract v) {
125:                                 return true;
126:                         }
127:                 };
128:                 return this.processModifierVisitor(visitor);
129:         }
130:         
131:         /**
132:          * Processes a Returnvisitor with Return-Type Boolean and returns the result.
133:          *
134:          * @param visitor
135:          * visitor.
136:          * @return boolean.
137:          */
138:         private boolean processModifierVisitor(final OperationModifierVisitorReturn<Boolean> visitor) {
139:                 for (final OperationModifier current : this.getModifiers()) {
140:                         if (current.accept(visitor)) {
141:                                 return true;
142:                         }
143:                 }
144:                 return false;
145:         }
146:         
147:         @Override
148:         public boolean equals(final Object o) {
149:                 if (o instanceof Operation) {
150:                         final Operation other = (Operation) o;
151:                         boolean result = true;
152:                         result = result && this.getName().equals(other.getName());
153:                         result = result && this.getReturnType().equals(other.getReturnType());
154:                         final Iterator<OperationModifier> i = this.getModifiers().iterator();
155:                         while (i.hasNext()) {
156:                                 final OperationModifier current = i.next();
157:                                 result = result && other.getModifiers().contains(current);
158:                         }
159:                         // there is no use hitting a dead body
160:                         if (!result) {
161:                                 return result;
162:                         }
163:                         return this.getParameters().equals(other.getParameters());
164:                 }
165:                 return false;
166:         }
167:         
168:         @Override
169:         public int hashCode() {
170:                 int result =
171:                                 this.getName().hashCode() ^ this.getReturnType().hashCode()
172:                                                 ^ this.getParameters().hashCode();
173:                 for (final OperationModifier modifier : this.getModifiers()) {
174:                         result ^= modifier.hashCode();
175:                 }
176:                 return result;
177:         }
178:         
179:         @Override
180:         public String toString() {
181:                 final StringBuilder result = new StringBuilder();
182:                 result.append(this.getName()).append(AstDescriptionConstants.COLON_TOKEN)
183:                                 .append(AstDescriptionConstants.DOUBLE_SQUARE_BRACKET_OPEN_TOKEN)
184:                                 .append(this.getParameters()).append(AstDescriptionConstants.ARROW_TOKEN)
185:                                 .append(this.getReturnType()).append(AstDescriptionConstants.DOUBLE_SQUARE_BRACKET_CLOSE_TOKEN);
186:                 if (!this.getModifiers().isEmpty()) {
187:                         final Iterator<OperationModifier> i = this.getModifiers().iterator();
188:                         while (i.hasNext()) {
189:                                 result.append(' ').append(i.next());
190:                         }
191:                 }
192:                 result.append(AstDescriptionConstants.SEMICOLON_TOKEN);
193:                 return result.toString();
194:         }
195:         
196:         /**
197:          * The modifiers of the operation.
198:          *
199:          * @return modifiers
200:          */
201:         public Collection<OperationModifier> getModifiers() {
202:                 return this.modifiers;
203:         }
204:         
205:         /**
206:          * The name of the operation.
207:          *
208:          * @return name
209:          */
210:         public String getName() {
211:                 return this.getSignature().getName();
212:         }
213:         
214:         @Override
215:         public void accept(final ConstructorOrOperationExceptionVisitor<TaskException> constructorAndOperationVisitor)
216:                         throws TaskException {
217:                 constructorAndOperationVisitor.handleOperation(this);
218:                 
219:         }
220: }