Skip to content

Commit

Permalink
Optimize implementations of aggregateBy in Bags to use forEachWithOcc…
Browse files Browse the repository at this point in the history
…urrences.Closes #449

Signed-off-by: Sirisha Pratha <sirisha.pratha@bnymellon.com>
  • Loading branch information
prathasirisha committed Jul 2, 2020
1 parent 7c997c0 commit 518e8e5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Goldman Sachs and others.
* Copyright (c) 2020 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 Down Expand Up @@ -67,7 +67,8 @@ public <K, V> ImmutableMap<K, V> aggregateBy(
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator)
{
MutableMap<K, V> map = UnifiedMap.newMap();
this.forEach(new NonMutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, nonMutatingAggregator));
NonMutatingAggregationProcedure<T, K, V> procedure = new NonMutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, nonMutatingAggregator);
this.forEachWithOccurrences(procedure::repeatForOccurrences);
return map.toImmutable();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Goldman Sachs and others.
* Copyright (c) 2020 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 Down Expand Up @@ -303,7 +303,8 @@ public <K, V> MutableMap<K, V> aggregateBy(
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator)
{
MutableMap<K, V> map = UnifiedMap.newMap();
this.forEach(new NonMutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, nonMutatingAggregator));
NonMutatingAggregationProcedure<T, K, V> procedure = new NonMutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, nonMutatingAggregator);
this.forEachWithOccurrences(procedure::repeatForOccurrences);
return map;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Goldman Sachs.
* Copyright (c) 2020 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 Down Expand Up @@ -47,4 +47,13 @@ public void value(T each)
K key = this.groupBy.valueOf(each);
this.map.updateValueWith(key, this.zeroValueFactory, this.nonMutatingAggregator, each);
}

public void repeatForOccurrences(T each, int occurrences)
{
K key = this.groupBy.valueOf(each);
for (int i = 0; i < occurrences; i++)
{
this.map.updateValueWith(key, this.zeroValueFactory, this.nonMutatingAggregator, each);
}
}
}

0 comments on commit 518e8e5

Please sign in to comment.