Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed `EnergyPriceValue.equals()` [#1479](https://github.com/ie3-institute/PowerSystemDataModel/issues/1479)
- Fixed tests of `TimeBasedValue` [#1469](https://github.com/ie3-institute/PowerSystemDataModel/issues/1469)
- Requiring `thermalBus` field in house and storage input [#1509](https://github.com/ie3-institute/PowerSystemDataModel/issues/1509)
- Fixed handling of erroneous field values in `EntitySource.enrichWithDefault()` [#1511](https://github.com/ie3-institute/PowerSystemDataModel/issues/1511)

### Changed
- Updated CI-Pipeline to run task `Deploy` and `Staging` only for `Main` [#1403](https://github.com/ie3-institute/PowerSystemDataModel/issues/1403)
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/edu/ie3/datamodel/io/factory/FactoryData.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ public boolean containsKey(String key) {
return fieldsToAttributes.containsKey(key);
}

/**
* Checks if the field is empty.
*
* @param field to check
* @return {@code true} if either the key is not present or the field is empty
*/
public boolean isFieldEmpty(String field) {
String value = fieldsToAttributes.getOrDefault(field, null);
return value == null || value.isEmpty();
}

/**
* Returns field value for given field name. Throws {@link FactoryException} if field does not
* exist.
Expand Down
28 changes: 25 additions & 3 deletions src/main/java/edu/ie3/datamodel/io/source/EntitySource.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,7 @@
BiFunction<E, T, R> buildingFcn) {
return entityData ->
entityData
.zip(
extractFunction(entityData, fieldName, entities)
.orElse(() -> Try.Success.of(defaultEntity)))
.zip(extractWithDefaultFunction(entityData, fieldName, entities, defaultEntity))
.map(enrichFunction(List.of(fieldName), buildingFcn));
}

Expand Down Expand Up @@ -383,6 +381,30 @@
+ exception.getMessage())));
}

/**
* Method to extract an entity with default.
*
* @param entityData data containing complex entities
* @param fieldName name of the field
* @param entities map: uuid to {@link Entity}
* @param defaultEntity that is used if the field is empty
* @return an enrichment
* @param <E> type of entity data
* @param <R> type of entity
*/
protected static <E extends EntityData, R> Try<R, SourceException> extractWithDefaultFunction(
Try<E, SourceException> entityData,
String fieldName,
Map<UUID, R> entities,
R defaultEntity) {
if (entityData.convert(data -> data.isFieldEmpty(fieldName), f -> false)) {

Check notice on line 400 in src/main/java/edu/ie3/datamodel/io/source/EntitySource.java

View check run for this annotation

SonarQubeGithubPRChecks / SonarQube Code Analysis

src/main/java/edu/ie3/datamodel/io/source/EntitySource.java#L400

Use a primitive boolean expression here.
// return the default entity, if the field is empty
return new Try.Success<>(defaultEntity);
} else {
return extractFunction(entityData, fieldName, entities);
}
}

/**
* Method to extract an {@link Entity} from a given map.
*
Expand Down