Skip to content

Commit

Permalink
Use enhanched switch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
ckittl committed Jan 6, 2022
1 parent 7431b26 commit 359267e
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 147 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Protected constructors for abstract classes
- Use pattern matching
- Remove unused imports
- Use enhanced switch statements
- Create JavaDoc with java 17 instead of java 8

### Added
Expand Down
37 changes: 12 additions & 25 deletions src/main/java/edu/ie3/datamodel/io/processor/EntityProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,18 @@ protected Optional<String> handleProcessorSpecificQuantity(
Quantity<?> quantity, String fieldName) {
Optional<String> normalizedQuantityValue = Optional.empty();
switch (fieldName) {
case "energy":
case "eConsAnnual":
case "eStorage":
normalizedQuantityValue =
quantityValToOptionalString(quantity.asType(Energy.class).to(StandardUnits.ENERGY_IN));
break;
case "q":
normalizedQuantityValue =
quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.REACTIVE_POWER_IN));
break;
case "p":
case "pMax":
case "pOwn":
case "pThermal":
normalizedQuantityValue =
quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.ACTIVE_POWER_IN));
break;
default:
log.error(
"Cannot process quantity with value '{}' for field with name {} in input entity processing!",
quantity,
fieldName);
break;
case "energy", "eConsAnnual", "eStorage" -> normalizedQuantityValue =
quantityValToOptionalString(quantity.asType(Energy.class).to(StandardUnits.ENERGY_IN));
case "q" -> normalizedQuantityValue =
quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.REACTIVE_POWER_IN));
case "p", "pMax", "pOwn", "pThermal" -> normalizedQuantityValue =
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);
}
return normalizedQuantityValue;
}
Expand Down
174 changes: 78 additions & 96 deletions src/main/java/edu/ie3/datamodel/io/processor/Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,102 +205,84 @@ protected String processMethodResult(Object methodReturnObject, Method method, S

switch (method.getReturnType().getSimpleName()) {
// primitives (Boolean, Character, Byte, Short, Integer, Long, Float, Double, String,
case "UUID":
case "boolean":
case "int":
case "double":
case "String":
case "DayOfWeek":
case "ChargingPointType":
case "EvcsLocationType":
resultStringBuilder.append(methodReturnObject.toString());
break;
case "Quantity":
case "ComparableQuantity":
resultStringBuilder.append(handleQuantity((Quantity<?>) methodReturnObject, fieldName));
break;
case "Optional":
// only quantity optionals are expected here!
// if optional and present, unpack value and call this method again, if not present return
// an empty string as by convention null == missing value == "" when persisting data
resultStringBuilder.append(
((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!");
}
})
.orElse(""));
break;
case "ZonedDateTime":
resultStringBuilder.append(processZonedDateTime((ZonedDateTime) methodReturnObject));
break;
case "OperationTime":
resultStringBuilder.append(
processOperationTime((OperationTime) methodReturnObject, fieldName));
break;
case "VoltageLevel":
resultStringBuilder.append(
processVoltageLevel((VoltageLevel) methodReturnObject, fieldName));
break;
case "Point":
case "LineString":
resultStringBuilder.append(geoJsonWriter.write((Geometry) methodReturnObject));
break;
case "StandardLoadProfile":
resultStringBuilder.append(((StandardLoadProfile) methodReturnObject).getKey());
break;
case "AssetTypeInput":
case "BmTypeInput":
case "ChpTypeInput":
case "EvTypeInput":
case "HpTypeInput":
case "LineTypeInput":
case "LineInput":
case "NodeInput":
case "StorageTypeInput":
case "SystemParticipantInput":
case "ThermalBusInput":
case "ThermalStorageInput":
case "TimeSeries":
case "Transformer2WTypeInput":
case "Transformer3WTypeInput":
case "WecTypeInput":
resultStringBuilder.append(((UniqueEntity) methodReturnObject).getUuid());
break;
case "OperatorInput":
resultStringBuilder.append(
((OperatorInput) methodReturnObject).getId().equalsIgnoreCase("NO_OPERATOR_ASSIGNED")
? ""
: ((OperatorInput) methodReturnObject).getUuid());
break;
case "EvCharacteristicInput":
case "OlmCharacteristicInput":
case "WecCharacteristicInput":
case "CosPhiFixed":
case "CosPhiP":
case "QV":
case "ReactivePowerCharacteristic":
case "CharacteristicInput":
resultStringBuilder.append(((CharacteristicInput<?, ?>) methodReturnObject).deSerialize());
break;
default:
throw new EntityProcessorException(
"Unable to process value for attribute/field '"
+ fieldName
+ "' and method return type '"
+ method.getReturnType().getSimpleName()
+ "' for method with name '"
+ method.getName()
+ "' in in entity model "
+ getRegisteredClass().getSimpleName()
+ ".class.");
case "UUID",
"boolean",
"int",
"double",
"String",
"DayOfWeek",
"ChargingPointType",
"EvcsLocationType" -> resultStringBuilder.append(methodReturnObject.toString());
case "Quantity", "ComparableQuantity" -> resultStringBuilder.append(
handleQuantity((Quantity<?>) methodReturnObject, fieldName));
case "Optional" ->
// only quantity optionals are expected here!
// if optional and present, unpack value and call this method again, if not present return
// an empty string as by convention null == missing value == "" when persisting data
resultStringBuilder.append(
((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!");
}
})
.orElse(""));
case "ZonedDateTime" -> resultStringBuilder.append(
processZonedDateTime((ZonedDateTime) methodReturnObject));
case "OperationTime" -> resultStringBuilder.append(
processOperationTime((OperationTime) methodReturnObject, fieldName));
case "VoltageLevel" -> resultStringBuilder.append(
processVoltageLevel((VoltageLevel) methodReturnObject, fieldName));
case "Point", "LineString" -> resultStringBuilder.append(
geoJsonWriter.write((Geometry) methodReturnObject));
case "StandardLoadProfile" -> resultStringBuilder.append(
((StandardLoadProfile) methodReturnObject).getKey());
case "AssetTypeInput",
"BmTypeInput",
"ChpTypeInput",
"EvTypeInput",
"HpTypeInput",
"LineTypeInput",
"LineInput",
"NodeInput",
"StorageTypeInput",
"SystemParticipantInput",
"ThermalBusInput",
"ThermalStorageInput",
"TimeSeries",
"Transformer2WTypeInput",
"Transformer3WTypeInput",
"WecTypeInput" -> resultStringBuilder.append(
((UniqueEntity) methodReturnObject).getUuid());
case "OperatorInput" -> resultStringBuilder.append(
((OperatorInput) methodReturnObject).getId().equalsIgnoreCase("NO_OPERATOR_ASSIGNED")
? ""
: ((OperatorInput) methodReturnObject).getUuid());
case "EvCharacteristicInput",
"OlmCharacteristicInput",
"WecCharacteristicInput",
"CosPhiFixed",
"CosPhiP",
"QV",
"ReactivePowerCharacteristic",
"CharacteristicInput" -> resultStringBuilder.append(
((CharacteristicInput<?, ?>) methodReturnObject).deSerialize());
default -> throw new EntityProcessorException(
"Unable to process value for attribute/field '"
+ fieldName
+ "' and method return type '"
+ method.getReturnType().getSimpleName()
+ "' for method with name '"
+ method.getName()
+ "' in in entity model "
+ getRegisteredClass().getSimpleName()
+ ".class.");
}

return resultStringBuilder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,19 @@ protected Optional<String> handleProcessorSpecificQuantity(
Quantity<?> quantity, String fieldName) {
Optional<String> normalizedQuantityValue = Optional.empty();
switch (fieldName) {
case "energy":
case "eConsAnnual":
case "eStorage":
normalizedQuantityValue =
quantityValToOptionalString(
quantity.asType(Energy.class).to(StandardUnits.ENERGY_RESULT));
break;
case "q":
normalizedQuantityValue =
quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.REACTIVE_POWER_RESULT));
break;
case "p":
case "pMax":
case "pOwn":
case "pThermal":
normalizedQuantityValue =
quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.ACTIVE_POWER_RESULT));
break;
default:
log.error(
"Cannot process quantity with value '{}' for field with name {} in result entity processing!",
quantity,
fieldName);
break;
case "energy", "eConsAnnual", "eStorage" -> normalizedQuantityValue =
quantityValToOptionalString(
quantity.asType(Energy.class).to(StandardUnits.ENERGY_RESULT));
case "q" -> normalizedQuantityValue =
quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.REACTIVE_POWER_RESULT));
case "p", "pMax", "pOwn", "pThermal" -> normalizedQuantityValue =
quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.ACTIVE_POWER_RESULT));
default -> log.error(
"Cannot process quantity with value '{}' for field with name {} in result entity processing!",
quantity,
fieldName);
}
return normalizedQuantityValue;
}
Expand Down

0 comments on commit 359267e

Please sign in to comment.