Skip to content

Commit

Permalink
Implement summarizeDouble, summarizeFloat, summarizeLong, summarizeIn…
Browse files Browse the repository at this point in the history
…t on Procedures2.

Signed-off-by: Donald Raab <Donald.Raab@bnymellon.com>
  • Loading branch information
donraab committed Jan 9, 2018
1 parent a87ac16 commit 6f4bfc6
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016 Goldman Sachs.
* Copyright (c) 2018 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand All @@ -11,8 +11,17 @@
package org.eclipse.collections.impl.block.factory;

import java.util.Collection;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.LongSummaryStatistics;

import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function3;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.impl.block.procedure.checked.CheckedProcedure2;
Expand Down Expand Up @@ -81,6 +90,42 @@ public static <T> Procedure2<T, Collection<T>> removeFromCollection()
return (Procedure2<T, Collection<T>>) REMOVE_FROM_COLLECTION;
}

/**
* Use with {@link org.eclipse.collections.api.RichIterable#aggregateInPlaceBy(Function, Function0, Procedure2)}
* @since 9.2.
*/
public static <T> Procedure2<DoubleSummaryStatistics, T> summarizeDouble(DoubleFunction<T> function)
{
return (DoubleSummaryStatistics dss, T value) -> dss.accept(function.doubleValueOf(value));
}

/**
* Use with {@link org.eclipse.collections.api.RichIterable#aggregateInPlaceBy(Function, Function0, Procedure2)}
* @since 9.2.
*/
public static <T> Procedure2<DoubleSummaryStatistics, T> summarizeFloat(FloatFunction<T> function)
{
return (DoubleSummaryStatistics dss, T value) -> dss.accept((double) function.floatValueOf(value));
}

/**
* Use with {@link org.eclipse.collections.api.RichIterable#aggregateInPlaceBy(Function, Function0, Procedure2)}
* @since 9.2.
*/
public static <T> Procedure2<IntSummaryStatistics, T> summarizeInt(IntFunction<T> function)
{
return (IntSummaryStatistics iss, T value) -> iss.accept(function.intValueOf(value));
}

/**
* Use with {@link org.eclipse.collections.api.RichIterable#aggregateInPlaceBy(Function, Function0, Procedure2)}
* @since 9.2.
*/
public static <T> Procedure2<LongSummaryStatistics, T> summarizeLong(LongFunction<T> function)
{
return (LongSummaryStatistics lss, T value) -> lss.accept(function.longValueOf(value));
}

private static final class ProcedureAdapter<T, P> implements Procedure2<T, P>
{
private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016 Goldman Sachs.
* Copyright (c) 2018 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand All @@ -11,9 +11,15 @@
package org.eclipse.collections.impl.block.procedure;

import java.io.IOException;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.LongSummaryStatistics;

import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.impl.block.factory.Procedures2;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.test.Verify;
import org.junit.Assert;
Expand Down Expand Up @@ -79,4 +85,68 @@ private static class MyRuntimeException extends RuntimeException
super(message, cause);
}
}

/**
* @since 9.2.
*/
@Test
public void summarizeDouble()
{
MutableList<Double> list = Lists.mutable.with(2.0, 2.0, 2.0, 3.0, 3.0, 3.0);
MutableMap<String, DoubleSummaryStatistics> map = list.aggregateInPlaceBy(
Object::toString,
DoubleSummaryStatistics::new,
Procedures2.summarizeDouble(Double::doubleValue));
Verify.assertSize(2, map);
Assert.assertEquals(6.0, map.get("2.0").getSum(), 0.0);
Assert.assertEquals(9.0, map.get("3.0").getSum(), 0.0);
}

/**
* @since 9.2.
*/
@Test
public void summarizeFloat()
{
MutableList<Float> list = Lists.mutable.with(2.0f, 2.0f, 2.0f, 3.0f, 3.0f, 3.0f);
MutableMap<String, DoubleSummaryStatistics> map = list.aggregateInPlaceBy(
Object::toString,
DoubleSummaryStatistics::new,
Procedures2.summarizeDouble(Float::floatValue));
Verify.assertSize(2, map);
Assert.assertEquals(6.0, map.get("2.0").getSum(), 0.0);
Assert.assertEquals(9.0, map.get("3.0").getSum(), 0.0);
}

/**
* @since 9.2.
*/
@Test
public void summarizeLong()
{
MutableList<Long> list = Lists.mutable.with(2L, 2L, 2L, 3L, 3L, 3L);
MutableMap<String, LongSummaryStatistics> map = list.aggregateInPlaceBy(
Object::toString,
LongSummaryStatistics::new,
Procedures2.summarizeLong(Long::longValue));
Verify.assertSize(2, map);
Assert.assertEquals(6, map.get("2").getSum());
Assert.assertEquals(9, map.get("3").getSum());
}

/**
* @since 9.2.
*/
@Test
public void summarizeInt()
{
MutableList<Integer> list = Lists.mutable.with(2, 2, 2, 3, 3, 3);
MutableMap<String, IntSummaryStatistics> map = list.aggregateInPlaceBy(
Object::toString,
IntSummaryStatistics::new,
Procedures2.summarizeInt(Integer::intValue));
Verify.assertSize(2, map);
Assert.assertEquals(6, map.get("2").getSum());
Assert.assertEquals(9, map.get("3").getSum());
}
}

0 comments on commit 6f4bfc6

Please sign in to comment.