Skip to content

Method: toStringWithoutSuperconstructors()

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.type.ClassType;
8: import de.fhdw.wtf.common.ast.type.ProductType;
9: import de.fhdw.wtf.common.ast.visitor.ConstructorOrOperationExceptionVisitor;
10: import de.fhdw.wtf.common.constants.parser.AstDescriptionConstants;
11: import de.fhdw.wtf.common.exception.walker.TaskException;
12: import de.fhdw.wtf.common.token.Token;
13:
14: /**
15: * This class represents a constructor of a class. It is held by a <ClassType>, can contain multiple
16: * {@link ConstructorReference} of super class constructors and has parameters.
17: */
18: public final class Constructor extends ConstructorOrOperation {
19:         
20:         /**
21:          * The constructors called by this constructor.
22:          *
23:          */
24:         private final Collection<ConstructorReference> superConstructors;
25:         
26:         /**
27:          * Private-Constructor of {@link Constructor}.
28:          *
29:          *
30:          * name of the constructor(generally the class-name of the constructor).
31:          *
32:          * @param parameters
33:          * The parameters of the constructor.
34:          * @param containingType
35:          * The classType of the class, that contains the constructor or operation.
36:          * @param superConstructors
37:          * The constructors called by this constructor.
38:          * @param firstToken
39:          * firstToken
40:          * @param lastToken
41:          * lastToken
42:          */
43:         private Constructor(final ProductType parameters,
44:                         final ClassType containingType,
45:                         final Collection<ConstructorReference> superConstructors,
46:                         final Token firstToken,
47:                         final Token lastToken) {
48:                 super("", parameters, containingType, containingType, firstToken, lastToken);
49:                 this.superConstructors = superConstructors;
50:         }
51:         
52:         /**
53:          * Creates a {@link Constructor}-Object.
54:          *
55:          * @param parameters
56:          * The parameters of the constructor.
57:          * @param containingType
58:          * The classType of the class, that contains the constructor or operation.
59:          *
60:          * @param superConstructors
61:          * The constructors called by this constructor
62:          * @param firstToken
63:          * firstToken
64:          * @param lastToken
65:          * lastToken
66:          * @return The Constructor-Object
67:          */
68:         public static Constructor create(final ProductType parameters,
69:                         final ClassType containingType,
70:                         final Collection<ConstructorReference> superConstructors,
71:                         final Token firstToken,
72:                         final Token lastToken) {
73:                 return new Constructor(parameters, containingType, superConstructors, firstToken, lastToken);
74:                 
75:         }
76:         
77:         /**
78:          * Creates a {@link Constructor}-Object.
79:          *
80:          * @param parameters
81:          * The parameters of the constructor.
82:          * @param containingType
83:          * The classType of the class, that contains the constructor or operation.
84:          * @param superConstructors
85:          * The constructors called by this constructor.
86:          * @param firstToken
87:          * firstToken
88:          * @return The Constructor-Object
89:          */
90:         public static Constructor create(final ProductType parameters,
91:                         final ClassType containingType,
92:                         final Collection<ConstructorReference> superConstructors,
93:                         final Token firstToken) {
94:                 return new Constructor(parameters, containingType, superConstructors, firstToken, null);
95:                 
96:         }
97:         
98:         @Override
99:         public String toString() {
100:                 final StringBuilder result = new StringBuilder();
101:                 result.append(this.getParameters());
102:                 if (!this.superConstructors.isEmpty()) {
103:                         result.append(AstDescriptionConstants.EQUAL_TOKEN);
104:                         final Iterator<ConstructorReference> iterator = this.superConstructors.iterator();
105:                         while (iterator.hasNext()) {
106:                                 result.append(iterator.next().toString());
107:                                 if (iterator.hasNext()) {
108:                                         result.append(AstDescriptionConstants.PLUS_TOKEN);
109:                                 }
110:                         }
111:                 }
112:                 result.append(AstDescriptionConstants.SEMICOLON_TOKEN);
113:                 return result.toString();
114:         }
115:         
116:         /**
117:          * Represent the {@link Constructor}-Object as a String without the constructors called by this constructor.
118:          *
119:          * @return {@link Constructor}-Object as a String
120:          */
121:         public String toStringWithoutSuperconstructors() {
122:                 final StringBuilder result = new StringBuilder();
123:                 result.append(this.getParameters());
124:                 return result.toString();
125:         }
126:         
127:         @Override
128:         public boolean equals(final Object o) {
129:                 if (o instanceof Constructor) {
130:                         final Constructor other = (Constructor) o;
131:                         return this.superConstructors.containsAll(other.getSuperConstructors())
132:                                         && other.getSuperConstructors().containsAll(this.superConstructors)
133:                                         && this.getParameters().equals(other.getParameters());
134:                 }
135:                 return false;
136:         }
137:         
138:         @Override
139:         public int hashCode() {
140:                 int result = this.getParameters().hashCode();
141:                 for (final ConstructorReference superConstructor : this.superConstructors) {
142:                         result ^= superConstructor.hashCode();
143:                 }
144:                 return result;
145:         }
146:         
147:         /**
148:          * The superConstructor of the constructor.
149:          *
150:          * @return superConstructors
151:          */
152:         public Collection<ConstructorReference> getSuperConstructors() {
153:                 return Collections.unmodifiableCollection(this.superConstructors);
154:         }
155:         
156:         @Override
157:         public void accept(final ConstructorOrOperationExceptionVisitor<TaskException> constructorAndOperationVisitor)
158:                         throws TaskException {
159:                 constructorAndOperationVisitor.handleConstructor(this);
160:                 
161:         }
162: }