com.tangosol.internal.net.management.AbstractMultiCollector 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, 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.net.management;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
/**
* An abstract class for {@link Collector collecting} map-style data structures;
* specifically with key-value pairs in which the values need to be aggregated
* independently.
*
* @author hr/sr 11.3.17
* @since 12.2.1.4.0
*/
public abstract class AbstractMultiCollector>
implements Collector
{
// ----- Collector interface --------------------------------------------
@Override
public Supplier supplier()
{
return () -> (T) new HashMap();
}
@Override
public BinaryOperator combiner()
{
return (mapResult1, mapResult2) ->
{
int cMap2 = mapResult2.size();
if (cMap2 > mapResult1.size()) // iterate the larger map
{
cMap2 = mapResult1.size();
Map mapTmp = mapResult1;
mapResult1 = mapResult2;
mapResult2 = (T) mapTmp;
}
Map mapResult = new HashMap<>(mapResult1.size());
for (Map.Entry entry : mapResult1.entrySet())
{
String sAttribute = entry.getKey();
Object oValue2 = null;
if (mapResult2.containsKey(sAttribute))
{
oValue2 = mapResult2.get(sAttribute);
cMap2--;
}
mapResult.put(sAttribute, ((Collector) f_mapCollector.get(sAttribute)).combiner().apply(
entry.getValue(), oValue2));
}
if (cMap2 > 0)
{
for (Map.Entry entry : mapResult2.entrySet())
{
if (!mapResult1.containsKey(entry.getKey()))
{
String sAttribute = entry.getKey();
mapResult.put(sAttribute, ((Collector) f_mapCollector.get(sAttribute)).combiner().apply(
null, entry.getValue()));
}
}
}
return (T) mapResult;
};
}
@Override
public Function finisher()
{
return (mapResult) ->
{
for (Iterator> iter = mapResult.entrySet().iterator(); iter.hasNext(); )
{
Map.Entry entry = iter.next();
String sAttribute = entry.getKey();
Object oResult = ((Collector)(f_mapCollector.get(sAttribute))).finisher().apply(entry.getValue());
oResult = ATTRIBUTE_NULLIFIER.apply(oResult);
// TODO: validate that type specific summary stats are serializable
// oResult = ATTRIBUTE_SERIALIZABLE.apply(oResult);
if (oResult == null)
{
iter.remove();
}
else
{
entry.setValue(oResult);
}
}
return mapResult;
};
}
@Override
public Set characteristics()
{
return null;
}
// ----- constants ------------------------------------------------------
/**
* A Function that returns the provided object or null if the object
* is one of the 'no value' defaults.
*/
public static final Function