Class BetweenFilter<T,​E extends Comparable<? super E>>

  • Type Parameters:
    T - the type of the input argument to the filter
    E - the type of the extracted attribute to use for comparison
    All Implemented Interfaces:
    ExternalizableLite, PortableObject, Filter, EntryFilter, IndexAwareFilter, QueryRecorderFilter, Serializable

    public class BetweenFilter<T,​E extends Comparable<? super E>>
    extends AndFilter
    Filter which compares the result of a method invocation with a value for "Between" condition. We use the standard ISO/IEC 9075:1992 semantic, according to which "X between Y and Z" is equivalent to "X >= Y && X <= Z". In a case when either result of a method invocation or a value to compare are equal to null, the evaluate test yields false. This approach is equivalent to the way the NULL values are handled by SQL.
    Author:
    cp/gg 2002.10.29, jk 2014.05.20
    See Also:
    Serialized Form
    • Constructor Detail

      • BetweenFilter

        public BetweenFilter()
        Default constructor (necessary for serialization).
      • BetweenFilter

        public BetweenFilter​(ValueExtractor<? super T,​? extends E> extractor,
                             E from,
                             E to)
        Construct a BetweenFilter for testing "Between" condition.
        Parameters:
        extractor - the ValueExtractor to use by this filter
        from - the object to compare the "Greater or Equals" boundary with
        to - the object to compare the "Less or Equals" boundary with
      • BetweenFilter

        public BetweenFilter​(String sMethod,
                             E from,
                             E to)
        Construct a BetweenFilter for testing "Between" condition.
        Parameters:
        sMethod - the name of the method to invoke via reflection
        from - the object to compare the "Greater or Equals" boundary with
        to - the object to compare the "Less or Equals" boundary with
      • BetweenFilter

        public BetweenFilter​(String sMethod,
                             E lowerBound,
                             E upperBound,
                             boolean fIncludeLowerBound,
                             boolean fIncludeUpperBound)
        Construct a BetweenFilter for testing "Between" condition.
        Parameters:
        sMethod - the name of the method to invoke via reflection
        lowerBound - the lower bound of the range
        upperBound - the upper bound of the range
        fIncludeLowerBound - a flag indicating whether values matching the lower bound evaluate to true
        fIncludeUpperBound - a flag indicating whether values matching the upper bound evaluate to true
      • BetweenFilter

        public BetweenFilter​(ValueExtractor<? super T,​? extends E> extractor,
                             E lowerBound,
                             E upperBound,
                             boolean fIncludeLowerBound,
                             boolean fIncludeUpperBound)
        Construct a BetweenFilter for testing "Between" condition.
        Parameters:
        extractor - the ValueExtractor to be used by this filter
        lowerBound - the lower bound of the range
        upperBound - the upper bound of the range
        fIncludeLowerBound - a flag indicating whether values matching the lower bound evaluate to true
        fIncludeUpperBound - a flag indicating whether values matching the upper bound evaluate to true
    • Method Detail

      • getValueExtractor

        public ValueExtractor getValueExtractor()
        Obtain the ValueExtractor used by this filter.
        Returns:
        the ValueExtractor used by this filter
      • getLowerBound

        public E getLowerBound()
        Obtain the lower bound of the range being used to evaluate values by this BetweenFilter.
        Returns:
        the lower bound of the range being used to evaluate values by this BetweenFilter
      • getUpperBound

        public E getUpperBound()
        Obtain the upper bound of the range being used to evaluate values by this BetweenFilter.
        Returns:
        the upper bound of the range being used to evaluate values by this BetweenFilter
      • isLowerBoundInclusive

        public boolean isLowerBoundInclusive()
        Obtain the flag indicating whether values matching the lower bound of the range evaluate to true.
        Returns:
        the flag indicating whether values matching the lower bound of the range evaluate to true
      • isUpperBoundInclusive

        public boolean isUpperBoundInclusive()
        Obtain the flag indicating whether values matching the upper bound of the range evaluate to true.
        Returns:
        the flag indicating whether values matching the upper bound of the range evaluate to true
      • evaluate

        public boolean evaluate​(Object oTarget)
        Apply the test to the input argument.
        Specified by:
        evaluate in interface Filter<T>
        Overrides:
        evaluate in class AllFilter
        Parameters:
        oTarget - the input argument to evaluate
        Returns:
        true if the input argument matches the filter, otherwise false
      • toExpression

        public String toExpression()
        Description copied from interface: Filter
        Return a string expression for this filter.
        Specified by:
        toExpression in interface Filter<T>
        Overrides:
        toExpression in class ArrayFilter
        Returns:
        a string expression for this filter
      • evaluateEntry

        protected boolean evaluateEntry​(Map.Entry entry,
                                        QueryContext ctx,
                                        QueryRecord.PartialResult.TraceStep step)
        Check if the given entry passes the filter's evaluation. Record the actual cost of execution for each of the participating filters if a query context is provided.
        Overrides:
        evaluateEntry in class AllFilter
        Parameters:
        entry - a key value pair to filter
        ctx - the query ctx; may be null
        step - the step used to record the execution cost
        Returns:
        true if the entry passes the filter, false otherwise
      • simplifyFilters

        protected Set<Filter<?>> simplifyFilters()
        Description copied from class: ArrayFilter
        Simplify internal filter array by merging and replacing filters if possible to reduce the overall number and nesting of the filters.
        Overrides:
        simplifyFilters in class AllFilter
        Returns:
        the simplified filter array
      • optimizeFilterOrder

        protected void optimizeFilterOrder​(Map mapIndexes,
                                           Set setKeys)
        Description copied from class: ArrayFilter
        Sort all the participating filters according to their effectiveness.
        Overrides:
        optimizeFilterOrder in class ArrayFilter
        Parameters:
        mapIndexes - the available MapIndex objects keyed by the related ValueExtractor; read-only
        setKeys - the set of keys that will be filtered; read-only
      • applyIndex

        public Filter applyIndex​(Map mapIndexes,
                                 Set setKeys)
        Filter remaining keys using a Map of available indexes.

        The filter is responsible for removing all keys from the passed set of keys that the applicable indexes can prove should be filtered. If the filter does not fully evaluate the remaining keys using just the index information, it must return a filter (which may be an EntryFilter) that can complete the task using an iterating implementation. If, on the other hand, the filter does fully evaluate the remaining keys using just the index information, then it should return null to indicate that no further filtering is necessary.

        Specified by:
        applyIndex in interface IndexAwareFilter<T,​E extends Comparable<? super E>>
        Overrides:
        applyIndex in class ArrayFilter
        Parameters:
        mapIndexes - the available MapIndex objects keyed by the related ValueExtractor; read-only
        setKeys - the mutable set of keys that remain to be filtered
        Returns:
        a Filter object (which may be an EntryFilter) that can be used to process the remaining keys, or null if no additional filter processing is necessary
      • calculateEffectiveness

        public int calculateEffectiveness​(Map mapIndexes,
                                          Set setKeys)
        Given a Map of available indexes, determine if this IndexAwareFilter can use any of the indexes to assist in its processing, and if so, determine how effective the use of that index would be.

        The returned value is an effectiveness estimate of how many keys will remain in the set after the index is applied. If no keys will remain in the set after the index is applied, this method should return 0. If all the keys will remain in the set, implying that no entries would be filtered out based on this filter, this method should return setKeys.size(). Otherwise, it should return the value between 0 and setKeys.size(). If there is no index in the specified index map that can be used by this filter, this method should return a negative integer.

        The effectiveness returned will be used by the composite filters to reorder nested filters from most to least effective, in order to optimize query execution.

        Specified by:
        calculateEffectiveness in interface IndexAwareFilter<T,​E extends Comparable<? super E>>
        Overrides:
        calculateEffectiveness in class AllFilter
        Parameters:
        mapIndexes - the available MapIndex objects keyed by the related ValueExtractor; read-only
        setKeys - the set of keys that will be filtered; read-only
        Returns:
        an effectiveness estimate of how well this filter can use the specified indexes to filter the specified keys
      • explain

        public void explain​(QueryContext ctx,
                            QueryRecord.PartialResult.ExplainStep step,
                            Set setKeys)
        Record the projected query execution cost by this filter.

        This method is expected to record the order of execution and estimated cost of applying corresponding indexes in the given step without actually applying any indexes or evaluating entries.

        Specified by:
        explain in interface QueryRecorderFilter<T>
        Overrides:
        explain in class ArrayFilter
        Parameters:
        ctx - the query context
        step - the step used to record the estimated execution cost
        setKeys - the set of keys that would be filtered
      • toString

        public String toString()
        Return a human-readable description for this Filter.
        Overrides:
        toString in class ArrayFilter
        Returns:
        a String description of the Filter
      • evaluateExtracted

        protected boolean evaluateExtracted​(Object oExtracted)
        Evaluate the specified extracted value.
        Parameters:
        oExtracted - an extracted value to evaluate
        Returns:
        true if the test passes
      • applySortedIndex

        protected void applySortedIndex​(MapIndex index,
                                        Set setKeys,
                                        SortedMap<E,​Set<?>> mapContents)
        Called by the applyIndex(java.util.Map, java.util.Set) method if the index corresponding to this filter's value extractor is a sorted index.
        Parameters:
        index - the index to apply
        setKeys - the set of keys of the entries being filtered
        mapContents - the index contents to evaluate
      • allOrNothing

        protected Integer allOrNothing​(MapIndex index,
                                       SortedMap<E,​Set<?>> mapContents,
                                       Set setKeys)
        Determine if the filter will match all or none of the entries in the index.
        Parameters:
        index - the index
        mapContents - the index contents
        setKeys - the set of keys to filter
        Returns:
        0 if no entries match; setKeys.size() if all entries match; and null if only some entries match or no conclusive determination can be made