Skip to content
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
df74670
Make ``handleEntity`` throw exceptions instead of returning empty opt…
staudtMarius Apr 24, 2023
1a207d7
Fix failing test.
staudtMarius Apr 24, 2023
546edb4
Fix quality gate issues.
staudtMarius Apr 26, 2023
f392485
Fix quality gate issues.
staudtMarius Apr 26, 2023
ce12aa0
Fixing failing test.
staudtMarius Apr 26, 2023
0cb9fb6
Fixing failing quality gate.
staudtMarius Apr 26, 2023
1d3afb8
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius Apr 27, 2023
d80b542
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius May 4, 2023
a4561ad
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius May 9, 2023
60e2afa
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius May 15, 2023
2f40a80
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius May 16, 2023
e02890a
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius May 22, 2023
98b6280
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius May 25, 2023
05d8a6b
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius May 30, 2023
9bb4e5a
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius Jun 7, 2023
e7ae53e
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
sebastian-peter Jun 21, 2023
5a3f95d
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius Jul 19, 2023
bec0f02
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
sebastian-peter Jul 25, 2023
87660a0
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius Jul 26, 2023
2187cfc
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius Jul 28, 2023
098ab79
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
sebastian-peter Jul 31, 2023
bb64244
Implementing requested changes.
staudtMarius Aug 1, 2023
ed9231b
Merge branch 'dev' into ms/#682-method-handleEntity-throws-Exception
staudtMarius Aug 1, 2023
2aebb8c
Applied type parameters to method call
sebastian-peter Aug 1, 2023
f83abfb
Avoided usage of RuntimeException
sebastian-peter Aug 1, 2023
3fcdca3
Avoided usage of RuntimeException in time series processor map creation
sebastian-peter Aug 1, 2023
0871294
Fixed code smell
sebastian-peter Aug 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Is thrown, when an something went wrong during entity field mapping creation in a {@link
* edu.ie3.datamodel.io.processor.EntityProcessor}
*/
public class EntityProcessorException extends RuntimeException {
public class EntityProcessorException extends Exception {
public EntityProcessorException(final String message, final Throwable cause) {
super(message, cause);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/edu/ie3/datamodel/io/factory/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public abstract class Factory<C, D extends FactoryData, R> {

private final List<Class<? extends C>> supportedClasses;

@SafeVarargs
protected Factory(Class<? extends C>... supportedClasses) {
this.supportedClasses = Arrays.asList(supportedClasses);
}
Expand All @@ -54,9 +55,9 @@ public Try<R, FactoryException> get(D data) {
validateParameters(data, allFields.toArray((IntFunction<Set<String>[]>) Set[]::new));

// build the model
return new Success<>(buildModel(data));
return Success.of(buildModel(data));
} catch (FactoryException e) {
return new Failure<>(
return Failure.of(
new FactoryException(
"An error occurred when creating instance of "
+ data.getTargetClass().getSimpleName()
Expand Down
42 changes: 22 additions & 20 deletions src/main/java/edu/ie3/datamodel/io/processor/EntityProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import edu.ie3.datamodel.exceptions.EntityProcessorException;
import edu.ie3.datamodel.models.StandardUnits;
import edu.ie3.datamodel.models.UniqueEntity;
import edu.ie3.datamodel.utils.Try;
import edu.ie3.datamodel.utils.Try.*;
import edu.ie3.util.exceptions.QuantityException;
import java.lang.reflect.Method;
import java.util.*;
import javax.measure.Quantity;
Expand Down Expand Up @@ -37,7 +40,7 @@ public abstract class EntityProcessor<T extends UniqueEntity> extends Processor<
*
* @param registeredClass the class the entity processor should be able to handle
*/
protected EntityProcessor(Class<? extends T> registeredClass) {
protected EntityProcessor(Class<? extends T> registeredClass) throws EntityProcessorException {
super(registeredClass);
this.fieldNameToMethod =
mapFieldNameToGetter(registeredClass, Collections.singleton(NODE_INTERNAL));
Expand All @@ -51,7 +54,7 @@ protected EntityProcessor(Class<? extends T> registeredClass) {
* @return an optional Map with fieldName to fieldValue or an empty optional if an error occurred
* during processing
*/
public Optional<LinkedHashMap<String, String>> handleEntity(T entity) {
public LinkedHashMap<String, String> handleEntity(T entity) throws EntityProcessorException {
if (!registeredClass.equals(entity.getClass()))
throw new EntityProcessorException(
"Cannot process "
Expand All @@ -62,33 +65,32 @@ public Optional<LinkedHashMap<String, String>> handleEntity(T entity) {
+ entity.getClass().getSimpleName()
+ ".class!");

try {
return Optional.of(processObject(entity, fieldNameToMethod));
} catch (EntityProcessorException e) {
logger.error("Cannot process the entity{}.", entity, e);
return Optional.empty();
}
return processObject(entity, fieldNameToMethod);
}

@Override
protected Optional<String> handleProcessorSpecificQuantity(
protected Try<String, QuantityException> handleProcessorSpecificQuantity(
Quantity<?> quantity, String fieldName) {
return switch (fieldName) {
case "energy", "eConsAnnual", "eStorage":
yield quantityValToOptionalString(
quantity.asType(Energy.class).to(StandardUnits.ENERGY_IN));
yield Success.of(
quantityValToOptionalString(quantity.asType(Energy.class).to(StandardUnits.ENERGY_IN)));
case "q":
yield quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.REACTIVE_POWER_IN));
yield Success.of(
quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.REACTIVE_POWER_IN)));
case "p", "pMax", "pOwn", "pThermal":
yield quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.ACTIVE_POWER_IN));
yield Success.of(
quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.ACTIVE_POWER_IN)));
default:
log.error(
"Cannot process quantity with value '{}' for field with name {} in input entity processing!",
quantity,
fieldName);
yield Optional.empty();
yield Failure.of(
new QuantityException(
"Cannot process quantity with value '"
+ quantity
+ "' for field with name "
+ fieldName
+ " in input entity processing!"));
};
}

Expand Down
73 changes: 44 additions & 29 deletions src/main/java/edu/ie3/datamodel/io/processor/Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import edu.ie3.datamodel.models.input.system.characteristic.CharacteristicInput;
import edu.ie3.datamodel.models.profile.LoadProfile;
import edu.ie3.datamodel.models.voltagelevels.VoltageLevel;
import edu.ie3.datamodel.utils.Try;
import edu.ie3.datamodel.utils.Try.*;
import edu.ie3.util.exceptions.QuantityException;
import java.beans.Introspector;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -72,7 +75,7 @@ public abstract class Processor<T> {
*
* @param foreSeenClass Class and its children that are foreseen to be handled with this processor
*/
protected Processor(Class<? extends T> foreSeenClass) {
protected Processor(Class<? extends T> foreSeenClass) throws EntityProcessorException {
if (!getEligibleEntityClasses().contains(foreSeenClass))
throw new EntityProcessorException(
"Cannot register class '"
Expand Down Expand Up @@ -104,9 +107,10 @@ public int compare(String a, String b) {
* Maps the foreseen table fields to the objects getters
*
* @param cls class to use for mapping
* @return an array of strings of all field values of the class
* @return a map of all field values of the class
*/
protected SortedMap<String, Method> mapFieldNameToGetter(Class<?> cls) {
protected SortedMap<String, Method> mapFieldNameToGetter(Class<?> cls)
throws EntityProcessorException {
return mapFieldNameToGetter(cls, Collections.emptyList());
}

Expand All @@ -115,10 +119,10 @@ protected SortedMap<String, Method> mapFieldNameToGetter(Class<?> cls) {
*
* @param cls class to use for mapping
* @param ignoreFields A collection of all field names to ignore during process
* @return an array of strings of all field values of the class
* @return a map of all field values of the class
*/
protected SortedMap<String, Method> mapFieldNameToGetter(
Class<?> cls, Collection<String> ignoreFields) {
Class<?> cls, Collection<String> ignoreFields) throws EntityProcessorException {
try {
final LinkedHashMap<String, Method> resFieldNameToMethod = new LinkedHashMap<>();
Arrays.stream(Introspector.getBeanInfo(cls, Object.class).getPropertyDescriptors())
Expand Down Expand Up @@ -178,7 +182,7 @@ public static <V> SortedMap<String, V> putUuidFirst(Map<String, V> unsorted) {
* @return Mapping from field name to value as String representation
*/
protected LinkedHashMap<String, String> processObject(
Object object, Map<String, Method> fieldNameToGetter) {
Object object, Map<String, Method> fieldNameToGetter) throws EntityProcessorException {
try {
LinkedHashMap<String, String> resultMap = new LinkedHashMap<>();
for (Map.Entry<String, Method> entry : fieldNameToGetter.entrySet()) {
Expand Down Expand Up @@ -207,7 +211,8 @@ protected LinkedHashMap<String, String> processObject(
* @param fieldName Name of the foreseen field
* @return A String representation of the result
*/
protected String processMethodResult(Object methodReturnObject, Method method, String fieldName) {
protected String processMethodResult(Object methodReturnObject, Method method, String fieldName)
throws EntityProcessorException {

StringBuilder resultStringBuilder = new StringBuilder();

Expand All @@ -231,13 +236,17 @@ protected String processMethodResult(Object methodReturnObject, Method method, S
((Optional<?>) methodReturnObject)
.map(
o -> {
if (o instanceof Quantity<?>) {
return handleQuantity((Quantity<?>) o, fieldName);
} else {
throw new EntityProcessorException(
"Handling of "
+ o.getClass().getSimpleName()
+ ".class instance wrapped into Optional is currently not supported by entity processors!");
try {
if (o instanceof Quantity<?>) {
return handleQuantity((Quantity<?>) o, fieldName);
} else {
throw new EntityProcessorException(
"Handling of "
+ o.getClass().getSimpleName()
+ ".class instance wrapped into Optional is currently not supported by entity processors!");
}
} catch (EntityProcessorException e) {
throw new RuntimeException(e);
}
})
.orElse(""));
Expand Down Expand Up @@ -306,7 +315,8 @@ protected String processMethodResult(Object methodReturnObject, Method method, S
* @return the resulting string of a VoltageLevel attribute value for the provided field or an
* empty string when an invalid field name is provided
*/
protected String processVoltageLevel(VoltageLevel voltageLevel, String fieldName) {
protected String processVoltageLevel(VoltageLevel voltageLevel, String fieldName)
throws EntityProcessorException {

StringBuilder resultStringBuilder = new StringBuilder();
if (fieldName.equalsIgnoreCase(VOLT_LVL)) resultStringBuilder.append(voltageLevel.getId());
Expand All @@ -324,21 +334,26 @@ protected String processVoltageLevel(VoltageLevel voltageLevel, String fieldName
* @return an optional string with the normalized to {@link StandardUnits} value of the quantity
* or empty if an error occurred during processing
*/
protected String handleQuantity(Quantity<?> quantity, String fieldName) {
Optional<String> optQuant;
protected String handleQuantity(Quantity<?> quantity, String fieldName)
throws EntityProcessorException {
Try<String, QuantityException> optQuant;
if (specificQuantityFieldNames.contains(fieldName)) {
optQuant = handleProcessorSpecificQuantity(quantity, fieldName);
} else {
optQuant = quantityValToOptionalString(quantity);
optQuant = Success.of(quantityValToOptionalString(quantity));
}
return optQuant.orElseThrow(
() ->
new EntityProcessorException(
"Unable to process quantity value for attribute '"
+ fieldName
+ "' in entity "
+ getRegisteredClass().getSimpleName()
+ ".class."));

return optQuant
.transformF(
e ->
new EntityProcessorException(
"Unable to process quantity value for attribute '"
+ fieldName
+ "' in entity "
+ getRegisteredClass().getSimpleName()
+ ".class.",
e))
.getOrThrow();
}

/**
Expand All @@ -354,7 +369,7 @@ protected String handleQuantity(Quantity<?> quantity, String fieldName) {
* @return an optional string with the normalized to {@link StandardUnits} value of the quantity
* or empty if an error occurred during processing
*/
protected abstract Optional<String> handleProcessorSpecificQuantity(
protected abstract Try<String, QuantityException> handleProcessorSpecificQuantity(
Quantity<?> quantity, String fieldName);

protected String processUUIDArray(UUID[] uuids) {
Expand Down Expand Up @@ -406,8 +421,8 @@ protected String processZonedDateTime(ZonedDateTime zonedDateTime) {
* @param quantity Quantity to convert
* @return A string of the quantity's value
*/
protected Optional<String> quantityValToOptionalString(Quantity<?> quantity) {
return Optional.of(Double.toString(quantity.getValue().doubleValue()));
protected String quantityValToOptionalString(Quantity<?> quantity) {
return Double.toString(quantity.getValue().doubleValue());
}

/**
Expand Down
Loading