Merge pull request #1 from hapifhir/ja_implement_fhirpath_equality_check
Implement FHIRPath equality test
This commit is contained in:
commit
d954f57c7e
|
@ -9,9 +9,9 @@ package org.hl7.fhir.r4.model;
|
|||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
@ -58,7 +58,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the specified precision is not allowed for this type
|
||||
*/
|
||||
|
@ -79,7 +79,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the specified precision is not allowed for this type
|
||||
*/
|
||||
|
@ -128,7 +128,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||
|
||||
/**
|
||||
* Returns <code>true</code> if the given object represents a date/time before <code>this</code> object
|
||||
*
|
||||
*
|
||||
* @throws NullPointerException
|
||||
* If <code>this.getValue()</code> or <code>theDateTimeType.getValue()</code>
|
||||
* return <code>null</code>
|
||||
|
@ -140,7 +140,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||
|
||||
/**
|
||||
* Returns <code>true</code> if the given object represents a date/time before <code>this</code> object
|
||||
*
|
||||
*
|
||||
* @throws NullPointerException
|
||||
* If <code>this.getValue()</code> or <code>theDateTimeType.getValue()</code>
|
||||
* return <code>null</code>
|
||||
|
@ -304,7 +304,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||
|
||||
/**
|
||||
* Gets the precision for this datatype (using the default for the given type if not set)
|
||||
*
|
||||
*
|
||||
* @see #setPrecision(TemporalPrecisionEnum)
|
||||
*/
|
||||
public TemporalPrecisionEnum getPrecision() {
|
||||
|
@ -370,7 +370,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||
|
||||
/**
|
||||
* Returns <code>true</code> if this object represents a date that is today's date
|
||||
*
|
||||
*
|
||||
* @throws NullPointerException
|
||||
* if {@link #getValue()} returns <code>null</code>
|
||||
*/
|
||||
|
@ -599,7 +599,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||
|
||||
/**
|
||||
* Sets the precision for this datatype
|
||||
*
|
||||
*
|
||||
* @throws DataFormatException
|
||||
*/
|
||||
public void setPrecision(TemporalPrecisionEnum thePrecision) throws DataFormatException {
|
||||
|
@ -668,7 +668,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||
* Sets the value for this type using the given Java Date object as the time, and using the specified precision, as
|
||||
* well as the local timezone as determined by the local operating system. Both of
|
||||
* these properties may be modified in subsequent calls if neccesary.
|
||||
*
|
||||
*
|
||||
* @param theValue
|
||||
* The date value
|
||||
* @param thePrecision
|
||||
|
@ -800,7 +800,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||
/**
|
||||
* Returns a human readable version of this date/time using the system local format, converted to the local timezone
|
||||
* if neccesary.
|
||||
*
|
||||
*
|
||||
* @see #toHumanDisplay() for a method which does not convert the time to the local timezone before rendering it.
|
||||
*/
|
||||
public String toHumanDisplayLocalTimezone() {
|
||||
|
@ -855,6 +855,71 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||
public Date dateTimeValue() {
|
||||
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