Skip to content

Commit

Permalink
Pull up implementations of aggregateBy() as default methods.
Browse files Browse the repository at this point in the history
Signed-off-by: Craig P. Motlin <cmotlin@gmail.com>
  • Loading branch information
motlin committed Jul 2, 2020
1 parent bfb3b76 commit 1e3713a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
package org.eclipse.collections.api.bag;

import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.primitive.ObjectIntToObjectFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.collection.ImmutableCollection;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.MutableMapIterable;
import org.eclipse.collections.api.multimap.bag.ImmutableBagIterableMultimap;
import org.eclipse.collections.api.partition.bag.PartitionImmutableBagIterable;
Expand Down Expand Up @@ -81,4 +86,22 @@ default ImmutableBagIterable<T> selectDuplicates()

@Override
<V> ImmutableCollection<V> collectWithOccurrences(ObjectIntToObjectFunction<? super T, ? extends V> function);

@Override
default <K, V> ImmutableMap<K, V> aggregateBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator)
{
MutableMap<K, V> result = Maps.mutable.empty();
this.forEachWithOccurrences((each, occurrences) ->
{
K key = groupBy.valueOf(each);
for (int i = 0; i < occurrences; i++)
{
result.updateValueWith(key, zeroValueFactory, nonMutatingAggregator, each);
}
});
return result.toImmutable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@

import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.primitive.ObjectIntToObjectFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.MutableMapIterable;
import org.eclipse.collections.api.multimap.bag.MutableBagIterableMultimap;
import org.eclipse.collections.api.partition.bag.PartitionMutableBagIterable;
Expand Down Expand Up @@ -143,4 +147,22 @@ default MutableBagIterable<T> withoutAll(Iterable<? extends T> elements)

@Override
<V> RichIterable<V> collectWithOccurrences(ObjectIntToObjectFunction<? super T, ? extends V> function);

@Override
default <K, V> MutableMap<K, V> aggregateBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator)
{
MutableMap<K, V> result = Maps.mutable.empty();
this.forEachWithOccurrences((each, occurrences) ->
{
K key = groupBy.valueOf(each);
for (int i = 0; i < occurrences; i++)
{
result.updateValueWith(key, zeroValueFactory, nonMutatingAggregator, each);
}
});
return result;
}
}

0 comments on commit 1e3713a

Please sign in to comment.