Debian Codegraph @

doc/org.eclipse.emf.examples.jet.article2/templates/TypeSafeEnumeration.javajet

raw
1 <%@ jet package="translated" imports="java.util.* org.eclipse.emf.examples.jet.article2.model.*" class="TypeSafeEnumeration" %>
2 <% TypesafeEnum type = (TypesafeEnum) argument; %>
3 <% if (type.getPackageName().length() > 0) { %>
4 package <%=type.getPackageName()%>;
5 <% } %>
6
7 /**
8 * This final class implements a type-safe enumeration
9 * over the valid instances of a <%=type.getClassName()%>.
10 * Instances of this class are immutable.
11 *
12 <% if (type.getAuthor().length() > 0) { %>
13 * @author <%=type.getAuthor()%>
14 <% } %>
15 <% if (type.getVersion().length() > 0) { %>
16 * @version <%=type.getVersion()%>
17 <% } %>
18 */
19 public final class <%=type.getClassName()%> {
20 <% for (Iterator<Instance> i = type.instances(); i.hasNext(); ) { %>
21 <% Instance instance = i.next(); %>
22
23 /**
24 * <%=type.getClassName()%> with <%=instance.keyDescription()%>
25 */
26 public static final <%=type.getClassName()%> <%=instance.getName()%> = new <%=type.getClassName()%>(<%=instance.constructorValues()%>);
27 <% } %>
28
29 private static final <%=type.getClassName()%>[] ALL = new <%=type.getClassName()%>[] {
30 <% for (Iterator<Instance> i = type.instances(); i.hasNext(); ) { %>
31 <% Instance instance = i.next(); %>
32 <%=instance.getName()%>,
33 <% } %>
34 };
35
36 <% for (Iterator<Attribute> i = type.attributes(); i.hasNext(); ) { %>
37 <% Attribute attribute = i.next(); %>
38 private final <%=attribute.getType()%> m<%=attribute.getCappedName()%>;
39 <% } %>
40
41 /**
42 * Private constructor.
43 */
44 private <%=type.getClassName()%>(<%=type.constructorParameterDescription()%>) {
45 <% for (Iterator<Attribute> i = type.attributes(); i.hasNext(); ) { %>
46 <% Attribute attribute = i.next(); %>
47 m<%=attribute.getCappedName()%> = <%=attribute.getUncappedName()%>;
48 <% } %>
49 }
50
51 /**
52 * Returns a copy of the array with all instances of this class.
53 * <p>
54 * Modifying the returned array will not affect this class.
55 *
56 * @return an array with all instances of this class
57 */
58 public static <%=type.getClassName()%>[] all() {
59
60 <%=type.getClassName()%>[] result = new <%=type.getClassName()%>[ALL.length];
61 System.arraycopy(ALL, 0, result, 0, ALL.length);
62 return result;
63 }
64
65 /**
66 * Returns the <code><%=type.getClassName()%></code> for the specified key field(s),
67 <% if (type.hasDefaultInstance()) { %>
68 * or returns <code><%=type.getDefaultInstance().getName()%></code>
69 <% } else { %>
70 * or throws an IllegalArgumentException
71 <% } %>
72 * if no <code><%=type.getClassName()%></code> exists for the specified key field(s).
73 *
74 <% for (Iterator i = type.keyAttributes(); i.hasNext(); ) { %>
75 <% Attribute attribute = (Attribute) i.next(); %>
76 * @param <%=attribute.getUncappedName()%> the <%=attribute.getUncappedName()%> of the <code><%=type.getClassName()%></code> to find
77 <% } %>
78 * @return the <code><%=type.getClassName()%></code> for the specified key field(s)
79 */
80 public static <%=type.getClassName()%> lookup(<%=type.keyParameterDescription()%>) {
81 for (int i = 0; i < ALL.length; i++) {
82 <% for (Iterator<Attribute> i = type.keyAttributes(); i.hasNext(); ) { %>
83 <% Attribute attribute = i.next(); %>
84 <% if (attribute.isBoolean() || attribute.isByte() || attribute.isChar() || attribute.isShort() || attribute.isInt() || attribute.isLong()) { %>
85 if (ALL[i].<%=attribute.toGetMethod()%>() != <%=attribute.getUncappedName()%>) {
86 <% } else if (attribute.isDouble()) { %>
87 if (Double.doubleToLongBits(ALL[i].<%=attribute.toGetMethod()%>()) != Double.doubleToLongBits(<%=attribute.getUncappedName()%>)) {
88 <% } else if (attribute.isFloat()) { %>
89 if (Float.floatToIntBits(ALL[i].<%=attribute.toGetMethod()%>()) != Float.floatToIntBits(<%=attribute.getUncappedName()%>)) {
90 <% } else { %>
91 if (ALL[i].<%=attribute.toGetMethod()%>() == null ? <%=attribute.getUncappedName()%> != null : !ALL[i].<%=attribute.toGetMethod()%>().equals(<%=attribute.getUncappedName()%>)) {
92 <% } %>
93 continue;
94 }
95 <% } %>
96 return ALL[i];
97 }
98 <% if (type.hasDefaultInstance()) { %>
99 return <%=type.getDefaultInstance().getName()%>;
100 <% } else { %>
101 StringBuilder msg = new StringBuilder();
102 <% for (Iterator<Attribute> i = type.keyAttributes(); i.hasNext(); ) { %>
103 <% Attribute attribute = i.next(); %>
104 msg.append("<%=attribute.getUncappedName()%>=").append(<%=attribute.getUncappedName()%>);
105 <% if (i.hasNext()) { %>
106 msg.append(", ");
107 <% } %>
108 <% } %>
109 throw new IllegalArgumentException("No <%=type.getClassName()%> found for " + msg);
110 <% } %>
111 }
112
113 <% for (Iterator<Instance> i = type.instances(); i.hasNext(); ) { %>
114 <% Instance instance = i.next(); %>
115 /**
116 * Returns whether this instance is the {@link <%=type.getClassName()%>#<%=instance.getName()%>} instance.
117 *
118 * @return whether this instance is the {@link <%=type.getClassName()%>#<%=instance.getName()%>} instance
119 */
120 public boolean is<%=instance.getCappedName()%>() {
121 return this == <%=type.getClassName()%>.<%=instance.getName()%>;
122 }
123 <% } %>
124
125 <% for (Iterator<Attribute> i = type.attributes(); i.hasNext(); ) { %>
126 <% Attribute attribute = i.next(); %>
127 /**
128 * Returns the <%=attribute.getName()%>.
129 *
130 * @return the <%=attribute.getName()%>.
131 */
132 public <%=attribute.getType()%> <%=attribute.toGetMethod()%>() {
133 return m<%=attribute.getCappedName()%>;
134 }
135
136 <% } %>
137 /**
138 * Returns a String description of this <code><%=type.getClassName()%></code>.
139 *
140 * @return a String description of this object.
141 */
142 @Override
143 public String toString() {
144
145 StringBuilder result = new StringBuilder();
146 result.append("<%=type.getClassName()%>[");
147 <% for (Iterator<Attribute> i = type.attributes(); i.hasNext(); ) { %>
148 <% Attribute attribute = i.next(); %>
149 result.append("<%=attribute.getName()%>=").append(<%=attribute.toGetMethod()%>());
150 <% if (i.hasNext()) { %>
151 result.append(", ");
152 <% } %>
153 <% } %>
154 result.append("]");
155 return result.toString();
156 }
157
158 /*
159 * Non-javadoc.
160 * @see java.lang.Object#equals(Object)
161 */
162 // Algorithm from "Effective Java" by Joshua Bloch.
163 @Override
164 public boolean equals(Object o) {
165 if (o == this) {
166 return true;
167 }
168 if (!(o instanceof <%=type.getClassName()%>)) {
169 return false;
170 }
171 <%=type.getClassName()%> other = (<%=type.getClassName()%>) o;
172 return true
173 <% for (Iterator<Attribute> keys = type.keyAttributes(); keys.hasNext(); ) { %>
174 <% Attribute attribute = keys.next(); %>
175 <% if (attribute.isBoolean() || attribute.isByte() || attribute.isChar() || attribute.isShort() || attribute.isInt() || attribute.isLong()) { %>
176 && <%=attribute.toGetMethod()%>() == other.<%=attribute.toGetMethod()%>()
177 <% } else if (attribute.isDouble()) { %>
178 && (Double.doubleToLongBits(<%=attribute.toGetMethod()%>()) == Double.doubleToLongBits(other.<%=attribute.toGetMethod()%>()))
179 <% } else if (attribute.isFloat()) { %>
180 && (Float.floatToIntBits(<%=attribute.toGetMethod()%>()) == Float.floatToIntBits(other.<%=attribute.toGetMethod()%>())
181 <% } else { %>
182 && (<%=attribute.toGetMethod()%>() == null ? other.<%=attribute.toGetMethod()%>() == null : <%=attribute.toGetMethod()%>().equals(other.<%=attribute.toGetMethod()%>()))
183 <% } %>
184 <% } %>
185 ;
186 }
187
188 /*
189 * Non-javadoc.
190 * @see java.lang.Object#hashCode()
191 */
192 // Algorithm from "Effective Java" by Joshua Bloch.
193 @Override
194 public int hashCode() {
195 int result = 17 * getClass().getName().hashCode();
196 <% for (Iterator<Attribute> keys = type.keyAttributes(); keys.hasNext(); ) { %>
197 <% Attribute attribute = keys.next(); %>
198 <% if (attribute.isBoolean()) { %>
199 result = 37 * result + (<%=attribute.toGetMethod()%>() ? 1 : 0);
200 <% } else if (attribute.isInt()) { %>
201 result = 37 * result + <%=attribute.toGetMethod()%>();
202 <% } else if (attribute.isByte() || attribute.isChar() || attribute.isShort()) { %>
203 result = 37 * result + ((int) <%=attribute.toGetMethod()%>());
204 <% } else if (attribute.isLong()) { %>
205 result = 37 * result + ((int) (<%=attribute.toGetMethod()%>() ^ (<%=attribute.toGetMethod()%>() >>> 32)));
206 <% } else if (attribute.isDouble()) { %>
207 result = 37 * result + ((int) (Double.doubleToLongBits(<%=attribute.toGetMethod()%>() ) ^ (Double.doubleToLongBits(<%=attribute.toGetMethod()%>()) >>> 32)));
208 <% } else if (attribute.isFloat()) { %>
209 result = 37 * result + Float.floatToIntBits(<%=attribute.toGetMethod()%>());
210 <% } else { %>
211 result = 37 * result + (<%=attribute.toGetMethod()%>() == null ? 0 : <%=attribute.toGetMethod()%>().hashCode());
212 <% } %>
213 <% } %>
214 return result;
215 }
216
217 /**
218 * If this class implements <code>java.io.Serializable</code>,
219 * the Java serialization mechanism provides a "hidden constructor".
220 * To ensure that no other instances are created than the
221 * ones declared above, we implement <code>readResolve</code>.
222 * (This is not necessary if this class does not
223 * implement <code>java.io.Serializable</code>).
224 */
225 // Algorithm from "Effective Java" by Joshua Bloch.
226 private Object readResolve() throws java.io.ObjectStreamException {
227
228 // look at the key attribute values of the instance
229 // that was just deserialized,
230 // and replace the deserialized instance
231 // with one of the static objects
232 return lookup(<%
233
234 for (Iterator<Attribute> keys = type.keyAttributes(); keys.hasNext(); ) {
235 Attribute attribute = keys.next();
236 %>m<%=attribute.getCappedName()%><%
237
238 if (keys.hasNext()) { %>, <% }
239 }
240 %>);
241 }
242 }