Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Gave predicates meaningful names #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,37 @@ public class OfflineDevicesJobImpl implements OfflineDevicesJob {
}

static Optional<Duration> calculateLastPassedThreshold(Instant start, Instant current, List<Duration> thresholds) {
if (current.isBefore(start) || start.equals(current) || thresholds == null || thresholds.isEmpty()) {
if (isStartAfterCurrent(start, current) || noTresholds(thresholds)) {
throw new IllegalArgumentException("Start must be before current and there should be at least 1 threshold");
}

Duration timePassed = Duration.between(start, current);

if (timePassed.compareTo(thresholds.get(0)) <= 0) {
if (tresholdAfterTimePassed((Duration) timePassed, thresholds.get(0))) {
return Optional.empty();
}

for (int i = 1; i < thresholds.size(); i++) {
if (timePassed.compareTo(thresholds.get(i)) <= 0) {
if (tresholdAfterTimePassed(timePassed, thresholds.get(i))) {
return Optional.of(thresholds.get(i - 1));
}
}

return Optional.of(thresholds.get(thresholds.size() - 1));
}

private static boolean noTresholds(List<Duration> thresholds) {
return thresholds == null || thresholds.isEmpty();
}

private static boolean isStartAfterCurrent(Instant start, Instant current) {
return current.isBefore(start) || start.equals(current);
}

private static boolean tresholdAfterTimePassed(Duration timePassed, Duration threshold) {
return timePassed.compareTo(threshold) <= 0;
}

static boolean shouldSendNotification(Instant jobStart, Instant deviceOffline, List<Duration> thresholds) {
return calculateLastPassedThreshold(deviceOffline, jobStart, thresholds).isPresent();
}
Expand Down