This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
/
equals.stg
62 lines (56 loc) · 2.05 KB
/
equals.stg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Copyright (C) 2014 41concepts Aps
*
* This is a helper template group file that is responsible for generating the equals method.
**/
method_equals_Object(clazz, method) ::= <<
<@javadoc><JavaDocMethod(method)><@end><@annotations><method.annotations:annotationLine()><@end><\\>
<declare_method(clazz, method)>
{
<@preamble><@end>
<@sameinstancecheck>
if (this == <first(method.parameters).name>)
return true;
<@end>
<@nullcheck>
if (<first(method.parameters).name> == null)
return false;
<@end>
<@supercheck>
<if(!method.declaringType.rootObject)>
if (!super.equals(obj))
return false;
<endif>
<@end>
<@typecheck>
if (getClass() != <first(method.parameters).name>.getClass())
return false;
<@end>
<if(clazz.anyMembers)>
@SuppressWarnings("unchecked")
<clazz.prototypicalName> <uniqueVariableName(clazz,"other")> = (<clazz.prototypicalName>) <first(method.parameters).name>;
<endif>
<@postamble><@end>
return <@returnvalue><if(clazz.anyMembers)>(<clazz.members:{m | <(equalsTemplateNamesByTypeCategory.(m.type.typeCategory))(clazz, m.name, m.type)>}; wrap, anchor, separator=" && ">)<else>true<endif><@end>;
}
>>
equalsTemplateNamesByTypeCategory ::= [ "PRIMITIVE": "equals_primitive", "ARRAY": "equals_array", default: "equals_object"]
equals_primitive(clazz, var, type) ::= <%
<if(type.primitiveFloat)>
(Float.floatToIntBits(<var>) == Float.floatToIntBits(<uniqueVariableName(clazz,"other")>.<var>))
<elseif(type.primitiveDouble)>
(Double.doubleToLongBits(<var>) == Double.doubleToLongBits(<uniqueVariableName(clazz,"other")>.<var>))
<else>
(<var> == <uniqueVariableName(clazz,"other")>.<var>)
<endif>
%>
equals_array(clazz, var, type) ::= <%
<if(type.multiDimensionalArray)>
<clazz.helperTypes.javaUtilArrays.name>.deepEquals(<var>, <uniqueVariableName(clazz,"other")>.<var>)
<else>
<clazz.helperTypes.javaUtilArrays.name>.equals(<var>, <uniqueVariableName(clazz,"other")>.<var>)
<endif>
%>
equals_object(clazz, var, type) ::= <%
<clazz.helperTypes.javaUtilObjects.name>.equals(<var>, <uniqueVariableName(clazz,"other")>.<var>)
%>