From 4a6d0eed45a8c57b71c802e66b8ae32193232ce7 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 28 Jan 2019 17:08:39 -0500 Subject: [PATCH] First crack at this --- .../hl7/fhir/r4/model/BaseDateTimeType.java | 40 +++++++++++++++++- .../org/hl7/fhir/r4/utils/FHIRPathEngine.java | 4 +- .../fhir/r4/model/BaseDateTimeTypeTest.java | 42 +++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/model/BaseDateTimeTypeTest.java diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/BaseDateTimeType.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/BaseDateTimeType.java index d3e6b5807..f7d72bd71 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/BaseDateTimeType.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/BaseDateTimeType.java @@ -855,6 +855,44 @@ public abstract class BaseDateTimeType extends PrimitiveType { public Date dateTimeValue() { return getValue(); } - + + /** + * This method implements an equality check using the rules as defined by FHIRPath. + * + * This method returns: + * + */ + public Boolean equalsUsingFhirPathRules(BaseDateTimeType theOther) { + + // 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(); + } + } + } + } + + if (getPrecision() != theOther.getPrecision()) { + return null; + } + + return false; + } + + } diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/FHIRPathEngine.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/FHIRPathEngine.java index 069b6e55c..83d235f95 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/FHIRPathEngine.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/FHIRPathEngine.java @@ -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()) { diff --git a/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/model/BaseDateTimeTypeTest.java b/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/model/BaseDateTimeTypeTest.java new file mode 100644 index 000000000..cb1fe5cd4 --- /dev/null +++ b/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/model/BaseDateTimeTypeTest.java @@ -0,0 +1,42 @@ +package org.hl7.fhir.r4.model; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class BaseDateTimeTypeTest { + + /** + * + */ + @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")); + // Different precision + assertNull(compareDateTimes("2001-01-02T11:22:33.444Z", "2001-01-02T11:22:33Z")); + } + + private Boolean compareDateTimes(String theLeft, String theRight) { + return new DateTimeType(theLeft).equalsUsingFhirPathRules(new DateTimeType(theRight)); + } + +} \ No newline at end of file