Skip to content

Method: QualifiedName(UnqualifiedName, Name, Token, Token)

1: package de.fhdw.wtf.common.ast;
2:
3: import java.util.List;
4:
5: import de.fhdw.wtf.common.ast.visitor.NameReturnVisitor;
6: import de.fhdw.wtf.common.ast.visitor.NameVisitor;
7: import de.fhdw.wtf.common.ast.visitor.NameVisitorReturnBoolean;
8: import de.fhdw.wtf.common.ast.visitor.NameVisitorReturnName;
9: import de.fhdw.wtf.common.ast.visitor.NameVisitorReturnNameException;
10: import de.fhdw.wtf.common.exception.referencer.InvalidTypeReferenceException;
11: import de.fhdw.wtf.common.token.IdentifierToken;
12: import de.fhdw.wtf.common.token.Token;
13:
14: /**
15: * This class represents a {@link QualifiedName}. A {@link Name} is qualified if it contains at least two
16: * {@link UnqualifiedName}s.
17: */
18: public final class QualifiedName implements Name {
19:         
20:         /**
21:          * generated.
22:          */
23:         private static final long serialVersionUID = -8270115342124653334L;
24:         
25:         /**
26:          * The rest of the name. This without first.
27:          */
28:         private final Name rest;
29:         
30:         /**
31:          * The first {@link UnqualifiedName} of the {@link QualifiedName}.
32:          *
33:          */
34:         private final UnqualifiedName first;
35:         
36:         /**
37:          * This for Multiple Inheritance.
38:          */
39:         private final SyntaxObjectInterface thiis;
40:         
41:         /**
42:          * Constructor of {@link QualifiedName}.
43:          *
44:          * @param first
45:          * first part of the name
46:          * @param rest
47:          * rest of the name
48:          * @param identifierToken
49:          * identifierToken
50:          * @param lastToken
51:          * lastToken
52:          */
53:         private QualifiedName(final UnqualifiedName first,
54:                         final Name rest,
55:                         final Token identifierToken,
56:                         final Token lastToken) {
57:                 this.first = first;
58:                 this.rest = rest;
59:                 this.thiis = SyntaxObjectConcret.create(identifierToken, lastToken);
60:         }
61:         
62:         /**
63:          * Creates a {@link QualifiedName}-Object.
64:          *
65:          * @param first
66:          * first part of the name
67:          * @param rest
68:          * rest of the name
69:          * @return The QualifiedName-Object
70:          */
71:         public static QualifiedName create(final UnqualifiedName first, final Name rest) {
72:                 return create(first, rest, first.getFirstToken(), rest.getLastToken());
73:         }
74:         
75:         /**
76:          * Creates a {@link QualifiedName}-Object.
77:          *
78:          * @param first
79:          * first part of the name
80:          * @param rest
81:          * rest of the name
82:          * @param identifierToken
83:          * identifierToken
84:          * @param lastToken
85:          * lastToken
86:          * @return The QualifiedName-Object
87:          */
88:         public static QualifiedName create(final UnqualifiedName first,
89:                         final Name rest,
90:                         final Token identifierToken,
91:                         final Token lastToken) {
92:                 return new QualifiedName(first, rest, identifierToken, lastToken);
93:         }
94:         
95:         /**
96:          * This method returns a {@link Name} consisting of at least one {@link UnqualifiedName}. The returned {@link Name}
97:          * is the name of <code>this</code> without the last part. That is only guaranteed if the method is initially called
98:          * with an empty list as parameter.
99:          *
100:          * @param tokens
101:          * : List of {@link IdentifierToken}.
102:          * @return : Name.
103:          */
104:         public Name getNameWithoutLast(final List<IdentifierToken> tokens) {
105:                 tokens.add(this.getFirst().getIdentifierToken());
106:                 return this.getRest().visit(new NameVisitorReturnName() {
107:                         
108:                         @Override
109:                         public Name handleUnqualifiedName(final UnqualifiedName unqName) {
110:                                 Name name = UnqualifiedName.create(tokens.get(0));
111:                                 for (int i = 1; i < tokens.size(); i++) {
112:                                         name = name.addName(tokens.get(i));
113:                                 }
114:                                 // name.setFirstToken(tokens.get(0));
115:                                 // name.setLastToken(tokens.get(tokens.size()-1));
116:                                 return name;
117:                         }
118:                         
119:                         @Override
120:                         public Name handleQualifiedName(final QualifiedName qName) {
121:                                 return qName.getNameWithoutLast(tokens);
122:                         }
123:                 });
124:         }
125:         
126:         @Override
127:         public Name visit(final NameVisitorReturnName v) {
128:                 return v.handleQualifiedName(this);
129:         }
130:         
131:         @Override
132:         public boolean equals(final Object o) {
133:                 if (o instanceof QualifiedName) {
134:                         final QualifiedName other = (QualifiedName) o;
135:                         return this.getFirst().equals(other.getFirst()) && this.getRest().equals(other.getRest());
136:                 }
137:                 return false;
138:         }
139:         
140:         @Override
141:         public int hashCode() {
142:                 return this.getFirst().hashCode() ^ this.getRest().hashCode();
143:         }
144:         
145:         @Override
146:         public String toString() {
147:                 final StringBuilder result = new StringBuilder();
148:                 result.append(this.getFirst())
149:                                 .append(de.fhdw.wtf.common.constants.parser.AstDescriptionConstants.GREATER_SYMBOL)
150:                                 .append(this.getRest());
151:                 return result.toString();
152:         }
153:         
154:         @Override
155:         public Name addName(final IdentifierToken identifierToken) {
156:                 return QualifiedName.create(this.getFirst(), this.rest.addName(identifierToken));
157:         }
158:         
159:         @Override
160:         public Name addName(final IdentifierToken identifierToken, final Token lastToken) {
161:                 return QualifiedName.create(this.getFirst(), this.rest.addName(identifierToken), identifierToken, lastToken);
162:         }
163:         
164:         @Override
165:         public Name concat(final Name name) {
166:                 return name.visit(new NameVisitorReturnName() {
167:                         
168:                         @Override
169:                         public Name handleUnqualifiedName(final UnqualifiedName unqName) {
170:                                 return QualifiedName.this.addName(unqName.getIdentifierToken());
171:                         }
172:                         
173:                         @Override
174:                         public Name handleQualifiedName(final QualifiedName qName) {
175:                                 Name result;
176:                                 result = QualifiedName.this.addName(qName.getFirst().getIdentifierToken());
177:                                 result = result.concat(qName.getRest());
178:                                 return result;
179:                         }
180:                 });
181:         }
182:         
183:         @Override
184:         public boolean equalsPartial(final Name name) {
185:                 final Name last = this.getLastAddedName();
186:                 return name.visit(new NameVisitorReturnBoolean() {
187:                         
188:                         @Override
189:                         public boolean handleUnqualifiedName(final UnqualifiedName unqName) {
190:                                 return last.equals(unqName);
191:                         }
192:                         
193:                         @Override
194:                         public boolean handleQualifiedName(final QualifiedName qName) {
195:                                 return last.equals(qName.getFirst());
196:                         }
197:                 });
198:         }
199:         
200:         @Override
201:         public UnqualifiedName getLastAddedName() {
202:                 return (UnqualifiedName) this.getRest().visit(new NameVisitorReturnName() {
203:                         @Override
204:                         public Name handleUnqualifiedName(final UnqualifiedName unqName) {
205:                                 return unqName.getLastAddedName();
206:                         }
207:                         
208:                         @Override
209:                         public Name handleQualifiedName(final QualifiedName qName) {
210:                                 return qName.getLastAddedName();
211:                         }
212:                 });
213:         }
214:         
215:         @Override
216:         public void visit(final NameVisitor v) throws InvalidTypeReferenceException {
217:                 v.handle(this);
218:         }
219:         
220:         @Override
221:         public boolean visit(final NameVisitorReturnBoolean v) {
222:                 return v.handleQualifiedName(this);
223:         }
224:         
225:         @Override
226:         public Name visit(final NameVisitorReturnNameException<InvalidTypeReferenceException> v)
227:                         throws InvalidTypeReferenceException {
228:                 return v.handleQualifiedName(this);
229:         }
230:         
231:         // // PROJECTIONS /////
232:         
233:         /**
234:          * The rest of the name. This without first.
235:          *
236:          * @return rest
237:          */
238:         public Name getRest() {
239:                 return this.rest;
240:         }
241:         
242:         /**
243:          * The first {@link UnqualifiedName} of the {@link QualifiedName}.
244:          *
245:          * @return first {@link UnqualifiedName}
246:          */
247:         public UnqualifiedName getFirst() {
248:                 return this.first;
249:         }
250:         
251:         @Override
252:         public Token getFirstToken() {
253:                 return this.getThis().getFirstToken();
254:         }
255:         
256:         @Override
257:         public void setFirstToken(final Token firstToken) {
258:                 this.getThis().setFirstToken(firstToken);
259:         }
260:         
261:         @Override
262:         public Token getLastToken() {
263:                 return this.getThis().getLastToken();
264:         }
265:         
266:         @Override
267:         public void setLastToken(final Token lastToken) {
268:                 this.getThis().setLastToken(lastToken);
269:         }
270:         
271:         /**
272:          * Returned the This of the Multiple Inheritance.
273:          *
274:          * @return this
275:          */
276:         private SyntaxObjectInterface getThis() {
277:                 return this.thiis;
278:         }
279:         
280:         @Override
281:         public <X> X visit(final NameReturnVisitor<X> v) {
282:                 return v.handle(this);
283:         }
284: }