This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit e95ccead5e9b34c2861e73e9353309d025892799 Author: Martin Desruisseaux <[email protected]> AuthorDate: Wed Nov 18 23:56:31 2026 +0100 Review of the changes for the handling of `During` and `AnyIntersects` with ISO 19108 objects. --- .../main/org/apache/sis/filter/TemporalFilter.java | 29 +++--- .../org/apache/sis/filter/TemporalOperation.java | 35 +++---- .../org/apache/sis/filter/TemporalFilterTest.java | 104 ++++++++++++++++----- .../org/apache/sis/temporal/DefaultInstant.java | 12 ++- .../org/apache/sis/temporal/GeneralDuration.java | 1 - .../org/apache/sis/temporal/TemporalObjects.java | 5 +- .../main/org/apache/sis/temporal/TimeMethods.java | 64 ++++++++++--- 7 files changed, 174 insertions(+), 76 deletions(-) diff --git a/endorsed/src/org.apache.sis.feature/main/org/apache/sis/filter/TemporalFilter.java b/endorsed/src/org.apache.sis.feature/main/org/apache/sis/filter/TemporalFilter.java index 271bdbe4c7..8d3b57c086 100644 --- a/endorsed/src/org.apache.sis.feature/main/org/apache/sis/filter/TemporalFilter.java +++ b/endorsed/src/org.apache.sis.feature/main/org/apache/sis/filter/TemporalFilter.java @@ -24,6 +24,7 @@ import org.apache.sis.temporal.TimeMethods; import org.apache.sis.feature.internal.shared.FeatureExpression; // Specific to the geoapi-3.1 and geoapi-4.0 branches: +import org.opengis.temporal.Instant; import org.opengis.temporal.Period; import org.opengis.filter.Filter; import org.opengis.filter.Expression; @@ -84,7 +85,6 @@ class TemporalFilter<R,T> extends BinaryFunction<R,T,T> * @param expression1 the first of the two expressions to be used by this function. * @param expression2 the second of the two expressions to be used by this function. */ - @SuppressWarnings({"rawtypes", "unchecked"}) public static <R,V> TemporalFilter<R,?> create( final Class<V> type, final TemporalOperation.Factory factory, @@ -93,19 +93,21 @@ class TemporalFilter<R,T> extends BinaryFunction<R,T,T> { final Class<? extends V> c1 = getValueClass(expression1, type); final Class<? extends V> c2 = getValueClass(expression2, type); - Class<? extends V> commonType = type; + Class<?> commonType = type; if (type.isInterface()) { for (final Class<?> c : Classes.findCommonInterfaces(c1, c2)) { if (commonType.isAssignableFrom(c)) { - commonType = (Class<? extends V>) c; // Safe because verified by `isAssignableFrom(c)`. + commonType = c; } } } else { final Class<?> c = Classes.findCommonClass(c1, c2); if (commonType.isAssignableFrom(c)) { - commonType = (Class<? extends V>) c; // Safe because verified by `isAssignableFrom(c)`. + commonType = c; } } + assert commonType.isAssignableFrom(c1) : c1; + assert commonType.isAssignableFrom(c2) : c2; /* * We cannot use a more specific type here because the `find(…)` method argument is parameterized * with `<? extends T>` while its return value is parameterized with `<? super T>`. Java language @@ -117,18 +119,15 @@ class TemporalFilter<R,T> extends BinaryFunction<R,T,T> * `commonType` of type `Class<T>` no matter if <T> is a super-type or a sub-type of <V>, we can * assert that the parameterized type of the two expressions is `<? extends T>`. */ - /* - * Periods are compared by their beginning and ending instants, never as whole objects. - * Therefore, the comparison methods are requested for the `Period` interface rather than - * for the implementation class, which may be a final class without natural ordering. - */ - final Class<?> methodsType = Period.class.isAssignableFrom(commonType) ? Period.class : commonType; - final TemporalOperation<?> operation = factory.create(TimeMethods.forType(methodsType)).unique(); + final boolean arePeriods = Period.class.isAssignableFrom(commonType); + if (arePeriods) { + // Periods are compared by their beginning and ending instants, never as whole objects. + commonType = Instant.class; + } + final TemporalOperation<?> operation = factory.create(TimeMethods.forType(commonType)).unique(); assert operation.comparators.type.isAssignableFrom(commonType) : commonType; - assert commonType.isAssignableFrom(c1) : c1; - assert commonType.isAssignableFrom(c2) : c2; - if (Period.class.isAssignableFrom(commonType)) { - // Safe because `commonType` extends both Period and T. + if (arePeriods) { + // Safe because `commonType` (before replacement) extends both `Period` and <T>. return new Periods(operation, expression1, expression2); } if (operation.comparators.isDynamic) { diff --git a/endorsed/src/org.apache.sis.feature/main/org/apache/sis/filter/TemporalOperation.java b/endorsed/src/org.apache.sis.feature/main/org/apache/sis/filter/TemporalOperation.java index b2d440e06f..a58248ee3e 100644 --- a/endorsed/src/org.apache.sis.feature/main/org/apache/sis/filter/TemporalOperation.java +++ b/endorsed/src/org.apache.sis.feature/main/org/apache/sis/filter/TemporalOperation.java @@ -58,7 +58,7 @@ abstract class TemporalOperation<T> implements Serializable { private static final long serialVersionUID = -5304814915639460679L; /** - * Temporal operations created in the current JVM. + * Temporal operations created in the current <abbr>JVM</abbr>. */ @SuppressWarnings("unchecked") private static final WeakHashSet<TemporalOperation<?>> POOL = new WeakHashSet<>((Class) TemporalOperation.class); @@ -650,7 +650,6 @@ abstract class TemporalOperation<T> implements Serializable { /** * The {@code "During"} operation. Defined by ISO 19108 as: * <ul> - * <li>{@literal self > other.begin AND self < other.end}</li> * <li>{@literal self.begin > other.begin AND self.end < other.end}</li> * </ul> * @@ -675,7 +674,7 @@ abstract class TemporalOperation<T> implements Serializable { return '⊊'; // `self` is a proper (or strict) subset of `other`. } - /** Condition defined by ISO 19108:2006 (corrigendum) §5.2.3.5. */ + /** Extension to ISO 19108: handle instant as a tiny period. */ @Override public boolean evaluate(final T self, final Period other) { return compare(TimeMethods.Test.AFTER, self, other.getBeginning()) && compare(TimeMethods.Test.BEFORE, self, other.getEnding()); @@ -755,11 +754,11 @@ abstract class TemporalOperation<T> implements Serializable { /** Condition defined by ISO 19108:2006 (corrigendum) §5.2.3.5. */ @Override public boolean evaluate(final Period self, final Period other) { - final Instant selfBegin, selfEnd, otherBegin, otherEnd; - return ((otherBegin = other.getBeginning()) != null) && - ((selfBegin = self.getBeginning()) != null) && compare(TimeMethods.Test.BEFORE, selfBegin, otherBegin) && - ((selfEnd = self. getEnding()) != null) && compare(TimeMethods.Test.AFTER, selfEnd, otherBegin) && - ((otherEnd = other. getEnding()) != null) && compare(TimeMethods.Test.BEFORE, selfEnd, otherEnd); + final Instant otherBegin = other.getBeginning(); + final Instant selfEnd; // Assignation inlined for invoking method only if needed. + return compare(TimeMethods.Test.BEFORE, self.getBeginning(), otherBegin) && + compare(TimeMethods.Test.AFTER, selfEnd = self.getEnding(), otherBegin) && + compare(TimeMethods.Test.BEFORE, selfEnd, other.getEnding()); } } @@ -788,11 +787,11 @@ abstract class TemporalOperation<T> implements Serializable { /** Condition defined by ISO 19108:2006 (corrigendum) §5.2.3.5. */ @Override public boolean evaluate(final Period self, final Period other) { - final Instant selfBegin, selfEnd, otherBegin, otherEnd; - return ((selfBegin = self.getBeginning()) != null) && - ((otherBegin = other.getBeginning()) != null) && compare(TimeMethods.Test.AFTER, selfBegin, otherBegin) && - ((otherEnd = other. getEnding()) != null) && compare(TimeMethods.Test.BEFORE, selfBegin, otherEnd) && - ((selfEnd = self. getEnding()) != null) && compare(TimeMethods.Test.AFTER, selfEnd, otherEnd); + final Instant selfBegin = self.getBeginning(); + final Instant otherEnd; // Assignation inlined for invoking method only if needed. + return compare(TimeMethods.Test.AFTER, selfBegin, other.getBeginning()) && + compare(TimeMethods.Test.BEFORE, selfBegin, otherEnd = other.getEnding()) && + compare(TimeMethods.Test.AFTER, self.getEnding(), otherEnd); } } @@ -800,8 +799,7 @@ abstract class TemporalOperation<T> implements Serializable { /** * The {@code "AnyInteracts"} filter. * This is a shortcut for NOT (Before OR Meets OR MetBy OR After). - * An instant is handled as a tiny period, so an instant interacts with a period - * if it is after the period beginning and before the period ending. + * As an extension to OGC filter specification, an instant is handled as a tiny period. * * @param <T> the base type of temporal objects. */ @@ -833,11 +831,8 @@ abstract class TemporalOperation<T> implements Serializable { /** Condition defined by OGC filter specification. */ @Override public boolean evaluate(final Period self, final Period other) { - final Instant selfBegin, selfEnd, otherBegin, otherEnd; - return ((selfBegin = self.getBeginning()) != null) && - ((otherEnd = other. getEnding()) != null) && compare(TimeMethods.Test.BEFORE, selfBegin, otherEnd) && - ((selfEnd = self. getEnding()) != null) && - ((otherBegin = other.getBeginning()) != null) && compare(TimeMethods.Test.AFTER, selfEnd, otherBegin); + return compare(TimeMethods.Test.BEFORE, self.getBeginning(), other.getEnding()) && + compare(TimeMethods.Test.AFTER, self.getEnding(), other.getBeginning()); } } } diff --git a/endorsed/src/org.apache.sis.feature/test/org/apache/sis/filter/TemporalFilterTest.java b/endorsed/src/org.apache.sis.feature/test/org/apache/sis/filter/TemporalFilterTest.java index ca506b72f3..4db04d64b4 100644 --- a/endorsed/src/org.apache.sis.feature/test/org/apache/sis/filter/TemporalFilterTest.java +++ b/endorsed/src/org.apache.sis.feature/test/org/apache/sis/filter/TemporalFilterTest.java @@ -28,6 +28,7 @@ import org.apache.sis.test.TestCase; import static org.apache.sis.test.Assertions.assertSerializedEquals; // Specific to the geoapi-3.1 and geoapi-4.0 branches: +import org.opengis.temporal.TemporalPrimitive; import org.opengis.temporal.Period; import org.opengis.feature.Feature; import org.opengis.filter.FilterFactory; @@ -41,11 +42,12 @@ import org.opengis.filter.TemporalOperatorName; * @author Johann Sorel (Geomatys) * @author Martin Desruisseaux (Geomatys) */ +@SuppressWarnings("exports") public final class TemporalFilterTest extends TestCase { /** * The factory to use for creating the objects to test. */ - private FilterFactory<Feature, Object, ? super Period> factory; + private FilterFactory<Feature, Object, ? super TemporalPrimitive> factory; /** * The filter to test. This field shall be assigned by each {@code testFoo()} method by invoking @@ -89,6 +91,9 @@ public final class TemporalFilterTest extends TestCase { /** * Evaluates the filter with a null resource. * This is okay if all filter operands are literal. + * + * <p>We use this method as a placeholder for future tests where the operands would not be literals. + * We may also add more assertions here.</p> */ private boolean evaluate() { return filter.test(null); @@ -357,22 +362,30 @@ public final class TemporalFilterTest extends TestCase { */ @Test public void testOnPeriods() { - factory = new DefaultFilterFactory.Features<>(Object.class, Period.class, WraparoundMethod.NONE); + factory = new DefaultFilterFactory.Features<>(Object.class, TemporalPrimitive.class, WraparoundMethod.NONE); testAfter(); } /** * Tests filters between two period literals. - * The period class used by the literals is final and has no natural ordering. + * The period class used by the literals has no natural ordering. + * Comparisons should be applied on their {@link Instant} components. */ @Test public void testPeriodLiterals() { - final FilterFactory<Feature,Object,Object> ff = DefaultFilterFactory.forFeatures(); - final Period inner = TemporalObjects.createPeriod(Instant.parse("2000-01-02T00:00:00Z"), Instant.parse("2000-01-03T00:00:00Z")); - final Period outer = TemporalObjects.createPeriod(Instant.parse("2000-01-01T00:00:00Z"), Instant.parse("2000-01-05T00:00:00Z")); - assertTrue (ff.during (ff.literal(inner), ff.literal(outer)).test(null)); - assertFalse(ff.during (ff.literal(outer), ff.literal(inner)).test(null)); - assertTrue (ff.anyInteracts(ff.literal(inner), ff.literal(outer)).test(null)); + expression2.begin = Instant.parse("2000-01-01T00:00:00Z").toEpochMilli(); + expression2.end = Instant.parse("2000-01-06T00:00:00Z").toEpochMilli(); + final var outer = factory.literal(expression2); + final var inner = factory.literal(expression1); // 2000-01-01T09:00:00Z to 2000-01-05T10:00:00Z + + filter = factory.during(inner, outer); + assertTrue(evaluate()); + + filter = factory.during(outer, inner); + assertFalse(evaluate()); + + filter = factory.anyInteracts(inner, outer); + assertTrue(evaluate()); } /** @@ -380,17 +393,66 @@ public final class TemporalFilterTest extends TestCase { */ @Test public void testInstantAndPeriod() { - final FilterFactory<Feature,Object,Object> ff = DefaultFilterFactory.forFeatures(); - final Period period = TemporalObjects.createPeriod(Instant.parse("2000-01-01T00:00:00Z"), Instant.parse("2000-01-05T00:00:00Z")); - final Instant inside = Instant.parse("2000-01-03T00:00:00Z"); - final Instant outside = Instant.parse("2000-01-07T00:00:00Z"); - final Instant start = Instant.parse("2000-01-01T00:00:00Z"); - assertTrue (ff.during (ff.literal(inside), ff.literal(period)).test(null)); - assertFalse(ff.during (ff.literal(outside), ff.literal(period)).test(null)); - assertFalse(ff.during (ff.literal(start), ff.literal(period)).test(null), "Boundaries are excluded."); - assertTrue (ff.anyInteracts(ff.literal(inside), ff.literal(period)).test(null)); - assertTrue (ff.anyInteracts(ff.literal(period), ff.literal(inside)).test(null)); - assertFalse(ff.anyInteracts(ff.literal(outside), ff.literal(period)).test(null)); - assertFalse(ff.anyInteracts(ff.literal(start), ff.literal(period)).test(null), "Meets is not an interaction."); + final var period = factory.literal(expression1); // 2000-01-01T09:00:00Z to 2000-01-05T10:00:00Z + final var inside = factory.literal(TemporalObjects.createInstant(Instant.parse("2000-01-01T09:30:00Z"))); + final var outside = factory.literal(TemporalObjects.createInstant(Instant.parse("2000-01-07T00:00:00Z"))); + final var start = factory.literal(TemporalObjects.createInstant(Instant.ofEpochMilli(expression1.begin))); + + filter = factory.during(inside, period); + assertTrue(evaluate()); + + filter = factory.during(period, inside); + assertFalse(evaluate()); + + filter = factory.during(outside, period); + assertFalse(evaluate()); + + filter = factory.during(start, period); + assertFalse(evaluate(), "Boundaries shall be excluded."); + + filter = factory.anyInteracts(inside, period); + assertTrue(evaluate()); + + filter = factory.anyInteracts(period, inside); + assertTrue(evaluate()); + + filter = factory.anyInteracts(outside, period); + assertFalse(evaluate()); + + filter = factory.anyInteracts(start, period); + assertFalse(evaluate(), "Meets is not an interaction."); + } + + /** + * Subset of {@link #testInstantAndPeriod()} but with Java instants instead of ISO 19108. + * In principle the instants should be instances of {@code org.opengis.temporal.Instant}, + * but Apache <abbr>SIS</abbr> should also accept instances of {@code java.time.Instant}. + */ + @Test + public void testJavaInstantAndPeriod() { + final var period = factory.literal(expression1); // 2000-01-01T09:00:00Z to 2000-01-05T10:00:00Z + final var inside = factory.literal(Instant.parse("2000-01-01T09:30:00Z")); + final var outside = factory.literal(Instant.parse("2000-01-07T00:00:00Z")); + + // This test needs a factory accepting `Object` for the <T> parameterized type. + final var ff = DefaultFilterFactory.forFeatures(); + + filter = ff.during(inside, period); + assertTrue(evaluate()); + + filter = ff.during(period, inside); + assertFalse(evaluate()); + + filter = ff.during(outside, period); + assertFalse(evaluate()); + + filter = ff.anyInteracts(inside, period); + assertTrue(evaluate()); + + filter = ff.anyInteracts(period, inside); + assertTrue(evaluate()); + + filter = ff.anyInteracts(outside, period); + assertFalse(evaluate()); } } diff --git a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/DefaultInstant.java b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/DefaultInstant.java index 0b1eed0cc9..450d1c8e39 100644 --- a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/DefaultInstant.java +++ b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/DefaultInstant.java @@ -43,11 +43,12 @@ import org.opengis.filter.TemporalOperatorName; * This is not the same as {@link java.time.Instant}, because the * instant can actually be a date, or may be indeterminate. * - * <h2>Thread-safety</h2> - * Instances of this class are mostly immutable, except for the list of identifiers. - * All instances are thread-safe. + * <p>Instances of this class are mostly immutable, except for the list of identifiers. + * All instances are thread-safe.</p> * * @author Martin Desruisseaux (Geomatys) + * + * @see TemporalObjects#createInstant(Temporal) */ final class DefaultInstant extends TemporalObject implements Instant { /** @@ -55,6 +56,11 @@ final class DefaultInstant extends TemporalObject implements Instant { */ private static final long serialVersionUID = 3898772638524283287L; + /** + * The constant for the "now" instant. + */ + static final DefaultInstant NOW = new DefaultInstant(null, IndeterminateValue.NOW); + /** * The constant for the "unknown" instant. */ diff --git a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/GeneralDuration.java b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/GeneralDuration.java index 50719b4789..d4da5c4c68 100644 --- a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/GeneralDuration.java +++ b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/GeneralDuration.java @@ -101,7 +101,6 @@ public final class GeneralDuration implements TemporalAmount, Serializable { * * @param text the text to parse. * @return the parsed period and/or duration. - * @throws DateTimeParseException if the given text cannot be parsed. * * @see Period#parse(CharSequence) * @see Duration#parse(CharSequence) diff --git a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/TemporalObjects.java b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/TemporalObjects.java index 3ff7ef3938..7a08f3d2c6 100644 --- a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/TemporalObjects.java +++ b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/TemporalObjects.java @@ -63,6 +63,9 @@ public final class TemporalObjects { if (indeterminate == IndeterminateValue.UNKNOWN) { return DefaultInstant.UNKNOWN; } + if (indeterminate == IndeterminateValue.NOW) { + return DefaultInstant.NOW; + } if (indeterminate == null || indeterminate == IndeterminateValue.BEFORE || indeterminate == IndeterminateValue.AFTER) { ArgumentChecks.ensureNonNull("position", position); } @@ -82,7 +85,7 @@ public final class TemporalObjects { if (indeterminate == IndeterminateValue.BEFORE || indeterminate == IndeterminateValue.AFTER) { throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalArgumentValue_2, "indeterminate", indeterminate)); } - return (indeterminate == IndeterminateValue.UNKNOWN) ? DefaultInstant.UNKNOWN : new DefaultInstant(null, indeterminate); + return createInstant(null, indeterminate); } /** diff --git a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/TimeMethods.java b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/TimeMethods.java index 5bbf882899..0adc65ff26 100644 --- a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/TimeMethods.java +++ b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/temporal/TimeMethods.java @@ -214,11 +214,13 @@ public final class TimeMethods<T> implements Serializable { /** * Whether the temporal object have a time zone, explicitly or implicitly. + * Used only for {@link Temporal} objects. */ private final transient boolean hasZone; /** - * Whether the end point will be determined dynamically every time that a method is invoked. + * Whether the end point may be determined dynamically every time that a method is invoked. + * Used for avoiding infinite recursive call. */ public final transient boolean isDynamic; @@ -347,9 +349,9 @@ public final class TimeMethods<T> implements Serializable { if (self == null || other == null) { return Boolean.FALSE; } - boolean isTemporal = false; - if (self instanceof TemporalDate) {self = ((TemporalDate) self).temporal; isTemporal = true;} - if (other instanceof TemporalDate) {other = ((TemporalDate) other).temporal; isTemporal = true;} + boolean isTemporal; // For skipping unecessary `if (x instanceof Temporal)` checks later. + isTemporal = (self != (self = unwrapTemporal(self ))); + isTemporal |= (other != (other = unwrapTemporal(other))); /* * For legacy java.util.Date, the compareTo(…) method is consistent only for dates of the same class. * Otherwise A.compareTo(B) and B.compareTo(A) are inconsistent if one object is a java.util.Date and @@ -363,19 +365,38 @@ public final class TimeMethods<T> implements Serializable { other = fromLegacy((Date) other); isTemporal = true; // For skipping unecessary `if (x instanceof Temporal)` checks. } - // Use `||` because an operand by still be a `java.utl.Date`. - if (isTemporal || self instanceof Temporal || other instanceof Temporal) { - return compareTemporalOrDate(test, self, other); + if (self != null && other != null) { + // Use `||` because an operand by still be a `java.utl.Date`. + if (isTemporal || self instanceof Temporal || other instanceof Temporal) { + return compareTemporalOrDate(test, self, other); + } } return null; } + /** + * Unwraps the {@link Temporal} component, or returns the object itself if it is not a wrapper. + * If the returned value is different, then it is an instance of {@link Temporal}. + */ + private static Object unwrapTemporal(final Object value) { + if (value instanceof TemporalDate) { + return ((TemporalDate) value).temporal; + } else if (value instanceof org.opengis.temporal.Instant) { + return ((org.opengis.temporal.Instant) value).getPosition(); + } else { + return value; + } + } + /** * Returns whether the given type is considered temporal. This is necessary for avoiding that * comparable objects such as {@link String} are wrongly handled by {@code TimeMethods}. */ private static boolean isTemporal(final Class<?> type) { - return (type != null) && (Temporal.class.isAssignableFrom(type) || Date.class.isAssignableFrom(type)); + return (type != null) && + (Temporal.class.isAssignableFrom(type) + || Date.class.isAssignableFrom(type) + || org.opengis.temporal.Instant.class.isAssignableFrom(type)); } /** @@ -695,12 +716,14 @@ adapt: if (type != other.getClass()) { * @param type the type of temporal object for which to get the last-resource fallback methods. * @return set of last-resort comparison methods for the given object type. */ + @SuppressWarnings("unchecked") private static <T> TimeMethods<? super T> fallback(final Class<T> type) { return new TimeMethods<>(type, null, (self, other) -> compareIfTemporalElseFalse(Test.BEFORE, self, other), (self, other) -> compareIfTemporalElseFalse(Test.AFTER, self, other), (self, other) -> compareIfTemporalElseFalse(Test.EQUAL, self, other), - null, null, false, true); + type.isAssignableFrom(Instant.class) ? (Supplier) Instant::now : null, + null, false, true); } /** @@ -720,7 +743,9 @@ adapt: if (type != other.getClass()) { */ public static <T> TimeMethods<? super T> forType(final Class<T> type) { final TimeMethods<? super T> methods = forType(type, true); - if (methods != null) return methods; + if (methods != null) { + return methods; + } throw new DateTimeException(Errors.format(Errors.Keys.CannotCompareInstanceOf_2, type, type)); } @@ -797,7 +822,16 @@ adapt: if (type != other.getClass()) { new TimeMethods<>(ChronoZonedDateTime.class, null, ChronoZonedDateTime::isBefore, ChronoZonedDateTime::isAfter, ChronoZonedDateTime::isEqual, ZonedDateTime::now, ChronoZonedDateTime::withZoneSameInstant, true, false), new TimeMethods<>(ChronoLocalDateTime.class, TimeMethods::toLocalDateTime, ChronoLocalDateTime::isBefore, ChronoLocalDateTime::isAfter, ChronoLocalDateTime::isEqual, LocalDateTime::now, ChronoLocalDateTime::atZone, false, false), new TimeMethods<>( ChronoLocalDate.class, TimeMethods::toLocalDate, ChronoLocalDate::isBefore, ChronoLocalDate::isAfter, ChronoLocalDate::isEqual, LocalDate::now, null, false, false), - new TimeMethods<>( Date.class, null, Date:: before, Date:: after, Date::equals, Date::new, TimeMethods::atZone, true, false) + new TimeMethods<>( Date.class, null, Date:: before, Date:: after, Date::equals, Date::new, TimeMethods::atZone, true, false), + new TimeMethods<>(org.opengis.temporal.Instant.class, + (object) -> (object instanceof Temporal) ? TemporalObjects.createInstant((Temporal) object) : null, + (self, other) -> compareLenient(Test.BEFORE, self.getPosition(), other.getPosition()), + (self, other) -> compareLenient(Test.AFTER, self.getPosition(), other.getPosition()), + (self, other) -> compareLenient(Test.EQUAL, self.getPosition(), other.getPosition()), + () -> DefaultInstant.NOW, + null, // withZone + false, // hasZone (not applicable) + true) // isDynamic (conservative value) }; /* @@ -811,11 +845,11 @@ adapt: if (type != other.getClass()) { * The two last entries are not final, but we really want to ignore all their subtypes. * All those types should be tested before {@link #FOR_PARENT_TYPES} because this check is quick. * - * <h4>Implementation note</h4> + * <p><b>Implementation note:</b> * {@link Year}, {@link YearMonth}, {@link MonthDay}, {@link LocalTime} and {@link Instant} * could be replaced by {@link Comparable}. We nevertheless keep the specialized classes in * case the implementations change in the future, and also for performance reason, because - * the code working on generic {@link Comparable} needs to check for special cases again. + * the code working on generic {@link Comparable} needs to check for special cases again.</p> */ private static final Map<Class<?>, TimeMethods<?>> FOR_EXACT_TYPES = Map.ofEntries( entry(new TimeMethods<>(OffsetDateTime.class, TimeMethods::toOffsetDateTime, OffsetDateTime::isBefore, OffsetDateTime::isAfter, OffsetDateTime::isEqual, OffsetDateTime::now, TimeMethods::withZoneSameInstant, true, false)), @@ -1010,8 +1044,8 @@ adapt: if (type != other.getClass()) { } /** - * Invoked when a conversion cause a field to be ignored. For example if a "date+time" object is compared - * with a "date" object, the "time" field is ignored. Expected values are: + * Invoked when a conversion causes a field to be ignored. For example, if a "date+time" object + * is compared with a "date" object, the "time" field is ignored. Expected values are: * * <ul> * <li>{@link ChronoField#OFFSET_SECONDS}: time zone is ignored.</li>
