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

com.tangosol.util.aggregator.ComparableMax Maven / Gradle / Ivy

There is a newer version: 24.09
Show newest version
/*
 * Copyright (c) 2000, 2020, 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.aggregator;


import com.tangosol.util.InvocableMap;
import com.tangosol.util.ValueExtractor;

import com.tangosol.util.comparator.SafeComparator;

import java.util.Comparator;

/**
* Calculates a maximum among values extracted from a set of entries in a Map.
* This aggregator is most commonly used with objects that implement
* {@link Comparable} such as {@link String String} or
* {@link java.util.Date Date}; a {@link Comparator} can also be supplied to
* perform the comparisons.
*
* @param   the type of the value to extract from
* @param   the type of the result
*
* @author gg  2006.02.13
* @since Coherence 3.2
*/
public class ComparableMax
        extends AbstractComparableAggregator
    {
    // ----- constructors ---------------------------------------------------

    /**
    * Default constructor (necessary for the ExternalizableLite interface).
    */
    public ComparableMax()
        {
        super();
        }

    /**
    * Construct a ComparableMax aggregator.
    *
    * @param extractor  the extractor that provides a value in the form of
    *                   any object that implements {@link Comparable}
    *                   interface
    */
    public > ComparableMax(ValueExtractor extractor)
        {
        super(extractor);
        }

    /**
    * Construct a ComparableMax aggregator.
    *
    * @param extractor  the extractor that provides an object to be compared
    * @param comparator the comparator used to compare the extracted object
    */
    public ComparableMax(ValueExtractor extractor,
                         Comparator comparator)
        {
        super(extractor, comparator);
        }

    /**
    * Construct a ComparableMax aggregator.
    *
    * @param sMethod  the name of the method that returns a value in the form
    *                 of any object that implements {@link Comparable}
    *                 interface
    */
    public ComparableMax(String sMethod)
        {
        super(sMethod);
        }

    // ----- StreamingAggregator methods ------------------------------------

    @Override
    public InvocableMap.StreamingAggregator supply()
        {
        return new ComparableMax<>(getValueExtractor(), m_comparator);
        }

    @Override
    public int characteristics()
        {
        return PARALLEL | PRESENT_ONLY;
        }

    // ----- AbstractAggregator methods -------------------------------------

    /**
    * {@inheritDoc}
    */
    protected void process(Object o, boolean fFinal)
        {
        if (o != null)
            {
            R oResult = m_oResult;
            if (oResult == null ||
                SafeComparator.compareSafe(m_comparator, oResult, o) < 0)
                {
                m_oResult = (R) o;
                }
            m_count++;
            }
        }
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy