Skip to content

Commit

Permalink
Override Java 8 default method Map#forEach
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Zheng <robin.zheng@bnymellon.com>
  • Loading branch information
rzrobin213 committed Jan 11, 2023
1 parent 7f5c20a commit 4e18b60
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiConsumer;

import org.eclipse.collections.api.block.procedure.Procedure;

Expand Down Expand Up @@ -43,4 +44,10 @@ default ConcurrentMutableMap<K, V> withMapIterable(MapIterable<? extends K, ? ex
this.putAllMapIterable(mapIterable);
return this;
}

@Override
default void forEach(BiConsumer<? super K, ? super V> action)
{
MutableMap.super.forEach(action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
package org.eclipse.collections.api.map;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BiConsumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

Expand Down Expand Up @@ -349,4 +351,10 @@ default <K1, V1, V2> MapIterable<K1, V2> aggregateBy(
valueFunction.valueOf(value)));
return map;
}

default void forEach(BiConsumer<? super K, ? super V> action)
{
Objects.requireNonNull(action);
this.forEachKeyValue(action::accept);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;

import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.block.function.Function;
Expand Down Expand Up @@ -433,4 +434,10 @@ default <K1, V1, V2> MutableMapIterable<K1, V2> aggregateBy(
valueFunction.valueOf(value)));
return map;
}

@Override
default void forEach(BiConsumer<? super K, ? super V> action)
{
MapIterable.super.forEach(action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.ImmutableBag;
Expand Down Expand Up @@ -500,4 +501,10 @@ protected Object writeReplace()
return new ImmutableBiMapSerializationProxy<>(this);
}
}

@Override
public void forEach(BiConsumer<? super K, ? super V> action)
{
super.forEach(action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;

import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.MutableBag;
Expand Down Expand Up @@ -508,4 +509,10 @@ public <V1> ImmutableBag<V1> countByEach(Function<? super V, ? extends Iterable<
{
return this.countByEach(function, Bags.mutable.empty()).toImmutable();
}

@Override
public void forEach(BiConsumer<? super K, ? super V> action)
{
super.forEach(action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.SortedMap;
import java.util.function.BiConsumer;

import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.block.function.Function;
Expand Down Expand Up @@ -596,4 +597,10 @@ public <V1> ImmutableBag<V1> countByEach(Function<? super V, ? extends Iterable<
{
return this.countByEach(function, Bags.mutable.empty()).toImmutable();
}

@Override
public void forEach(BiConsumer<? super K, ? super V> action)
{
super.forEach(action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,18 @@ default void BiMap_toList()
target,
iterable.toList());
}

@Override
default void MapIterable_forEach()
{
BiMap<Object, Integer> bimap = this.newWith(3, 2, 1);
MutableCollection<Integer> forEach = this.newMutableForFilter();
bimap.forEach((key, value) -> forEach.add(value + 10));
assertEquals(this.newMutableForFilter(13, 13, 13, 12, 12, 11), forEach);

BiMap<Integer, String> bimap2 = this.newWithKeysValues(3, "Three", 2, "Two", 1, "One");
MutableCollection<String> forEach2 = this.newMutableForFilter();
bimap2.forEach((key, value) -> forEach2.add(key + value));
assertEquals(this.newMutableForFilter("3Three", "2Two", "1One"), forEach2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,18 @@ default void MapIterable_containsValue()
assertTrue(map2.containsValue(null));
}
}

@Test
default void MapIterable_forEach()
{
MapIterable<Object, Integer> map = this.newWith(3, 3, 3, 2, 2, 1);
MutableCollection<Integer> forEach = this.newMutableForFilter();
map.forEach((key, value) -> forEach.add(value + 10));
assertEquals(this.newMutableForFilter(13, 13, 13, 12, 12, 11), forEach);

MapIterable<Integer, String> map2 = this.newWithKeysValues(3, "Three", 2, "Two", 1, "Three");
MutableCollection<String> forEach2 = this.newMutableForFilter();
map2.forEach((key, value) -> forEach2.add(key + value));
assertEquals(this.newMutableForFilter("3Three", "2Two", "1Three"), forEach2);
}
}

0 comments on commit 4e18b60

Please sign in to comment.