forked from apache/plc4x
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenum-template.java.ftlh
executable file
·164 lines (147 loc) · 7.6 KB
/
enum-template.java.ftlh
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<#--
<#--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<#-- Prevent freemarker from escaping stuff -->
<#outputformat "undefined">
<#-- Declare the name and type of variables passed in to the template -->
<#-- @ftlvariable name="languageName" type="java.lang.String" -->
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.java.JavaLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.EnumTypeDefinition" -->
${helper.packageName(protocolName, languageName, outputFlavor)?replace(".", "/")}/${type.name}.java
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package ${helper.packageName(protocolName, languageName, outputFlavor)};
import org.apache.plc4x.java.spi.generation.Message;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// Code generated by code-generation. DO NOT EDIT.
public enum ${type.name} {
<@compress single_line=true>
<#list type.enumValues as enumValue>
${enumValue.name}(
<#if type.type.isPresent()>
(${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)})
<#if type.type.orElseThrow().isNonSimpleTypeReference()>
<#if type.type.orElseThrow().isEnumTypeReference()>
${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)}.${enumValue.value}
<#else>
${enumValue.value}
</#if>
<#else>
${enumValue.value}<#if helper.needsLongMarker(type.type)>L</#if>
</#if>
</#if>
<#if type.constantNames?has_content>
<#if type.type?has_content>, </#if>
<#list type.constantNames as constantName>
<#if type.getConstantType(constantName).isNonSimpleTypeReference()>
<#if helper.escapeValue(type.getConstantType(constantName), enumValue.getConstant(constantName).orElse(null)) == 'null'>
null
<#elseif type.getConstantType(constantName).isEnumTypeReference()>
${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)}.${helper.escapeValue(type.getConstantType(constantName), enumValue.getConstant(constantName).orElseThrow())}<#else>(${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)}) ${helper.escapeValue(type.getConstantType(constantName), enumValue.getConstant(constantName).orElseThrow())}</#if><#else>(${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)}) ${helper.escapeValue(type.getConstantType(constantName), enumValue.getConstant(constantName).orElseThrow())}
</#if>
<#sep>, </#sep>
</#list>
</#if>)
<#sep>, </#sep>
</#list>;
</@compress>
<#if type.type.isPresent()>
private static final Map<${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), false)}, ${type.name}> map;
static {
map = new HashMap<>();
for (${type.name} value : ${type.name}.values()) {
map.put((${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)}) value.getValue(), value);
}
}
private ${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)} value;
</#if>
<#if type.constantNames?has_content>
<#list type.constantNames as constantName>
private ${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)} ${constantName};
</#list>
</#if>
${type.name}(<#if type.type.isPresent()>${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)} value</#if><#if type.constantNames?has_content><#if type.type?has_content>, </#if><#list type.constantNames as constantName>${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)} ${constantName}<#sep>, </#sep></#list></#if>) {
<#if type.type.isPresent()> this.value = value;</#if>
<#if type.constantNames?has_content>
<#list type.constantNames as constantName>
this.${constantName} = ${constantName};
</#list>
</#if>
}
<#if type.type.isPresent()>
public ${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)} getValue() {
return value;
}
</#if>
<#if type.constantNames?has_content>
<#list type.constantNames as constantName>
public ${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)} get${constantName?cap_first}() {
return ${constantName};
}
public static ${type.name} firstEnumForField${constantName?cap_first}(${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)} fieldValue) {
for (${type.name} _val : ${type.name}.values()) {
if(_val.get${constantName?cap_first}() == fieldValue) {
return _val;
}
}
return null;
}
public static List<${type.name}> enumsForField${constantName?cap_first}(${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)} fieldValue) {
List<${type.name}> _values = new ArrayList();
for (${type.name} _val : ${type.name}.values()) {
if(_val.get${constantName?cap_first}() == fieldValue) {
_values.add(_val);
}
}
return _values;
}
</#list>
</#if>
<#if type.type.isPresent()>
public static ${type.name} enumForValue(${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)} value) {
return map.get(value);
}
</#if>
<#if type.type.isPresent()>
public static Boolean isDefined(${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)} value) {
return map.containsKey(value);
}
</#if>
}
</#outputformat>