Let's say that we have a TrainingDate
class like below:
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
class TrainingDate {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private LocalDateTime startDate;
private LocalDateTime endDate;
@OneToOne(mappedBy = "trainingDate")
private Training training;
}
I want to create a method that will check if the proposed date of training will not collide with the time table of the trainer. The method looks like:
private boolean isTrainingDateNotCollidingWithTrainerSchedule(List<Training> trainerTrainings,
TrainingDate potentialTrainingDate) {
List<TrainingDate> trainingList = trainerTrainings
.stream()
.map(Training::getTrainingDate)
.filter(
trainingDate -> trainingDate.startDate.isAfter(potentialTrainingDate.endDate) &&
trainingDate.endDate.isBefore(potentialTrainingDate.startDate))
.collect(Collectors.toList());
if (trainingList.size() > 1) {
throw exception here
} else {
return true;
}
}
Seemingly it's working fine but for me, it's not so gentle way to reach a goal. Here comes the question, is it possible to refactor the above solution and make it a little more clear. I know that I can split this filter
declaration but this post click suggests that this is not a good choice if we will take a final performance into consideration. I will be grateful for the suggestion on how to check if start date
and end date
will not collide with the trainer timetable by improving clearness of the current solution.