Skip to content

Instantly share code, notes, and snippets.

@vernetto
Created October 17, 2023 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vernetto/e199165ad8e9a0c4dbab487dab75d974 to your computer and use it in GitHub Desktop.
Save vernetto/e199165ad8e9a0c4dbab487dab75d974 to your computer and use it in GitHub Desktop.
changes
Here are some potential improvements to the provided code:
Descriptive Naming: Improve the method and variable names to be more descriptive, making the code more self-explanatory.
Reduce Nesting: The deep nesting in the lambda within the forEachExisting can be simplified for better readability.
Method Decomposition: Breaking down the lambda function can help improve readability and maintainability.
Let's apply these improvements:
java
public void processSegmentsOfCondition(ConditionEntity conditionEntity) {
forEachInCollection(conditionEntity.getSegmentSet(), this::handleRelation);
}
public void handleRelation(Relation relation) {
reconcileSegmentWithDatabase(relation);
setRelationIdFromConditionSegment(relation, conditionEntity);
}
private void reconcileSegmentWithDatabase(Relation relation) {
Segment segment = relation.getSegment();
Segment reconciledSegment = fetchFromDbOrElseSave(segment, segment::getName, segmentRepository::findByName, segmentRepository::save);
relation.setSegment(reconciledSegment);
}
private void setRelationIdFromConditionSegment(Relation relation, ConditionEntity conditionEntity) {
Optional.ofNullable(relation.getSegment().getConditionSegments())
.flatMap(relations -> relations.stream()
.filter(existingRelation -> Objects.equals(existingRelation.getCondition().getId(), conditionEntity.getId()))
.findFirst()
.map(ConditionSegmentEntity::getId))
.ifPresent(relation::setId);
}
public static <T> void forEachInCollection(Collection<T> collection, Consumer<? super T> action) {
Optional.ofNullable(collection).ifPresent(coll -> coll.forEach(action));
}
private <T, X> T fetchFromDbOrElseSave(T entity, Supplier<X> identifierSupplier, Function<X, Optional<T>> dbFetcher, Function<T, T> dbSaver) {
return Optional.ofNullable(entity)
.map(unused -> dbFetcher.apply(identifierSupplier.get())
.orElseGet(() -> dbSaver.apply(entity)))
.orElse(null);
}
Changes made:
forEachExisting was renamed to forEachInCollection for clarity.
Broke down the large lambda into two separate methods: handleRelation and reconcileSegmentWithDatabase.
Renamed ifDbHasItOrElseSave to fetchFromDbOrElseSave for a clearer intent.
Introduced a new method setRelationIdFromConditionSegment to handle the ID setting logic, making the main process more readable.
Used the more generic Collection in forEachInCollection.
Changed the method and variable names to make their purpose more evident.
With these changes, the code should be more maintainable and easier to understand.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment