Skip to content

Method: hashCode()

1: package de.fhdw.wtf.generator.java.generatorModel;
2:
3: import de.fhdw.wtf.generator.java.visitor.GenTypeVisitor;
4: import de.fhdw.wtf.generator.java.visitor.GenTypeVisitorException;
5: import de.fhdw.wtf.generator.java.visitor.GenTypeVisitorReturn;
6: import de.fhdw.wtf.generator.java.visitor.GenericTypeStateVisitorReturn;
7:
8: /**
9: * Instances of the class Generic represent Generics like known from Java, that shall for example be generated amongst
10: * some Attributes or Methods.
11: */
12: public final class Generic extends GenType implements Comparable<Generic> {
13:         
14:         /**
15:          * Separates a generic's name and bound.
16:          */
17:         private static final String BOUND_SEPARATOR = " extends ";
18:         
19:         /**
20:          * The name of this Generic.
21:          */
22:         private final String name;
23:         /**
24:          * The state whether the Generic actually has Generics or not. TODO is this correct? Also may correct comments for
25:          * GenericTypeState.
26:          */
27:         private final GenericTypeState type;
28:         
29:         /**
30:          * Creates a new Generic with the given name and type.
31:          *
32:          * @param name
33:          * The name that represents the created Generic.
34:          * @param type
35:          * The type whether the Generic actually has Generics or not. TODO is this correct? Also may correct
36:          * comments for GenericTypeState.
37:          */
38:         private Generic(final String name, final GenericTypeState type) {
39:                 super(name);
40:                 this.name = name;
41:                 this.type = type;
42:         }
43:         
44:         /**
45:          * Creates a new Generic with the given name and type.
46:          *
47:          * @param name
48:          * The name that represents the created Generic.
49:          * @param type
50:          * The type whether the Generic actually has Generics or not. TODO is this correct? Also may correct
51:          * comments for GenericTypeState.
52:          * @return Returns the created Generic.
53:          */
54:         public static Generic create(final String name, final GenericTypeState type) {
55:                 return new Generic(name, type);
56:         }
57:         
58:         /*
59:          * Projections.
60:          */
61:         
62:         /**
63:          * Returns the GenericName of the Generic.
64:          *
65:          * @return GenericName
66:          */
67:         public String getGenericName() {
68:                 
69:                 return this.name;
70:         }
71:         
72:         /**
73:          * Return the type of the Generic.
74:          *
75:          * @return GenericTypeState
76:          */
77:         public GenericTypeState getGenericType() {
78:                 return this.type;
79:         }
80:         
81:         @Override
82:         public String getFullyQualifiedTypeName() {
83:                 
84:                 return this.getName();
85:         }
86:         
87:         /**
88:          * Returns the name of the Type referenced by this generic. Returns the empty string if no real type is referenced
89:          * by this generic
90:          *
91:          * @return the type name.
92:          */
93:         public String getTypeName() {
94:                 final String stateName = this.type.accept(new GenericTypeStateVisitorReturn<String>() {
95:                         
96:                         @Override
97:                         public String handle(final GenHasGenericType hasType) {
98:                                 return hasType.getType().getFullyQualifiedTypeNameWithGenericArguments();
99:                         }
100:                         
101:                         @Override
102:                         public String handle(final GenHasNoGenericType hasNoType) {
103:                                 return "";
104:                         }
105:                         
106:                 });
107:                 return stateName;
108:         }
109:         
110:         @Override
111:         public String getFullyQualifiedTypeNameWithGenericArguments() {
112:                 return this.getFullyQualifiedTypeName();
113:         }
114:         
115:         @Override
116:         public void accept(final GenTypeVisitor visitor) {
117:                 visitor.handle(this);
118:                 
119:         }
120:         
121:         @Override
122:         public <X> X accept(final GenTypeVisitorReturn<X> visitor) {
123:                 return visitor.handle(this);
124:         }
125:         
126:         @Override
127:         public <X extends java.lang.Exception> void accept(final GenTypeVisitorException<X> visitor) throws X {
128:                 visitor.handle(this);
129:         }
130:         
131:         @Override
132:         public boolean equals(final Object obj) {
133:                 if (obj instanceof Generic) {
134:                         final Generic genObj = (Generic) obj;
135:                         if (this.getFullyQualifiedTypeName().equals(genObj.getFullyQualifiedTypeName())
136:                                         && this.getGenericName().equals(genObj.getGenericName())
137:                                         && this.getGenericType().equals(genObj.getGenericType()) && this.getName().equals(genObj.getName())) {
138:                                 return true;
139:                         }
140:                 }
141:                 return false;
142:         }
143:         
144:         @Override
145:         public int hashCode() {
146:                 return this.getFullyQualifiedTypeName().hashCode() ^ this.getGenericName().hashCode()
147:                                 ^ this.getGenericType().hashCode() ^ this.getName().hashCode();
148:         }
149:         
150:         @Override
151:         public int compareTo(final Generic o) {
152:                 return this.getGenericName().compareTo(o.getGenericName());
153:         }
154:         
155:         @Override
156:         public String toString() {
157:                 String result = this.getName();
158:                 result += this.getGenericType().accept(new GenericTypeStateVisitorReturn<String>() {
159:                         @Override
160:                         public String handle(final GenHasGenericType hasType) {
161:                                 return BOUND_SEPARATOR + hasType.getType().getFullyQualifiedTypeName();
162:                         }
163:                         
164:                         @Override
165:                         public String handle(final GenHasNoGenericType hasNoType) {
166:                                 return "";
167:                         }
168:                 });
169:                 return result;
170:         }
171:         
172: }