Skip to content

Commit

Permalink
Pull the implementation of MutableMapIterable.removeIf() up as a defa…
Browse files Browse the repository at this point in the history
…ult method.

Signed-off-by: Craig P. Motlin <cmotlin@gmail.com>
  • Loading branch information
motlin committed Jan 15, 2020
1 parent 7ce7a19 commit 4d37ad2
Show file tree
Hide file tree
Showing 10 changed files with 4 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ default boolean removeAllKeys(Set<? extends K> keys)
* @return true if any entry is removed.
* @since 10.0
*/
boolean removeIf(Predicate2<? super K, ? super V> predicate);
default boolean removeIf(Predicate2<? super K, ? super V> predicate)
{
return this.entrySet().removeIf(entry -> predicate.accept(entry.getKey(), entry.getValue()));
}

/**
* Get and return the value in the Map at the specified key. Alternatively, if there is no value in the map at the key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,22 +301,6 @@ public V removeKey(K key)
return this.remove(key);
}

@Override
public boolean removeIf(Predicate2<? super K, ? super V> predicate)
{
int previousSize = this.size();
Iterator<Entry<K, V>> iterator = this.entrySet().iterator();
while (iterator.hasNext())
{
Entry<K, V> entry = iterator.next();
if (predicate.accept(entry.getKey(), entry.getValue()))
{
iterator.remove();
}
}
return previousSize > this.size();
}

@Override
public void clear()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.Function3;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
Expand Down Expand Up @@ -2081,22 +2080,6 @@ public V removeKey(K key)
return this.remove(key);
}

@Override
public boolean removeIf(Predicate2<? super K, ? super V> predicate)
{
int previousSize = this.size();
Iterator<Map.Entry<K, V>> iterator = this.entrySet().iterator();
while (iterator.hasNext())
{
Map.Entry<K, V> entry = iterator.next();
if (predicate.accept(entry.getKey(), entry.getValue()))
{
iterator.remove();
}
}
return previousSize > this.size();
}

@Override
public <P> V getIfAbsentPutWith(K key, Function<? super P, ? extends V> function, P parameter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.Function3;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
Expand Down Expand Up @@ -2198,22 +2197,6 @@ public V removeKey(K key)
return this.remove(key);
}

@Override
public boolean removeIf(Predicate2<? super K, ? super V> predicate)
{
int previousSize = this.size();
Iterator<Map.Entry<K, V>> iterator = this.entrySet().iterator();
while (iterator.hasNext())
{
Map.Entry<K, V> entry = iterator.next();
if (predicate.accept(entry.getKey(), entry.getValue()))
{
iterator.remove();
}
}
return previousSize > this.size();
}

@Override
public <P> V getIfAbsentPutWith(K key, Function<? super P, ? extends V> function, P parameter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
Expand Down Expand Up @@ -253,22 +252,6 @@ public V removeKey(K key)
return this.delegate.remove(key);
}

@Override
public boolean removeIf(Predicate2<? super K, ? super V> predicate)
{
int previousSize = this.size();
Iterator<Entry<K, V>> iterator = this.entrySet().iterator();
while (iterator.hasNext())
{
Entry<K, V> entry = iterator.next();
if (predicate.accept(entry.getKey(), entry.getValue()))
{
iterator.remove();
}
}
return previousSize > this.size();
}

@Override
public boolean containsKey(Object key)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Set;

import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
Expand Down Expand Up @@ -164,22 +163,6 @@ public V removeKey(K key)
return this.delegate.remove(key);
}

@Override
public boolean removeIf(Predicate2<? super K, ? super V> predicate)
{
int previousSize = this.size();
Iterator<Entry<K, V>> iterator = this.entrySet().iterator();
while (iterator.hasNext())
{
Entry<K, V> entry = iterator.next();
if (predicate.accept(entry.getKey(), entry.getValue()))
{
iterator.remove();
}
}
return previousSize > this.size();
}

@Override
public boolean containsKey(Object key)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,6 @@ public V removeKey(K key)
return this.delegate.remove(key);
}

@Override
public boolean removeIf(Predicate2<? super K, ? super V> predicate)
{
int previousSize = this.size();
Iterator<Entry<K, V>> iterator = this.entrySet().iterator();
while (iterator.hasNext())
{
Entry<K, V> entry = iterator.next();
if (predicate.accept(entry.getKey(), entry.getValue()))
{
iterator.remove();
}
}
return previousSize > this.size();
}

@Override
public String toString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.SortedMap;

import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.list.MutableList;
Expand Down Expand Up @@ -196,22 +195,6 @@ public V removeKey(K key)
return this.delegate.remove(key);
}

@Override
public boolean removeIf(Predicate2<? super K, ? super V> predicate)
{
int previousSize = this.size();
Iterator<Entry<K, V>> iterator = this.entrySet().iterator();
while (iterator.hasNext())
{
Entry<K, V> entry = iterator.next();
if (predicate.accept(entry.getKey(), entry.getValue()))
{
iterator.remove();
}
}
return previousSize > this.size();
}

@Override
public MutableSortedMap<K, V> with(Pair<K, V>... pairs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.TreeMap;

import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.list.MutableList;
Expand Down Expand Up @@ -217,22 +216,6 @@ public V removeKey(K key)
return this.treeMap.remove(key);
}

@Override
public boolean removeIf(Predicate2<? super K, ? super V> predicate)
{
int previousSize = this.size();
Iterator<Entry<K, V>> iterator = this.entrySet().iterator();
while (iterator.hasNext())
{
Entry<K, V> entry = iterator.next();
if (predicate.accept(entry.getKey(), entry.getValue()))
{
iterator.remove();
}
}
return previousSize > this.size();
}

@Override
public TreeSortedMap<K, V> clone()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
Expand Down Expand Up @@ -1079,22 +1078,6 @@ public V removeKey(K key)
return this.remove(key);
}

@Override
public boolean removeIf(Predicate2<? super K, ? super V> predicate)
{
int previousSize = this.size();
Iterator<Entry<K, V>> iterator = this.entrySet().iterator();
while (iterator.hasNext())
{
Entry<K, V> entry = iterator.next();
if (predicate.accept(entry.getKey(), entry.getValue()))
{
iterator.remove();
}
}
return previousSize > this.size();
}

private void chainedForEachEntry(Object[] chain, Procedure2<? super K, ? super V> procedure)
{
for (int i = 0; i < chain.length; i += 2)
Expand Down

0 comments on commit 4d37ad2

Please sign in to comment.