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

com.tangosol.internal.util.stream.collectors.SummarizingDoubleCollector Maven / Gradle / Ivy

There is a newer version: 24.03
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.internal.util.stream.collectors;

import com.tangosol.internal.util.DoubleSummaryStatistics;

import com.tangosol.internal.util.invoke.Lambdas;

import com.tangosol.io.ExternalizableLite;

import com.tangosol.io.pof.PofReader;
import com.tangosol.io.pof.PofWriter;
import com.tangosol.io.pof.PortableObject;

import com.tangosol.util.ExternalizableHelper;

import com.tangosol.util.function.Remote;

import com.tangosol.util.stream.RemoteCollector;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;

import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;

import javax.json.bind.annotation.JsonbProperty;

/**
 * An implementation of {@code RemoteCollector} which applies an
 * {@code double}-producing mapping function to each input element, and returns
 * summary statistics for the resulting values.
 *
 * @param   the type of input elements to be collected
 *
 * @author as 20014.12.30
 * @since 12.2.1
 */
public class SummarizingDoubleCollector
        implements RemoteCollector,
                   ExternalizableLite, PortableObject
    {
    // ---- constructors ----------------------------------------------------

    /**
     * Deserialization constructor.
     */
    public SummarizingDoubleCollector()
        {
        }

    /**
     * Construct SummarizingDoubleCollector instance.
     *
     * @param mapper  a function extracting the property to be summarized
     */
    public SummarizingDoubleCollector(Remote.ToDoubleFunction mapper)
        {
        m_mapper = Lambdas.ensureRemotable(mapper);
        }

    // ---- Collector interface ---------------------------------------------

    @Override
    public Supplier supplier()
        {
        return DoubleSummaryStatistics::new;
        }

    @Override
    public BiConsumer accumulator()
        {
        final ToDoubleFunction mapper = m_mapper;
        return (a, t) -> a.accept(mapper.applyAsDouble(t));
        }

    @Override
    public BinaryOperator combiner()
        {
        return (a, b) ->
                    {
                    a.combine(b);
                    return a;
                    };
        }

    @Override
    public Function finisher()
        {
        return Function.identity();
        }

    @Override
    public Set characteristics()
        {
        return S_CHARACTERISTICS;
        }

    // ---- ExternalizableLite interface ------------------------------------

    @Override
    public void readExternal(DataInput in) throws IOException
        {
        m_mapper = ExternalizableHelper.readObject(in);
        }

    @Override
    public void writeExternal(DataOutput out) throws IOException
        {
        ExternalizableHelper.writeObject(out, m_mapper);
        }

    // ---- PortableObject interface ----------------------------------------

    @Override
    public void readExternal(PofReader in) throws IOException
        {
        m_mapper = in.readObject(0);
        }

    @Override
    public void writeExternal(PofWriter out) throws IOException
        {
        out.writeObject(0, m_mapper);
        }

    // ---- static members ----------------------------------------------------

    protected static final Set S_CHARACTERISTICS =
            Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));

    // ---- data members ----------------------------------------------------

    @JsonbProperty("mapper")
    protected ToDoubleFunction m_mapper;
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy