All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tangosol.util.filter.GreaterEqualsFilter Maven / Gradle / Ivy

There is a newer version: 24.03
Show newest version
/*
 * Copyright (c) 2000, 2023, Oracle and/or its affiliates.
 *
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * https://oss.oracle.com/licenses/upl.
 */

package com.tangosol.util.filter;


import com.tangosol.util.Filter;
import com.tangosol.util.MapIndex;
import com.tangosol.util.ValueExtractor;
import com.tangosol.util.SafeSortedMap;

import java.util.HashSet;
import java.util.Iterator;
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
* "Greater or Equal" condition. 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 value to use for comparison
*
* @author cp/gg 2002.10.29
*/
public class GreaterEqualsFilter>
        extends    ComparisonFilter
        implements IndexAwareFilter
    {
    // ----- constructors ---------------------------------------------------

    /**
    * Default constructor (necessary for the ExternalizableLite interface).
    */
    public GreaterEqualsFilter()
        {
        }

    /**
    * Construct a GreaterEqualFilter for testing "Greater or Equal"
    * condition.
    *
    * @param extractor the ValueExtractor to use by this filter
    * @param value     the object to compare the result with
    */
    public GreaterEqualsFilter(ValueExtractor extractor, E value)
        {
        super(extractor, value);
        }

    /**
    * Construct a GreaterEqualFilter for testing "Greater or Equal"
    * condition.
    *
    * @param sMethod  the name of the method to invoke via reflection
    * @param value    the object to compare the result with
    */
    public GreaterEqualsFilter(String sMethod, E value)
        {
        super(sMethod, value);
        }


    // ----- ExtractorFilter methods ----------------------------------------

    /**
    * {@inheritDoc}
    */
    protected boolean evaluateExtracted(E extracted)
        {
        E value = getValue();

        return extracted != null && value != null &&
            extracted.compareTo(value) >= 0;
        }


    // ----- IndexAwareFilter interface -------------------------------------

    /**
    * {@inheritDoc}
    */
    public int calculateEffectiveness(Map mapIndexes, Set setKeys)
        {
        return calculateRangeEffectiveness(mapIndexes, setKeys);
        }

    /**
    * {@inheritDoc}
    */
    public Filter applyIndex(Map mapIndexes, Set setKeys)
        {
        Object oValue = getValue();
        if (oValue == null)
            {
            // nothing could be compared to null
            setKeys.clear();
            return null;
            }

        MapIndex index = (MapIndex) mapIndexes.get(getValueExtractor());
        if (index == null)
            {
            // there is no relevant index
            return this;
            }

        if (index.isOrdered())
            {
            SortedMap mapContents = (SortedMap) index.getIndexContents();
            SortedMap mapLT       = mapContents.headMap(oValue);
            SortedMap mapGE       = mapContents.tailMap(oValue);
            boolean   fHeadHeavy  = mapLT.size() > mapContents.size() / 2;

            if (fHeadHeavy || index.isPartial())
                {
                Set setGE = new HashSet();
                for (Iterator iterGE = mapGE.values().iterator(); iterGE.hasNext();)
                    {
                    setGE.addAll(ensureSafeSet((Set) iterGE.next()));
                    }
                setKeys.retainAll(setGE);
                }
            else
                {
                for (Iterator iterLT = mapLT.values().iterator(); iterLT.hasNext();)
                    {
                    setKeys.removeAll(ensureSafeSet((Set) iterLT.next()));
                    }
                }
            // Note: the NULL set doesn't get in
            }
        else
            {
            Map mapContents = index.getIndexContents();

            if (index.isPartial())
                {
                Set setGE = new HashSet();
                for (Iterator iter = mapContents.entrySet().iterator();
                     iter.hasNext();)
                    {
                    Map.Entry  entry = (Map.Entry) iter.next();
                    Comparable oTest = (Comparable) entry.getKey();
                    if (oTest != null && oTest.compareTo(oValue) >= 0)
                        {
                        setGE.addAll(ensureSafeSet((Set) entry.getValue()));
                        }
                    }
                setKeys.retainAll(setGE);
                }
            else
                {
                for (Iterator iter = mapContents.entrySet().iterator();
                     iter.hasNext();)
                    {
                    Map.Entry  entry = (Map.Entry) iter.next();
                    Comparable oTest = (Comparable) entry.getKey();
                    if (oTest == null || oTest.compareTo(oValue) < 0)
                        {
                        setKeys.removeAll((Set) entry.getValue());
                        }
                    }
                }
            }

        return null;
        }
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy