com.tangosol.util.aggregator.CompositeAggregator 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.aggregator;
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.ClassHelper;
import com.tangosol.util.ExternalizableHelper;
import com.tangosol.util.ImmutableArrayList;
import com.tangosol.util.InvocableMap;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import javax.json.bind.annotation.JsonbProperty;
/**
* CompositeAggregator provides an ability to execute a collection of
* aggregators against the same subset of the entries in an InvocableMap,
* resulting in a list of corresponding aggregation results. The size of the
* returned list will always be equal to the length of the aggregators' array.
*
* @author gg 2006.02.08
* @since Coherence 3.2
*/
public class CompositeAggregator
extends ExternalizableHelper
implements InvocableMap.StreamingAggregator,
ExternalizableLite, PortableObject
{
// ----- constructors ---------------------------------------------------
/**
* Default constructor (necessary for the ExternalizableLite interface).
*/
public CompositeAggregator()
{
}
/**
* Construct a CompositeAggregator based on a specified EntryAggregator
* array.
*
* @param aAggregator an array of EntryAggregator objects; may not be
* null
*/
public CompositeAggregator(InvocableMap.EntryAggregator[] aAggregator)
{
Objects.requireNonNull(aAggregator);
m_aAggregator = aAggregator;
}
// ----- EntryAggregator interface --------------------------------------
/**
* Process a set of InvocableMap Entry objects using each of the
* underlying agregators in order to produce an array of aggregated
* results.
*
* @param setEntries a Set of read-only InvocableMap Entry objects to
* aggregate
*
* @return a List of aggregated results from processing the entries by the
* corresponding underlying aggregators
*/
public List aggregate(Set setEntries)
{
InvocableMap.EntryAggregator[] aAggregator = m_aAggregator;
int cAggregators = aAggregator.length;
Object[] aoResult = new Object[cAggregators];
for (int i = 0; i < cAggregators; i++)
{
aoResult[i] = aAggregator[i].aggregate(setEntries);
}
return new ImmutableArrayList(aoResult);
}
// ----- StreamingAggregator interface ----------------------------------
@Override
public InvocableMap.StreamingAggregator supply()
{
ensureInitialized();
if (m_fStreaming)
{
InvocableMap.StreamingAggregator[] aCopy =
Arrays.stream(m_aAggregator)
.map(aggr -> ((InvocableMap.StreamingAggregator) aggr).supply())
.toArray(InvocableMap.StreamingAggregator[]::new);
return createInstance(aCopy);
}
else
{
return createInstance(m_aAggregator);
}
}
@Override
public boolean accumulate(InvocableMap.Entry entry)
{
ensureInitialized();
if (m_fStreaming)
{
InvocableMap.EntryAggregator[] aAggregator = m_aAggregator;
for (int i = 0; i < aAggregator.length; i++)
{
((InvocableMap.StreamingAggregator) aAggregator[i]).accumulate(entry);
}
}
else
{
m_setEntries.add(entry);
}
return true;
}
@Override
public boolean combine(Object oResultPart)
{
ensureInitialized();
InvocableMap.EntryAggregator[] aAggregator = m_aAggregator;
int cAggregators = aAggregator.length;
if (m_fStreaming)
{
List