Merge pull request #1 from hapifhir/ja_implement_fhirpath_equality_check
Implement FHIRPath equality test
This commit is contained in:
commit
d954f57c7e
|
@ -856,5 +856,70 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||
return getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method implements a datetime equality check using the rules as defined by FHIRPath.
|
||||
*
|
||||
* This method returns:
|
||||
* <ul>
|
||||
* <li>true if the given datetimes represent the exact same instant with the same precision (irrespective of the timezone)</li>
|
||||
* <li>true if the given datetimes represent the exact same instant but one includes milliseconds of <code>.[0]+</code> while the other includes only SECONDS precision (irrespecitve of the timezone)</li>
|
||||
* <li>true if the given datetimes represent the exact same year/year-month/year-month-date (if both operands have the same precision)</li>
|
||||
* <li>false if both datetimes have equal precision of MINUTE or greater, one has no timezone specified but the other does, and could not represent the same instant in any timezone</li>
|
||||
* <li>null if both datetimes have equal precision of MINUTE or greater, one has no timezone specified but the other does, and could potentially represent the same instant in any timezone</li>
|
||||
* <li>false if the given datetimes have the same precision but do not represent the same instant (irrespective of timezone)</li>
|
||||
* <li>null otherwise (since these datetimes are not comparable)</li>
|
||||
* </ul>
|
||||
*/
|
||||
public Boolean equalsUsingFhirPathRules(BaseDateTimeType theOther) {
|
||||
|
||||
if (hasTimezoneIfRequired() != theOther.hasTimezoneIfRequired()) {
|
||||
if (getPrecision() == theOther.getPrecision()) {
|
||||
if (getPrecision().ordinal() >= TemporalPrecisionEnum.MINUTE.ordinal() && theOther.getPrecision().ordinal() >= TemporalPrecisionEnum.MINUTE.ordinal()) {
|
||||
boolean couldBeTheSameTime = couldBeTheSameTime(this, theOther) || couldBeTheSameTime(theOther, this);
|
||||
if (!couldBeTheSameTime) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Same precision
|
||||
if (getPrecision() == theOther.getPrecision()) {
|
||||
return getValue().getTime() == theOther.getValue().getTime();
|
||||
}
|
||||
|
||||
// Both represent 0 millis but the millis are optional
|
||||
if (((Integer)0).equals(getMillis())) {
|
||||
if (((Integer)0).equals(theOther.getMillis())) {
|
||||
if (getPrecision().ordinal() >= TemporalPrecisionEnum.SECOND.ordinal()) {
|
||||
if (theOther.getPrecision().ordinal() >= TemporalPrecisionEnum.SECOND.ordinal()) {
|
||||
return getValue().getTime() == theOther.getValue().getTime();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean couldBeTheSameTime(BaseDateTimeType theArg1, BaseDateTimeType theArg2) {
|
||||
boolean theCouldBeTheSameTime = false;
|
||||
if (theArg1.getTimeZone() == null && theArg2.getTimeZone() != null) {
|
||||
long lowLeft = new DateTimeType(theArg1.getValueAsString()+"Z").getValue().getTime() - (14 * DateUtils.MILLIS_PER_HOUR);
|
||||
long highLeft = new DateTimeType(theArg1.getValueAsString()+"Z").getValue().getTime() + (14 * DateUtils.MILLIS_PER_HOUR);
|
||||
long right = theArg2.getValue().getTime();
|
||||
if (right >= lowLeft && right <= highLeft) {
|
||||
theCouldBeTheSameTime = true;
|
||||
}
|
||||
}
|
||||
return theCouldBeTheSameTime;
|
||||
}
|
||||
|
||||
boolean hasTimezoneIfRequired() {
|
||||
return getPrecision().ordinal() <= TemporalPrecisionEnum.DAY.ordinal() ||
|
||||
getTimeZone() != null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -463,9 +463,7 @@ public class FHIRPathEngine {
|
|||
DateTimeType dateRight = new DateTimeType(dateRightString);
|
||||
|
||||
if (theEquivalenceTest) {
|
||||
TemporalPrecisionEnum lowestPrecision = dateLeft.getPrecision().ordinal() < dateRight.getPrecision().ordinal() ? dateLeft.getPrecision() : dateRight.getPrecision();
|
||||
dateLeft.setPrecision(lowestPrecision);
|
||||
dateRight.setPrecision(lowestPrecision);
|
||||
return dateLeft.equalsUsingFhirPathRules(dateRight) == Boolean.TRUE ? 0 : 1;
|
||||
}
|
||||
|
||||
if (dateLeft.getPrecision().ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
package org.hl7.fhir.r4.model;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class BaseDateTimeTypeTest {
|
||||
|
||||
/**
|
||||
* <ul>
|
||||
* <li>true if the given datetimes represent the exact same instant with the same precision (irrespective of the timezone)</li>
|
||||
* <li>true if the given datetimes represent the exact same instant but one includes milliseconds of <code>.[0]+</code> while the other includes only SECONDS precision (irrespecitve of the timezone)</li>
|
||||
* <li>true if the given datetimes represent the exact same year/year-month/year-month-date (if both operands have the same precision)</li>
|
||||
* <li>false if the given datetimes have the same precision but do not represent the same instant (irrespective of timezone)</li>
|
||||
* <li>null otherwise (since these datetimes are not comparable)</li>
|
||||
* </ul>
|
||||
*/
|
||||
@Test
|
||||
public void equalsUsingFhirPathRules() {
|
||||
// Exact same - Same timezone
|
||||
assertTrue(compareDateTimes("2001-01-02T11:22:33.444Z", "2001-01-02T11:22:33.444Z"));
|
||||
// Exact same - Different timezone
|
||||
assertTrue(compareDateTimes("2001-01-02T11:22:33.444-03:00", "2001-01-02T10:22:33.444-04:00"));
|
||||
// Exact same - Dates
|
||||
assertTrue(compareDateTimes("2001", "2001"));
|
||||
assertTrue(compareDateTimes("2001-01", "2001-01"));
|
||||
assertTrue(compareDateTimes("2001-01-02", "2001-01-02"));
|
||||
// Same precision but different values - Dates
|
||||
assertFalse(compareDateTimes("2001", "2002"));
|
||||
assertFalse(compareDateTimes("2001-01", "2001-02"));
|
||||
assertFalse(compareDateTimes("2001-01-02", "2001-01-03"));
|
||||
// Different instant - Same timezone
|
||||
assertFalse(compareDateTimes("2001-01-02T11:22:33.444Z", "2001-01-02T11:22:33.445Z"));
|
||||
|
||||
// FHIRPath tests:
|
||||
assertFalse(compareDateTimes("1974-12-25", "1974-12-25T12:34:00+10:00"));
|
||||
assertFalse(compareDateTimes("1974-12-25", "1974-12-25T12:34:00-10:00"));
|
||||
assertFalse(compareDateTimes("1974-12-25", "1974-12-25T12:34:00Z"));
|
||||
assertFalse(compareDateTimes("2012-04-15", "2012-04-16"));
|
||||
assertFalse(compareDateTimes("2012-04-15T15:00:00", "2012-04-15T10:00:00"));
|
||||
assertFalse(compareDateTimes("2017-11-05T01:30:00.0-04:00", "2017-11-05T01:15:00.0-05:00"));
|
||||
assertNull(compareDateTimes("1974-12-25", "1974-12-25T12:34:00"));
|
||||
assertNull(compareDateTimes("2012-04-15", "2012-04-15T10:00:00"));
|
||||
assertNull(compareDateTimes("2012-04-15T15:00:00Z", "2012-04-15T10:00:00"));
|
||||
assertTrue(compareDateTimes("1974-12-25", "1974-12-25"));
|
||||
assertTrue(compareDateTimes("2012-04-15", "2012-04-15"));
|
||||
assertTrue(compareDateTimes("2012-04-15T15:00:00+02:00", "2012-04-15T16:00:00+03:00"));
|
||||
assertTrue(compareDateTimes("2012-04-15T15:00:00+02:00", "2012-04-15T16:00:00+03:00"));
|
||||
assertTrue(compareDateTimes("2017-11-05T01:30:00.0-04:00", "2017-11-05T00:30:00.0-05:00"));
|
||||
|
||||
assertFalse(compareDateTimes("2016-12-02T13:00:00Z", "2016-11-02T10:00:00")); // no timezone, but cannot be the same time
|
||||
assertNull(compareDateTimes("2016-12-02T13:00:00Z", "2016-12-02T10:00:00")); // no timezone, might be the same time
|
||||
}
|
||||
|
||||
private Boolean compareDateTimes(String theLeft, String theRight) {
|
||||
DateTimeType leftDt = new DateTimeType(theLeft);
|
||||
DateTimeType rightDt = new DateTimeType(theRight);
|
||||
return leftDt.equalsUsingFhirPathRules(rightDt);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue