com.tangosol.util.filter.BetweenFilter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of coherence Show documentation
Show all versions of coherence Show documentation
Oracle Coherence Community Edition
/*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
package com.tangosol.util.filter;
import com.tangosol.util.Filter;
import com.tangosol.util.InvocableMapHelper;
import com.tangosol.util.MapIndex;
import com.tangosol.util.QueryContext;
import com.tangosol.util.QueryMap;
import com.tangosol.util.QueryRecord;
import com.tangosol.util.ValueExtractor;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
/**
* 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.
*
* @param the type of the input argument to the filter
* @param the type of the extracted attribute to use for comparison
*
* @author cp/gg 2002.10.29
* @author jk 2014.05.20
*/
// This class extends AndFilter to maintain backward compatibility with previous
// versions of Coherence. The methods of AndFilter are overridden in this class
// so that it effectively behave more like an ExtractorFilter.
public class BetweenFilter>
extends AndFilter
{
// ----- constructors ---------------------------------------------------
/**
* Default constructor (necessary for serialization).
*/
public BetweenFilter()
{
}
/**
* Construct a BetweenFilter for testing "Between" condition.
*
* @param extractor the ValueExtractor to use by this filter
* @param from the object to compare the "Greater or Equals"
* boundary with
* @param to the object to compare the "Less or Equals" boundary
* with
*/
public BetweenFilter(ValueExtractor extractor, E from, E to)
{
this(extractor, from, to, true, true);
}
/**
* Construct a BetweenFilter for testing "Between" condition.
*
* @param sMethod the name of the method to invoke via reflection
* @param from the object to compare the "Greater or Equals" boundary
* with
* @param to the object to compare the "Less or Equals" boundary
* with
*/
public BetweenFilter(String sMethod, E from, E to)
{
this(sMethod, from, to, true, true);
}
/**
* Construct a BetweenFilter for testing "Between" condition.
*
* @param sMethod the name of the method to invoke via reflection
* @param lowerBound the lower bound of the range
* @param upperBound the upper bound of the range
* @param fIncludeLowerBound a flag indicating whether values matching the lower bound evaluate to true
* @param fIncludeUpperBound a flag indicating whether values matching the upper bound evaluate to true
*/
public BetweenFilter(String sMethod, E lowerBound, E upperBound,
boolean fIncludeLowerBound, boolean fIncludeUpperBound)
{
super(fIncludeLowerBound
? new GreaterEqualsFilter<>(sMethod, lowerBound)
: new GreaterFilter<>(sMethod, lowerBound),
fIncludeUpperBound
? new LessEqualsFilter<>(sMethod, upperBound)
: new LessFilter<>(sMethod, upperBound));
}
/**
* Construct a BetweenFilter for testing "Between" condition.
*
* @param extractor the {@link ValueExtractor} to be used by this filter
* @param lowerBound the lower bound of the range
* @param upperBound the upper bound of the range
* @param fIncludeLowerBound a flag indicating whether values matching the lower bound evaluate to true
* @param fIncludeUpperBound a flag indicating whether values matching the upper bound evaluate to true
*/
public BetweenFilter(
ValueExtractor extractor, E lowerBound, E upperBound,
boolean fIncludeLowerBound, boolean fIncludeUpperBound)
{
super(fIncludeLowerBound
? new GreaterEqualsFilter<>(extractor, lowerBound)
: new GreaterFilter<>(extractor, lowerBound),
fIncludeUpperBound
? new LessEqualsFilter<>(extractor, upperBound)
: new LessFilter<>(extractor, upperBound));
}
// ----- accessor methods -----------------------------------------------
/**
* Obtain the ValueExtractor used by this filter.
*
* @return the ValueExtractor used by this filter
*/
public ValueExtractor getValueExtractor()
{
return ((ComparisonFilter) getFilters()[0]).getValueExtractor();
}
/**
* Obtain the lower bound of the range being used to evaluate
* values by this BetweenFilter.
*
* @return the lower bound of the range being used to evaluate
* values by this BetweenFilter
*/
public E getLowerBound()
{
return (E) ((ComparisonFilter) getFilters()[0]).getValue();
}
/**
* Obtain the upper bound of the range being used to evaluate
* values by this BetweenFilter.
*
* @return the upper bound of the range being used to evaluate
* values by this BetweenFilter
*/
public E getUpperBound()
{
return (E) ((ComparisonFilter) getFilters()[1]).getValue();
}
/**
* Obtain the flag indicating whether values matching the lower bound
* of the range evaluate to true.
*
* @return the flag indicating whether values matching the lower bound
* of the range evaluate to true
*/
public boolean isLowerBoundInclusive()
{
return getFilters()[0] instanceof GreaterEqualsFilter;
}
/**
* Obtain the flag indicating whether values matching the upper bound
* of the range evaluate to true.
*
* @return the flag indicating whether values matching the upper bound
* of the range evaluate to true
*/
public boolean isUpperBoundInclusive()
{
return getFilters()[1] instanceof LessEqualsFilter;
}
// ----- Filter methods -------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
public boolean evaluate(Object oTarget)
{
return evaluateExtracted(getValueExtractor().extract(oTarget));
}
// ----- EntryFilter interface ------------------------------------------
/**
* {@inheritDoc}
*/
public boolean evaluateEntry(Map.Entry entry)
{
ValueExtractor extractor = getValueExtractor();
return evaluateExtracted(entry instanceof QueryMap.Entry
? ((QueryMap.Entry) entry).extract(extractor)
: InvocableMapHelper.extractFromEntry(extractor, entry));
}
// ----- ArrayFilter methods --------------------------------------------
/**
* {@inheritDoc}
*/
@Override
protected boolean evaluateEntry(Map.Entry entry, QueryContext ctx, QueryRecord.PartialResult.TraceStep step)
{
return evaluateFilter(this, entry, ctx, step == null ? null : step.ensureStep(this));
}
// ----- IndexAwareFilter methods ---------------------------------------
/**
* {@inheritDoc}
*/
@Override
public Filter applyIndex(Map mapIndexes, Set setKeys)
{
if (getLowerBound() == null || getUpperBound() == null)
{
setKeys.clear();
return null;
}
MapIndex mapIndex = (MapIndex) mapIndexes.get(getValueExtractor());
if (mapIndex == null)
{
return this;
}
Map