1717import edu .ie3 .datamodel .models .input .system .characteristic .CharacteristicInput ;
1818import edu .ie3 .datamodel .models .profile .LoadProfile ;
1919import edu .ie3 .datamodel .models .voltagelevels .VoltageLevel ;
20+ import edu .ie3 .datamodel .utils .Try ;
21+ import edu .ie3 .datamodel .utils .Try .*;
22+ import edu .ie3 .util .exceptions .QuantityException ;
2023import java .beans .Introspector ;
2124import java .lang .reflect .InvocationTargetException ;
2225import java .lang .reflect .Method ;
@@ -72,7 +75,7 @@ public abstract class Processor<T> {
7275 *
7376 * @param foreSeenClass Class and its children that are foreseen to be handled with this processor
7477 */
75- protected Processor (Class <? extends T > foreSeenClass ) {
78+ protected Processor (Class <? extends T > foreSeenClass ) throws EntityProcessorException {
7679 if (!getEligibleEntityClasses ().contains (foreSeenClass ))
7780 throw new EntityProcessorException (
7881 "Cannot register class '"
@@ -104,9 +107,10 @@ public int compare(String a, String b) {
104107 * Maps the foreseen table fields to the objects getters
105108 *
106109 * @param cls class to use for mapping
107- * @return an array of strings of all field values of the class
110+ * @return a map of all field values of the class
108111 */
109- protected SortedMap <String , Method > mapFieldNameToGetter (Class <?> cls ) {
112+ protected SortedMap <String , Method > mapFieldNameToGetter (Class <?> cls )
113+ throws EntityProcessorException {
110114 return mapFieldNameToGetter (cls , Collections .emptyList ());
111115 }
112116
@@ -115,10 +119,10 @@ protected SortedMap<String, Method> mapFieldNameToGetter(Class<?> cls) {
115119 *
116120 * @param cls class to use for mapping
117121 * @param ignoreFields A collection of all field names to ignore during process
118- * @return an array of strings of all field values of the class
122+ * @return a map of all field values of the class
119123 */
120124 protected SortedMap <String , Method > mapFieldNameToGetter (
121- Class <?> cls , Collection <String > ignoreFields ) {
125+ Class <?> cls , Collection <String > ignoreFields ) throws EntityProcessorException {
122126 try {
123127 final LinkedHashMap <String , Method > resFieldNameToMethod = new LinkedHashMap <>();
124128 Arrays .stream (Introspector .getBeanInfo (cls , Object .class ).getPropertyDescriptors ())
@@ -178,7 +182,7 @@ public static <V> SortedMap<String, V> putUuidFirst(Map<String, V> unsorted) {
178182 * @return Mapping from field name to value as String representation
179183 */
180184 protected LinkedHashMap <String , String > processObject (
181- Object object , Map <String , Method > fieldNameToGetter ) {
185+ Object object , Map <String , Method > fieldNameToGetter ) throws EntityProcessorException {
182186 try {
183187 LinkedHashMap <String , String > resultMap = new LinkedHashMap <>();
184188 for (Map .Entry <String , Method > entry : fieldNameToGetter .entrySet ()) {
@@ -207,7 +211,8 @@ protected LinkedHashMap<String, String> processObject(
207211 * @param fieldName Name of the foreseen field
208212 * @return A String representation of the result
209213 */
210- protected String processMethodResult (Object methodReturnObject , Method method , String fieldName ) {
214+ protected String processMethodResult (Object methodReturnObject , Method method , String fieldName )
215+ throws EntityProcessorException {
211216
212217 StringBuilder resultStringBuilder = new StringBuilder ();
213218
@@ -232,15 +237,19 @@ protected String processMethodResult(Object methodReturnObject, Method method, S
232237 .map (
233238 o -> {
234239 if (o instanceof Quantity <?>) {
235- return handleQuantity ((Quantity <?>) o , fieldName );
240+ return Try .of (
241+ () -> handleQuantity ((Quantity <?>) o , fieldName ),
242+ EntityProcessorException .class );
236243 } else {
237- throw new EntityProcessorException (
238- "Handling of "
239- + o .getClass ().getSimpleName ()
240- + ".class instance wrapped into Optional is currently not supported by entity processors!" );
244+ return Failure .of (
245+ new EntityProcessorException (
246+ "Handling of "
247+ + o .getClass ().getSimpleName ()
248+ + ".class instance wrapped into Optional is currently not supported by entity processors!" ));
241249 }
242250 })
243- .orElse ("" ));
251+ .orElse (Success .of ("" )) // (in case of empty optional)
252+ .getOrThrow ());
244253 case "ZonedDateTime" -> resultStringBuilder .append (
245254 processZonedDateTime ((ZonedDateTime ) methodReturnObject ));
246255 case "OperationTime" -> resultStringBuilder .append (
@@ -306,7 +315,8 @@ protected String processMethodResult(Object methodReturnObject, Method method, S
306315 * @return the resulting string of a VoltageLevel attribute value for the provided field or an
307316 * empty string when an invalid field name is provided
308317 */
309- protected String processVoltageLevel (VoltageLevel voltageLevel , String fieldName ) {
318+ protected String processVoltageLevel (VoltageLevel voltageLevel , String fieldName )
319+ throws EntityProcessorException {
310320
311321 StringBuilder resultStringBuilder = new StringBuilder ();
312322 if (fieldName .equalsIgnoreCase (VOLT_LVL )) resultStringBuilder .append (voltageLevel .getId ());
@@ -324,21 +334,26 @@ protected String processVoltageLevel(VoltageLevel voltageLevel, String fieldName
324334 * @return an optional string with the normalized to {@link StandardUnits} value of the quantity
325335 * or empty if an error occurred during processing
326336 */
327- protected String handleQuantity (Quantity <?> quantity , String fieldName ) {
328- Optional <String > optQuant ;
337+ protected String handleQuantity (Quantity <?> quantity , String fieldName )
338+ throws EntityProcessorException {
339+ Try <String , QuantityException > optQuant ;
329340 if (specificQuantityFieldNames .contains (fieldName )) {
330341 optQuant = handleProcessorSpecificQuantity (quantity , fieldName );
331342 } else {
332- optQuant = quantityValToOptionalString (quantity );
343+ optQuant = Success . of ( quantityValToOptionalString (quantity ) );
333344 }
334- return optQuant .orElseThrow (
335- () ->
336- new EntityProcessorException (
337- "Unable to process quantity value for attribute '"
338- + fieldName
339- + "' in entity "
340- + getRegisteredClass ().getSimpleName ()
341- + ".class." ));
345+
346+ return optQuant
347+ .transformF (
348+ e ->
349+ new EntityProcessorException (
350+ "Unable to process quantity value for attribute '"
351+ + fieldName
352+ + "' in entity "
353+ + getRegisteredClass ().getSimpleName ()
354+ + ".class." ,
355+ e ))
356+ .getOrThrow ();
342357 }
343358
344359 /**
@@ -354,7 +369,7 @@ protected String handleQuantity(Quantity<?> quantity, String fieldName) {
354369 * @return an optional string with the normalized to {@link StandardUnits} value of the quantity
355370 * or empty if an error occurred during processing
356371 */
357- protected abstract Optional <String > handleProcessorSpecificQuantity (
372+ protected abstract Try <String , QuantityException > handleProcessorSpecificQuantity (
358373 Quantity <?> quantity , String fieldName );
359374
360375 protected String processUUIDArray (UUID [] uuids ) {
@@ -406,8 +421,8 @@ protected String processZonedDateTime(ZonedDateTime zonedDateTime) {
406421 * @param quantity Quantity to convert
407422 * @return A string of the quantity's value
408423 */
409- protected Optional < String > quantityValToOptionalString (Quantity <?> quantity ) {
410- return Optional . of ( Double .toString (quantity .getValue ().doubleValue () ));
424+ protected String quantityValToOptionalString (Quantity <?> quantity ) {
425+ return Double .toString (quantity .getValue ().doubleValue ());
411426 }
412427
413428 /**
0 commit comments