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 May 17, 2020
1 parent 43aed07 commit 76199ce
Show file tree
Hide file tree
Showing 14 changed files with 294 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.eclipse.collections.api.collection.primitive.MutableLongCollection;
import org.eclipse.collections.api.collection.primitive.MutableShortCollection;
import org.eclipse.collections.api.factory.Bags;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.MutableMap;
Expand Down Expand Up @@ -2282,7 +2283,20 @@ <V, R extends MutableMapIterable<V, T>> R groupByUniqueKey(
*
* @since 3.0
*/
<K, V> MapIterable<K, V> aggregateInPlaceBy(Function<? super T, ? extends K> groupBy, Function0<? extends V> zeroValueFactory, Procedure2<? super V, ? super T> mutatingAggregator);
default <K, V> MapIterable<K, V> aggregateInPlaceBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Procedure2<? super V, ? super T> mutatingAggregator)
{
MutableMap<K, V> map = Maps.mutable.empty();
this.forEach(each ->
{
K key = groupBy.valueOf(each);
V value = map.getIfAbsentPut(key, zeroValueFactory);
mutatingAggregator.value(value, each);
});
return map;
}

/**
* Applies an aggregate function over the iterable grouping results into a map based on the specific groupBy function.
Expand All @@ -2291,7 +2305,19 @@ <V, R extends MutableMapIterable<V, T>> R groupByUniqueKey(
*
* @since 3.0
*/
<K, V> MapIterable<K, V> aggregateBy(Function<? super T, ? extends K> groupBy, Function0<? extends V> zeroValueFactory, Function2<? super V, ? super T, ? extends V> nonMutatingAggregator);
default <K, V> MapIterable<K, V> aggregateBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator)
{
MutableMap<K, V> map = Maps.mutable.empty();
this.forEach(each ->
{
K key = groupBy.valueOf(each);
map.updateValueWith(key, zeroValueFactory, nonMutatingAggregator, each);
});
return map;
}

/**
* Applies a groupBy function over the iterable, followed by a collect function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.ImmutableBagIterable;
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.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
Expand All @@ -28,7 +27,6 @@
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.block.procedure.Procedure2;
import org.eclipse.collections.api.factory.SortedSets;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.primitive.ImmutableBooleanList;
Expand All @@ -39,7 +37,6 @@
import org.eclipse.collections.api.list.primitive.ImmutableIntList;
import org.eclipse.collections.api.list.primitive.ImmutableLongList;
import org.eclipse.collections.api.list.primitive.ImmutableShortList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.sorted.MutableSortedMap;
import org.eclipse.collections.api.multimap.sortedbag.ImmutableSortedBagMultimap;
import org.eclipse.collections.api.partition.bag.sorted.PartitionImmutableSortedBag;
Expand Down Expand Up @@ -222,24 +219,6 @@ default <V> ImmutableBag<V> countByEach(Function<? super T, ? extends Iterable<V
@Override
<V> ImmutableSortedBagMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function);

/**
* Can return an ImmutableMap that's backed by a LinkedHashMap.
*/
@Override
<K, V> ImmutableMap<K, V> aggregateBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator);

/**
* Can return an ImmutableMap that's backed by a LinkedHashMap.
*/
@Override
<K, V> ImmutableMap<K, V> aggregateInPlaceBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Procedure2<? super V, ? super T> mutatingAggregator);

@Override
<S> ImmutableList<Pair<T, S>> zip(Iterable<S> that);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.MutableBagIterable;
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.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
Expand All @@ -28,7 +27,6 @@
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.block.procedure.Procedure2;
import org.eclipse.collections.api.factory.SortedSets;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.MutableBooleanList;
Expand All @@ -39,7 +37,6 @@
import org.eclipse.collections.api.list.primitive.MutableIntList;
import org.eclipse.collections.api.list.primitive.MutableLongList;
import org.eclipse.collections.api.list.primitive.MutableShortList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.sorted.MutableSortedMap;
import org.eclipse.collections.api.multimap.sortedbag.MutableSortedBagMultimap;
import org.eclipse.collections.api.partition.bag.sorted.PartitionMutableSortedBag;
Expand Down Expand Up @@ -255,24 +252,6 @@ default <V> MutableBag<V> countByEach(Function<? super T, ? extends Iterable<V>>
return this.asLazy().flatCollect(function).toBag();
}

/**
* Can return an MutableMap that's backed by a LinkedHashMap.
*/
@Override
<K, V> MutableMap<K, V> aggregateBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator);

/**
* Can return an MutableMap that's backed by a LinkedHashMap.
*/
@Override
<K, V> MutableMap<K, V> aggregateInPlaceBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Procedure2<? super V, ? super T> mutatingAggregator);

@Override
<S> MutableList<Pair<T, S>> zip(Iterable<S> that);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.eclipse.collections.api.bag.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.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
Expand All @@ -30,7 +29,6 @@
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.block.procedure.Procedure2;
import org.eclipse.collections.api.list.ListIterable;
import org.eclipse.collections.api.list.primitive.BooleanList;
import org.eclipse.collections.api.list.primitive.ByteList;
Expand All @@ -40,7 +38,6 @@
import org.eclipse.collections.api.list.primitive.IntList;
import org.eclipse.collections.api.list.primitive.LongList;
import org.eclipse.collections.api.list.primitive.ShortList;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.sorted.SortedMapIterable;
import org.eclipse.collections.api.multimap.sortedbag.SortedBagMultimap;
import org.eclipse.collections.api.ordered.ReversibleIterable;
Expand Down Expand Up @@ -207,24 +204,6 @@ default <P, V> ListIterable<V> flatCollectWith(Function2<? super T, ? super P, ?
@Override
<V> SortedBagMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function);

/**
* Can return an MapIterable that's backed by a LinkedHashMap.
*/
@Override
<K, V> MapIterable<K, V> aggregateBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator);

/**
* Can return an MapIterable that's backed by a LinkedHashMap.
*/
@Override
<K, V> MapIterable<K, V> aggregateInPlaceBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Procedure2<? super V, ? super T> mutatingAggregator);

/**
* Returns the comparator used to order the elements in this bag, or null if this bag uses the natural ordering of
* its elements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,35 @@ default <V> ImmutableMap<V, T> groupByUniqueKey(Function<? super T, ? extends V>
ImmutableCollection<Pair<T, Integer>> zipWithIndex();

@Override
<K, V> ImmutableMap<K, V> aggregateInPlaceBy(
default <K, V> ImmutableMap<K, V> aggregateInPlaceBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Procedure2<? super V, ? super T> mutatingAggregator);
Procedure2<? super V, ? super T> mutatingAggregator)
{
MutableMap<K, V> map = Maps.mutable.empty();
this.forEach(each ->
{
K key = groupBy.valueOf(each);
V value = map.getIfAbsentPut(key, zeroValueFactory);
mutatingAggregator.value(value, each);
});
return map.toImmutable();
}

@Override
<K, V> ImmutableMap<K, V> aggregateBy(
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);
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator)
{
MutableMap<K, V> map = Maps.mutable.empty();
this.forEach(each ->
{
K key = groupBy.valueOf(each);
map.updateValueWith(key, zeroValueFactory, nonMutatingAggregator, each);
});
return map.toImmutable();
}

/**
* @since 9.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,14 +564,33 @@ default <V> MutableMap<V, T> groupByUniqueKey(Function<? super T, ? extends V> f
boolean retainAllIterable(Iterable<?> iterable);

@Override
<K, V> MutableMap<K, V> aggregateInPlaceBy(
default <K, V> MutableMap<K, V> aggregateInPlaceBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Procedure2<? super V, ? super T> mutatingAggregator);
Procedure2<? super V, ? super T> mutatingAggregator)
{
MutableMap<K, V> map = Maps.mutable.empty();
this.forEach(each ->
{
K key = groupBy.valueOf(each);
V value = map.getIfAbsentPut(key, zeroValueFactory);
mutatingAggregator.value(value, each);
});
return map;
}

@Override
<K, V> MutableMap<K, V> aggregateBy(
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);
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator)
{
MutableMap<K, V> map = Maps.mutable.empty();
this.forEach(each ->
{
K key = groupBy.valueOf(each);
map.updateValueWith(key, zeroValueFactory, nonMutatingAggregator, each);
});
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,35 @@ default <V1> ImmutableMap<V1, V> groupByUniqueKey(Function<? super V, ? extends
}

@Override
<K2, V2> ImmutableMap<K2, V2> aggregateInPlaceBy(
Function<? super V, ? extends K2> groupBy,
Function0<? extends V2> zeroValueFactory,
Procedure2<? super V2, ? super V> mutatingAggregator);
default <KK, VV> ImmutableMap<KK, VV> aggregateInPlaceBy(
Function<? super V, ? extends KK> groupBy,
Function0<? extends VV> zeroValueFactory,
Procedure2<? super VV, ? super V> mutatingAggregator)
{
MutableMap<KK, VV> map = Maps.mutable.empty();
this.forEach(each ->
{
KK key = groupBy.valueOf(each);
VV value = map.getIfAbsentPut(key, zeroValueFactory);
mutatingAggregator.value(value, each);
});
return map.toImmutable();
}

@Override
<K2, V2> ImmutableMap<K2, V2> aggregateBy(
Function<? super V, ? extends K2> groupBy,
Function0<? extends V2> zeroValueFactory,
Function2<? super V2, ? super V, ? extends V2> nonMutatingAggregator);
default <KK, VV> ImmutableMap<KK, VV> aggregateBy(
Function<? super V, ? extends KK> groupBy,
Function0<? extends VV> zeroValueFactory,
Function2<? super VV, ? super V, ? extends VV> nonMutatingAggregator)
{
MutableMap<KK, VV> map = Maps.mutable.empty();
this.forEach(each ->
{
KK key = groupBy.valueOf(each);
map.updateValueWith(key, zeroValueFactory, nonMutatingAggregator, each);
});
return map.toImmutable();
}

@Override
ImmutableMap<V, K> flipUniqueValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.collection.ImmutableCollection;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.multimap.ImmutableMultimap;
import org.eclipse.collections.api.partition.PartitionImmutableCollection;
import org.eclipse.collections.api.tuple.Pair;
Expand Down Expand Up @@ -124,8 +125,33 @@ default <V1> ImmutableBag<V1> countByEach(Function<? super V, ? extends Iterable
ImmutableCollection<Pair<V, Integer>> zipWithIndex();

@Override
<KK, VV> ImmutableMapIterable<KK, VV> aggregateInPlaceBy(Function<? super V, ? extends KK> groupBy, Function0<? extends VV> zeroValueFactory, Procedure2<? super VV, ? super V> mutatingAggregator);
default <KK, VV> ImmutableMapIterable<KK, VV> aggregateInPlaceBy(
Function<? super V, ? extends KK> groupBy,
Function0<? extends VV> zeroValueFactory,
Procedure2<? super VV, ? super V> mutatingAggregator)
{
MutableMap<KK, VV> map = Maps.mutable.empty();
this.forEach(each ->
{
KK key = groupBy.valueOf(each);
VV value = map.getIfAbsentPut(key, zeroValueFactory);
mutatingAggregator.value(value, each);
});
return map.toImmutable();
}

@Override
<KK, VV> ImmutableMapIterable<KK, VV> aggregateBy(Function<? super V, ? extends KK> groupBy, Function0<? extends VV> zeroValueFactory, Function2<? super VV, ? super V, ? extends VV> nonMutatingAggregator);
default <KK, VV> ImmutableMapIterable<KK, VV> aggregateBy(
Function<? super V, ? extends KK> groupBy,
Function0<? extends VV> zeroValueFactory,
Function2<? super VV, ? super V, ? extends VV> nonMutatingAggregator)
{
MutableMap<KK, VV> map = Maps.mutable.empty();
this.forEach(each ->
{
KK key = groupBy.valueOf(each);
map.updateValueWith(key, zeroValueFactory, nonMutatingAggregator, each);
});
return map.toImmutable();
}
}
Loading

0 comments on commit 76199ce

Please sign in to comment.