11import re
22from typing import List
33
4- from src .java_model import EnumClass , indent_lvl1 , indent_lvl2 , indent_lvl3
5- from src .header_generator import set_package
4+ from src .header_generator import set_package , render_javadoc
5+ from src .java_model import EnumClass , indent
66
77
88def to_java_constant (value : str ) -> str :
@@ -21,109 +21,89 @@ def generate_enum_class(enum_class: EnumClass, package: str) -> str:
2121 "" ,
2222 f"import java.util.HashMap;" ,
2323 f"import java.util.Map;" ,
24+ f"" ,
25+ render_javadoc (enum_class .description , indent_lvl = 0 ),
26+ f"public enum { enum_class .name } {{" ,
27+ _get_constants (enum_class .values ),
28+ f"" ,
29+ f"{ indent (1 )} private final static Map<String, { enum_class .name } > CONSTANTS = new HashMap<String, { enum_class .name } >();" ,
30+ _get_static_method (enum_class .name ),
31+ f"" ,
32+ f"{ indent (1 )} private final String value;" ,
33+ _get_constructor (enum_class .name ),
34+ _get_from_value_method (enum_class .name ),
35+ _get_to_string_method (),
36+ _get_value_method (),
37+ "}" ,
2438 f""
2539 ]
2640
27- enum_body .extend (_get_javadoc (enum_class .description ))
28-
29- enum_body .append (f"public enum { enum_class .name } {{" )
30- enum_body .extend (_get_constants (enum_class .values ))
31-
32- enum_body .append (f"" )
33- enum_body .append (
34- f"{ indent_lvl1 } private final static Map<String, { enum_class .name } > CONSTANTS = new HashMap<String, { enum_class .name } >();" )
35- enum_body .append (f"" )
36- enum_body .extend (_get_static_method (enum_class .name ))
37-
38- enum_body .append (f"" )
39- enum_body .append (f"{ indent_lvl1 } private final String value;" )
40- enum_body .append (f"" )
41- enum_body .extend (_get_constructor (enum_class .name ))
42-
43- enum_body .append (f"" )
44- enum_body .extend (_get_from_value_method (enum_class .name ))
45-
46- enum_body .append (f"" )
47- enum_body .extend (_get_to_string_method ())
48-
49- enum_body .append (f"" )
50- enum_body .extend (_get_value_method ())
51- enum_body .append ("}" )
52- enum_body .append (f"" )
53-
5441 return "\n " .join (enum_body )
5542
5643
57- def _get_javadoc (description : str ) -> List [str ]:
58- javadoc = ["" ]
59- if description is not None :
60- javadoc = [
61- "" ,
62- "/**" ,
63- f" * { description } " ,
64- " */"
65- ]
66- return javadoc
67-
68-
69- def _get_constants (constants : List [str ]) -> List [str ]:
44+ def _get_constants (constants : List [str ]) -> str :
7045 values = []
7146 for i , value in enumerate (constants ):
7247 line_end = ";" if i == (len (constants ) - 1 ) else ","
7348 values .append (
74- f'{ indent_lvl1 } { to_java_constant (value )} ("{ value } "){ line_end } '
49+ f'{ indent ( 1 ) } { to_java_constant (value )} ("{ value } "){ line_end } '
7550 )
76- return values
51+ return " \n " . join ( values )
7752
7853
79- def _get_static_method (class_name : str ) -> List [ str ] :
54+ def _get_static_method (class_name : str ) -> str :
8055 body = [
81- f"{ indent_lvl1 } static {{" ,
82- f"{ indent_lvl2 } for ({ class_name } c : values()) {{" ,
83- f"{ indent_lvl3 } CONSTANTS.put(c.value, c);" ,
84- f"{ indent_lvl2 } }}" ,
85- f"{ indent_lvl1 } }}"
56+ "" ,
57+ f"{ indent (1 )} static {{" ,
58+ f"{ indent (2 )} for ({ class_name } c : values()) {{" ,
59+ f"{ indent (3 )} CONSTANTS.put(c.value, c);" ,
60+ f"{ indent (2 )} }}" ,
61+ f"{ indent (1 )} }}"
8662 ]
87- return body
63+ return " \n " . join ( body )
8864
8965
90- def _get_constructor (class_name : str ) -> List [ str ] :
66+ def _get_constructor (class_name : str ) -> str :
9167 body = [
92- f"{ indent_lvl1 } { class_name } (String value) {{" ,
93- f"{ indent_lvl2 } this.value = value;" ,
94- f"{ indent_lvl1 } }}"
68+ "" ,
69+ f"{ indent (1 )} { class_name } (String value) {{" ,
70+ f"{ indent (2 )} this.value = value;" ,
71+ f"{ indent (1 )} }}"
9572 ]
96- return body
73+ return " \n " . join ( body )
9774
9875
99- def _get_from_value_method (class_name : str ) -> List [ str ] :
76+ def _get_from_value_method (class_name : str ) -> str :
10077 body = [
101- f"{ indent_lvl1 } public static { class_name } fromValue(String value) {{" ,
102- f"{ indent_lvl2 } { class_name } constant = CONSTANTS.get(value);" ,
103- f"{ indent_lvl2 } if (constant == null) {{" ,
104- f"{ indent_lvl3 } throw new IllegalArgumentException(value);" ,
105- f"{ indent_lvl2 } }} else {{" ,
106- f"{ indent_lvl3 } return constant;" ,
107- f"{ indent_lvl2 } }}" ,
108- f"{ indent_lvl1 } }}"
78+ "" ,
79+ f"{ indent (1 )} public static { class_name } fromValue(String value) {{" ,
80+ f"{ indent (2 )} { class_name } constant = CONSTANTS.get(value);" ,
81+ f"{ indent (2 )} if (constant == null) {{" ,
82+ f"{ indent (3 )} throw new IllegalArgumentException(value);" ,
83+ f"{ indent (2 )} }} else {{" ,
84+ f"{ indent (3 )} return constant;" ,
85+ f"{ indent (2 )} }}" ,
86+ f"{ indent (1 )} }}"
10987 ]
110- return body
88+ return " \n " . join ( body )
11189
11290
113- def _get_to_string_method () -> List [ str ] :
91+ def _get_to_string_method () -> str :
11492 body = [
115- f"{ indent_lvl1 } @Override" ,
116- f"{ indent_lvl1 } public String toString() {{" ,
117- f"{ indent_lvl2 } return this.value;" ,
118- f"{ indent_lvl1 } }}"
93+ "" ,
94+ f"{ indent (1 )} @Override" ,
95+ f"{ indent (1 )} public String toString() {{" ,
96+ f"{ indent (2 )} return this.value;" ,
97+ f"{ indent (1 )} }}"
11998 ]
120- return body
99+ return " \n " . join ( body )
121100
122101
123- def _get_value_method () -> List [ str ] :
102+ def _get_value_method () -> str :
124103 body = [
125- f"{ indent_lvl1 } public String value() {{" ,
126- f"{ indent_lvl2 } return this.value;" ,
127- f"{ indent_lvl1 } }}"
104+ "" ,
105+ f"{ indent (1 )} public String value() {{" ,
106+ f"{ indent (2 )} return this.value;" ,
107+ f"{ indent (1 )} }}"
128108 ]
129- return body
109+ return " \n " . join ( body )
0 commit comments