77import com .regnosys .rosetta .types .REnumType ;
88import com .regnosys .rosetta .types .RType ;
99
10+ /**
11+ * A utility class for mapping Rune (Rosetta) types and attributes to their
12+ * corresponding Python types.
13+ * This class also handles name mangling for Python keywords and reserved words.
14+ */
1015public class RuneToPythonMapper {
1116 private static final Set <String > PYTHON_KEYWORDS = new HashSet <>();
1217
18+ /**
19+ * Private constructor to prevent instantiation of this utility class.
20+ */
21+ private RuneToPythonMapper () {
22+ }
23+
1324 static {
1425 // Initialize the set with Python keywords and soft keywords
1526 PYTHON_KEYWORDS .add ("False" );
@@ -53,23 +64,31 @@ public class RuneToPythonMapper {
5364 PYTHON_KEYWORDS .add ("_" );
5465 }
5566
56- // Define the set of Python types as a static final field
67+ /**
68+ * Define the set of Python types as a static final field.
69+ */
5770 private static final Set <String > PYTHON_TYPES = Set .of (
58- "int" , "str" , "Decimal" , "date" , "datetime" , "datetime.datetime" ,
59- "datetime.date" , "datetime.time" , "time" , "bool"
60- );
71+ "int" , "str" , "Decimal" , "date" , "datetime" , "datetime.datetime" ,
72+ "datetime.date" , "datetime.time" , "time" , "bool" );
6173
74+ /**
75+ * Check if the attribute is a Python keyword or starts with an underscore.
76+ *
77+ * @param attrib the attribute name
78+ * @return the mangled name
79+ */
6280 public static String mangleName (String attrib ) {
63- // Check if the attribute is a Python keyword or starts with an underscore
6481 if (PYTHON_KEYWORDS .contains (attrib ) || attrib .charAt (0 ) == '_' ) {
6582 return "rune_attr_" + attrib ;
6683 }
6784 return attrib ;
6885 }
69-
86+
87+ /**
88+ * Inner private function to convert from Rosetta type to Python type.
89+ * Returns null if no matching type.
90+ */
7091 private static String toPythonBasicTypeInnerFunction (String rosettaType ) {
71- // inner private function to convert from Rosetta type to Python type
72- // returns null if no matching type
7392 switch (rosettaType ) {
7493 case "string" :
7594 case "eventType" :
@@ -94,9 +113,14 @@ private static String toPythonBasicTypeInnerFunction(String rosettaType) {
94113 }
95114 }
96115
97- public static String getAttributeTypeWithMeta (String attributeType ) {
98- // inner private function to convert from Rosetta type to Python type
99- // returns null if no matching type
116+ /**
117+ * Inner private function to convert from Rosetta type to Python type.
118+ * Returns null if no matching type.
119+ *
120+ * @param attributeType the attribute type
121+ * @return the Python type with meta, or the original attribute type if no match
122+ */
123+ public static String getAttributeTypeWithMeta (String attributeType ) {
100124 switch (attributeType ) {
101125 case "str" :
102126 return "StrWithMeta" ;
@@ -116,37 +140,77 @@ public static String getAttributeTypeWithMeta (String attributeType) {
116140 return attributeType ;
117141 }
118142 }
143+
144+ /**
145+ * Convert from Rune type as string to Python type.
146+ *
147+ * @param rosettaType the Rune type name
148+ * @return the Python type name
149+ */
119150 public static String toPythonBasicType (String rosettaType ) {
120151 String pythonType = toPythonBasicTypeInnerFunction (rosettaType );
121152 return (pythonType == null ) ? rosettaType : pythonType ;
122153 }
123154
155+ /**
156+ * Convert from Rune RType to Python type.
157+ *
158+ * @param rt the Rune RType object
159+ * @return the Python type name string, or null if rt is null
160+ */
124161 public static String toPythonType (RType rt ) {
125162 if (rt == null )
126163 return null ;
127164 var pythonType = toPythonBasicTypeInnerFunction (rt .getName ());
128165 if (pythonType == null ) {
129- String rtName = rt .getName ();
166+ String rtName = rt .getName ();
130167 pythonType = rt .getNamespace ().toString () + "." + rtName ;
131168 pythonType = (rt instanceof REnumType ) ? pythonType + "." + rtName : pythonType ;
132169 }
133170 return pythonType ;
134171 }
172+
173+ /**
174+ * Check if the given type is a basic type.
175+ *
176+ * @param rtName the type name
177+ * @return true if it is a basic type, false otherwise
178+ */
135179 public static boolean isRosettaBasicType (String rtName ) {
136180 return (toPythonBasicTypeInnerFunction (rtName ) != null );
137181 }
182+
183+ /**
184+ * Check if the given type is a basic type.
185+ *
186+ * @param rt the RType object
187+ * @return true if it is a basic type, false otherwise
188+ */
138189 public static boolean isRosettaBasicType (RType rt ) {
139190 return (toPythonBasicTypeInnerFunction (rt .getName ()) != null );
140191 }
192+
193+ /**
194+ * Check if the given attribute is a basic type.
195+ *
196+ * @param ra the RAttribute object
197+ * @return true if the attribute's type is a basic type, false otherwise
198+ */
141199 public static boolean isRosettaBasicType (RAttribute ra ) {
142- if (ra == null ) {
200+ if (ra == null ) {
143201 return false ;
144202 }
145203 RType rt = ra .getRMetaAnnotatedType ().getRType ();
146204 return (rt != null ) ? isRosettaBasicType (rt .getName ()) : false ;
147205 }
206+
207+ /**
208+ * Check if the given type is in the set of Python types.
209+ *
210+ * @param pythonType the Python type name
211+ * @return true if it is a known Python type, false otherwise
212+ */
148213 public static boolean isPythonBasicType (final String pythonType ) {
149- // Check if the given type is in the set of Python types
150214 return PYTHON_TYPES .contains (pythonType );
151215 }
152216}
0 commit comments