Class ComparisonFilter<R>

Type Parameters:
R - the type of resources (e.g. Feature) used as inputs.
All Implemented Interfaces:
Serializable, Predicate<R>, Filter<R>, Optimization.OnFilter<R>, BinaryComparisonOperator<R>
Direct Known Subclasses:
ComparisonFilter.EqualTo, ComparisonFilter.GreaterThan, ComparisonFilter.GreaterThanOrEqualTo, ComparisonFilter.LessThan, ComparisonFilter.LessThanOrEqualTo, ComparisonFilter.NotEqualTo

abstract class ComparisonFilter<R> extends BinaryFunction<R,Object,Object> implements BinaryComparisonOperator<R>, Optimization.OnFilter<R>
Comparison operators between two values. Values are converted to the same type before comparison, using a widening conversion (for example from Integer to Double). If values cannot be compared because they cannot be converted to a common type, or because a value is null or NaN, then the comparison result if false. A consequence of this rule is that the conditions A < B and A ≥ B may be false at the same time.

If one operand is a collection, all collection elements may be compared to the other value. Null elements in the collection (not to be confused with null operands) are ignored. If both operands are collections, current implementation returns false.

Comparisons between temporal objects are done with isBefore(…) or isAfter(…) methods when they have a different semantic than the compareTo(…) methods. If the two temporal objects are not of the same type, only the fields that are common two both types are compared. For example, comparison between LocalDate and LocalDateTime ignores the time fields.

Comparisons of numerical types shall be done by overriding one of the applyAs… methods and returning 0 if false or 1 if true. Comparisons of other types is done by overriding the compare(…) methods.

Since:
1.1
Version:
1.1
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      For cross-version compatibility.
      See Also:
    • isMatchingCase

      protected final boolean isMatchingCase
      Specifies whether comparisons are case sensitive.
    • matchAction

      protected final MatchAction matchAction
      Specifies how the comparisons shall be evaluated for a collection of values. Values can be ALL, ANY or ONE.
  • Constructor Details

    • ComparisonFilter

      ComparisonFilter(Expression<? super R,?> expression1, Expression<? super R,?> expression2, boolean isMatchingCase, MatchAction matchAction)
      Creates a new comparator.
      Parameters:
      expression1 - the first of the two expressions to be used by this comparator.
      expression2 - the second of the two expressions to be used by this comparator.
      isMatchingCase - specifies whether comparisons are case sensitive.
      matchAction - specifies how the comparisons shall be evaluated for a collection of values.
  • Method Details

    • getOperand1

      public final Expression<? super R,?> getOperand1()
      Returns the element on the left side of the comparison expression. This is the element at index 0 in the list of expressions.
      Specified by:
      getOperand1 in interface BinaryComparisonOperator<R>
    • getOperand2

      public final Expression<? super R,?> getOperand2()
      Returns the element on the right side of the comparison expression. This is the element at index 1 in the list of expressions.
      Specified by:
      getOperand2 in interface BinaryComparisonOperator<R>
    • isMatchingCase

      public final boolean isMatchingCase()
      Returns whether comparisons are case sensitive.
      Specified by:
      isMatchingCase in interface BinaryComparisonOperator<R>
    • getMatchAction

      public final MatchAction getMatchAction()
      Returns how the comparisons are evaluated for a collection of values.
      Specified by:
      getMatchAction in interface BinaryComparisonOperator<R>
    • hashCode

      public final int hashCode()
      Takes in account the additional properties in hash code calculation.
      Overrides:
      hashCode in class Node
    • equals

      public final boolean equals(Object obj)
      Takes in account the additional properties in object comparison.
      Overrides:
      equals in class Node
      Parameters:
      obj - the other object to compare with this node.
      Returns:
      whether the two object are equal.
    • test

      public final boolean test(R candidate)
      Determines if the test(s) represented by this filter passes with the given operands. Values of BinaryFunction.expression1 and BinaryFunction.expression2 can be two single values, or at most one expression can produce a collection.
      Specified by:
      test in interface Filter<R>
      Specified by:
      test in interface Predicate<R>
      Parameters:
      candidate - the object (often a Feature instance) to evaluate.
      Returns:
      true if the test(s) are passed for the provided object.
    • evaluate

      private boolean evaluate(Object left, Object right)
      Compares the given objects. If both values are numerical, then this method delegates to an applyAs… method. For other kind of objects, this method delegates to a compare(…) method. If the two objects are not of the same type, then the less accurate one is converted to the most accurate type if possible.
      Parameters:
      left - the first object to compare. Must be non-null.
      right - the second object to compare. Must be non-null.
    • fromLegacy

      private static Temporal fromLegacy(Date value)
      Converts a legacy Date object to an object from the java.time package. We performs this conversion before to compare to Date instances that are not of the same class, because the Date.compareTo(Date) method in such case is not well defined.
    • toInstant

      static Instant toInstant(Object value)
      Converts the given object to an Instant, or returns null if unconvertible. This method handles a few types from the java.time package and legacy types like Date (with a special case for SQL dates) and Calendar.
    • toOffsetDateTime

      private static OffsetDateTime toOffsetDateTime(Object value)
      Converts the given object to an OffsetDateTime, or returns null if unconvertible.
    • toLocalDateTime

      private static ChronoLocalDateTime<?> toLocalDateTime(Object value)
      Converts the given object to a ChronoLocalDateTime, or returns null if unconvertible. This method handles the case of legacy SQL Timestamp objects. Conversion may lost timezone information.
    • toLocalDate

      private static ChronoLocalDate toLocalDate(Object value)
      Converts the given object to a ChronoLocalDate, or returns null if unconvertible. This method handles the case of legacy SQL Date objects. Conversion may lost timezone information and time fields.
    • toLocalTime

      private static LocalTime toLocalTime(Object value)
      Converts the given object to a LocalTime, or returns null if unconvertible. This method handles the case of legacy SQL Time objects. Conversion may lost timezone information.
    • ignoringField

      private static void ignoringField(ChronoField field)
      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:
      Parameters:
      field - the field which is ignored.
      See Also:
    • number

      private static Number number(boolean result)
      Converts the boolean result as an integer for use as a return value of the applyAs… methods. This is a helper class for subclasses.
    • fromCompareTo

      protected abstract boolean fromCompareTo(int result)
      Converts the result of Comparable.compareTo(Object).
    • compare

      protected abstract boolean compare(OffsetTime left, OffsetTime right)
      Compares two times with time-zone information. Implementations shall not use compareTo(…) because that method compares more information than desired in order to ensure consistency with equals(…).
    • compare

      protected abstract boolean compare(OffsetDateTime left, OffsetDateTime right)
      Compares two dates with time-zone information. Implementations shall not use compareTo(…) because that method compares more information than desired in order to ensure consistency with equals(…).
    • compare

      protected abstract boolean compare(ChronoLocalDate left, ChronoLocalDate right)
      Compares two dates without time-of-day and time-zone information. Implementations shall not use compareTo(…) because that method also compares chronology, which is not desired for the purpose of "is before" or "is after" comparison functions.
    • compare

      protected abstract boolean compare(ChronoLocalDateTime<?> left, ChronoLocalDateTime<?> right)
      Compares two dates without time-zone information. Implementations shall not use compareTo(…) because that method also compares chronology, which is not desired for the purpose of "is before" or "is after" comparison functions.
    • compare

      protected abstract boolean compare(ChronoZonedDateTime<?> left, ChronoZonedDateTime<?> right)
      Compares two dates with time-zone information. Implementations shall not use compareTo(…) because that method also compares chronology, which is not desired for the purpose of "is before" or "is after" comparison functions.
    • applyAsDecimal

      protected final Number applyAsDecimal(BigDecimal left, BigDecimal right)
      Delegates to BigDecimal.compareTo(BigDecimal) and interprets the result with fromCompareTo(int).
      Overrides:
      applyAsDecimal in class BinaryFunction<R,Object,Object>
    • applyAsInteger

      protected final Number applyAsInteger(BigInteger left, BigInteger right)
      Description copied from class: BinaryFunction
      Calculates this function using given operands of BigInteger type. If this function is a filter, then this method should returns an Integer value 0 or 1 for false or true respectively. Otherwise the result is usually a BigInteger, except for division which may produce other types. This method may return null if the operation cannot apply on numbers.
      Overrides:
      applyAsInteger in class BinaryFunction<R,Object,Object>
    • applyAsFraction

      protected final Number applyAsFraction(Fraction left, Fraction right)
      Description copied from class: BinaryFunction
      Calculates this function using given operands of Fraction type. If this function is a filter, then this method should returns an Integer value 0 or 1 for false or true respectively. Otherwise the result is usually a Fraction. This method may return null if the operation cannot apply on numbers.
      Overrides:
      applyAsFraction in class BinaryFunction<R,Object,Object>