Skip to content

Commit

Permalink
add removeIf to primitiveObject maps
Browse files Browse the repository at this point in the history
  • Loading branch information
mohrezaei committed Jan 1, 2023
1 parent 57baed9 commit babd789
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ public interface Mutable<name>ObjectMap\<V> extends <name>ObjectMap\<V>, Mutable
* @return a synchronized view of this map
*/
Mutable<name>ObjectMap\<V> asSynchronized();

/**
* Remove an entry from the map if the {@code predicate} evaluates to true.
*
* @param predicate should return true if the key/value pair should be removed.
* @return true if any entry is removed.
* @since 12.0
*/
boolean removeIf(<name>ObjectPredicate\<? super V> predicate);
}

>>
Original file line number Diff line number Diff line change
Expand Up @@ -2673,6 +2673,49 @@ public class <name>ObjectHashMap\<V> implements Mutable<name>ObjectMap\<V>, Exte
return <name>ObjectMaps.immutable.withAll(this);
}

@Override
public boolean removeIf(<name>ObjectPredicate\<? super V> predicate)
{
boolean result = false;
if (this.sentinelValues != null && this.sentinelValues.containsZeroKey
&& predicate.accept(EMPTY_KEY, this.sentinelValues.zeroValue))
{
if (this.sentinelValues.containsOneKey)
{
this.sentinelValues.containsZeroKey = false;
this.sentinelValues.zeroValue = null;
}
else
{
this.sentinelValues = null;
}
result = true;
}
if (this.sentinelValues != null && this.sentinelValues.containsOneKey
&& predicate.accept(REMOVED_KEY, this.sentinelValues.oneValue))
{
if (this.sentinelValues.containsZeroKey)
{
this.sentinelValues.containsOneKey = false;
this.sentinelValues.oneValue = null;
}
else
{
this.sentinelValues = null;
}
result = true;
}
for (int i = 0; i \< this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]) && predicate.accept(this.keys[i], this.values[i]))
{
this.removeKeyAtIndex(i);
result = true;
}
}
return result;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ public class Synchronized<name>ObjectMap\<V>
}
}

@Override
public boolean removeIf(<name>ObjectPredicate\<? super V> predicate)
{
synchronized (this.lock)
{
return this.map.removeIf(predicate);
}
}

@Override
public V get(<type> key)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ public class Unmodifiable<name>ObjectMap\<V>
throw new UnsupportedOperationException("Cannot call updateValueWith() on " + this.getClass().getSimpleName());
}

@Override
public boolean removeIf(<name>ObjectPredicate\<? super V> predicate)
{
throw new UnsupportedOperationException("Cannot call removeIf() on " + this.getClass().getSimpleName());
}

@Override
public V get(<type> key)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,23 @@ public abstract class AbstractMutable<name>ObjectMapTestCase extends Abstract<na
Assert.assertEquals(<name>ObjectHashMap.newWithKeysValues(<(literal.(type))("1")>, "<(toStringLiteral.(type))("1")>"), emptyMap1);
}

@Test
public void removeIf()
{
this.map.removeIf((k, v) -> true);
Assert.assertTrue(this.map.isEmpty());

Mutable<name>ObjectMap\<String> testMap = this.classUnderTest();
testMap.put(<(literal.(type))("1")>, "one");
testMap.removeIf((k, v) -> true);
Assert.assertTrue(testMap.isEmpty());

testMap = this.classUnderTest();
testMap.put(<(literal.(type))("1")>, "one");
testMap.removeIf((k, v) -> k % 2 == 0);
Assert.assertEquals(2, testMap.size());
}

@Test
public void updateValue()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ public class Unmodifiable<name>ObjectMapTest extends AbstractMutable<name>Object
this.map.getIfAbsentPutWithKey(<(literal.(type))("1")>, toString);
}

@Override
@Test(expected = UnsupportedOperationException.class)
public void removeIf()
{
super.removeIf();
}

@Override
@Test
public void freeze()
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@
<show>public</show>
<detectJavaApiLink>true</detectJavaApiLink>
<links>
<link>http://junit.sourceforge.net/javadoc/</link>
<link>https://junit.org/junit4/javadoc/latest</link>
<link>https://docs.oracle.com/javase/8/docs/api/</link>
</links>
<destDir>${project.version}</destDir>
Expand Down Expand Up @@ -631,7 +631,7 @@
<show>public</show>
<detectJavaApiLink>true</detectJavaApiLink>
<links>
<link>http://junit.sourceforge.net/javadoc/</link>
<link>https://junit.org/junit4/javadoc/latest</link>
<link>https://docs.oracle.com/javase/8/docs/api/</link>
</links>
<destDir>${project.version}</destDir>
Expand Down

0 comments on commit babd789

Please sign in to comment.