Skip to content

Commit

Permalink
Enhance time unit handling in compareTo() method
Browse files Browse the repository at this point in the history
  • Loading branch information
jcnamendiOdysseus committed Jan 25, 2024
1 parent 690b273 commit 2f97201
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions src/main/java/org/ohdsi/circe/check/checkers/Comparisons.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@

public class Comparisons {

private static final Map<String, Integer> TIME_UNIT_CONVERSION = new HashMap<>();
static {
TIME_UNIT_CONVERSION.put(IntervalUnit.HOUR.getName(), 60 * 60);
TIME_UNIT_CONVERSION.put(IntervalUnit.MINUTE.getName(), 60);
}

public static Boolean startIsGreaterThanEnd(NumericRange r) {

return Objects.nonNull(r.value) && Objects.nonNull(r.extent) && r.value.intValue() > r.extent.intValue();
Expand Down Expand Up @@ -108,26 +102,48 @@ public static Predicate<Concept> compare(Concept source) {
.build();
}

/**
* If timeUnit is equal to values such as Hours, Minutes, Seconds, the values will be converted to seconds
* Otherwise it will return to the previous logic.
* @param filter
* @param window
* @return
*/
public static int compareTo(ObservationFilter filter, Window window) {
int range1, range2Start, range2End;
if (Objects.nonNull(window.start) && (Objects.nonNull(window.start.days) || IntervalUnit.DAY.getName().equals(window.start.timeUnit))) {
range1 = filter.postDays + filter.priorDays;
range2Start = window.start.coeff * window.start.days;
range2End = Objects.nonNull(window.end) && Objects.nonNull(window.end.days) ? window.end.coeff * window.end.days : 0;
} else {
int range1, range2Start = 0, range2End = 0;
if (Objects.nonNull(window.start) && Objects.nonNull(window.start.timeUnit) && !IntervalUnit.DAY.getName().equals(window.start.timeUnit)) {
range1 = (filter.postDays + filter.priorDays) * 24 * 60 * 60;
range2Start = getTimeInSeconds(window.start);
range2End = getTimeInSeconds(window.end);
} else {
range1 = filter.postDays + filter.priorDays;
if (Objects.nonNull(window.start) && Objects.nonNull(window.start.days)) {
range2Start = window.start.coeff * window.start.days;
}
if (Objects.nonNull(window.end) && Objects.nonNull(window.end.days)) {
range2End = window.end.coeff * window.end.days;
}
}
return range1 - (range2End - range2Start);
}
private static int getTimeInSeconds(Window.Endpoint endpoint) {
return Optional.ofNullable(endpoint)
.map(ep -> {
int convertRate = TIME_UNIT_CONVERSION.getOrDefault(ep.timeUnit, 1);
return Objects.nonNull(ep.timeUnitValue) ? ep.coeff * ep.timeUnitValue * convertRate : 0;
}).orElse(0);
}

/**
* @return Convert values to seconds.
*/
private static int getTimeInSeconds(Window.Endpoint endpoint) {
if (Objects.isNull(endpoint)) {
return 0;
}
int convertRate;
if (IntervalUnit.HOUR.getName().equals(endpoint.timeUnit)) {
convertRate = 60 * 60;
} else if (IntervalUnit.MINUTE.getName().equals(endpoint.timeUnit)) {
convertRate = 60;
} else convertRate = 1;
return Objects.nonNull(endpoint.timeUnitValue)
? endpoint.coeff * endpoint.timeUnitValue * convertRate
: 0;
}

public static boolean compare(Criteria c1, Criteria c2) {

Expand Down

0 comments on commit 2f97201

Please sign in to comment.