Skip to content

Commit

Permalink
Merge pull request #1229 from goldbal330/master
Browse files Browse the repository at this point in the history
Allow primitive map put method to return the previous value. Closes #…
  • Loading branch information
donraab committed Mar 17, 2022
2 parents 208dfc9 + 7795c22 commit 9617846
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ public interface MutableObject<name>Map\<K> extends Object<name>Map\<K>
*/
<type> getIfAbsentPut(K key, <type> value);

/**
* Retrieves the value associated with the key if one exists; if it does not,
* associates a putValue with the key.
* @param key the key
* @param putValue the value to associate with {@code key} if no such mapping exists
* @param defaultValue the value to return if no mapping associated with {@code key} exists
* @return the value associated with key, if one exists, or {@code defaultValue} if not
* @since 11.1.
*/
default <type> getAndPut(K key, <type> putValue, <type> defaultValue)
{
if (this.containsKey(key))
{
<type> existingValue = this.get(key);
this.put(key, putValue);
return existingValue;
}
this.put(key, putValue);
return defaultValue;
}

/**
* Retrieves the value associated with the key if one exists; if it does not,
* invokes the supplier and associates the result with the key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,20 @@ public class Object<name>HashMap\<K> implements MutableObject<name>Map\<K>, Exte
return value;
}

@Override
public <type> getAndPut(K key, <type> putValue, <type> defaultValue)
{
int index = this.probe(key);
if (isNonSentinel(this.keys[index]) && nullSafeEquals(this.toNonSentinel(this.keys[index]), key))
{
<type> existingValue = this.values[index];
this.values[index] = putValue;
return existingValue;
}
this.addKeyValueAtIndex(key, putValue, index);
return defaultValue;
}

@Override
public <type> getIfAbsentPut(K key, <name>Function0 function)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,20 @@ public class Object<name>HashMapWithHashingStrategy\<K> implements MutableObject
return value;
}

@Override
public <type> getAndPut(K key, <type> putValue, <type> defaultValue)
{
int index = this.probe(key);
if (isNonSentinel(this.keys[index]) && nullSafeEquals(this.toNonSentinel(this.keys[index]), key))
{
<type> existingValue = this.values[index];
this.values[index] = putValue;
return existingValue;
}
this.addKeyValueAtIndex(key, putValue, index);
return defaultValue;
}

@Override
public <type> getIfAbsentPut(K key, <name>Function0 function)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "copyright.stg"
import "copyrightAndOthers.stg"
import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"
Expand All @@ -12,7 +12,7 @@ class(primitive) ::= <<
>>

body(type, name) ::= <<
<copyright()>
<copyrightAndOthers()>

package org.eclipse.collections.impl.map.mutable.primitive;

Expand Down Expand Up @@ -168,6 +168,15 @@ public class SynchronizedObject<name>Map\<K>
}
}

@Override
public <type> getAndPut(K key, <type> putValue, <type> defaultValue)
{
synchronized (this.lock)
{
return this.map.getAndPut(key, putValue, defaultValue);
}
}

@Override
public <type> getIfAbsentPut(K key, <name>Function0 function)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "copyright.stg"
import "copyrightAndOthers.stg"
import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"
Expand All @@ -12,7 +12,7 @@ class(primitive) ::= <<
>>

body(type, name) ::= <<
<copyright()>
<copyrightAndOthers()>

package org.eclipse.collections.impl.map.mutable.primitive;

Expand Down Expand Up @@ -86,6 +86,12 @@ public class UnmodifiableObject<name>Map\<K>
return result;
}

@Override
public <type> getAndPut(K key, <type> defaultValue, <type> putValue)
{
throw new UnsupportedOperationException("Cannot call getAndPut() on " + this.getClass().getSimpleName());
}

@Override
public void clear()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "copyright.stg"
import "copyrightAndOthers.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"
import "primitiveEquals.stg"
Expand All @@ -16,7 +16,7 @@ class(primitive) ::= <<
>>

body(type, name, wrapperName) ::= <<
<copyright()>
<copyrightAndOthers()>

package org.eclipse.collections.impl.map.mutable.primitive;

Expand Down Expand Up @@ -362,6 +362,21 @@ public abstract class AbstractMutableObject<name>MapTestCase extends AbstractObj
Assert.assertEquals(Object<name>HashMap.newWithKeysValues(null, <(literal.(type))("50")>), map3);
}

@Test
public void getAndPut()
{
MutableObject<name>Map\<Integer> map1 = this.getEmptyMap();
Assert.assertEquals(<(literal.(type))("20")>, map1.getAndPut(Integer.valueOf(5), <(literal.(type))("100")>, <(literal.(type))("20")>)<delta.(type)>);
Assert.assertEquals(<(literal.(type))("100")>, map1.get(Integer.valueOf(5))<delta.(type)>);
Assert.assertEquals(<(literal.(type))("100")>, map1.getAndPut(Integer.valueOf(5), <(literal.(type))("70")>, <(literal.(type))("50")>)<delta.(type)>);
Assert.assertEquals(<(literal.(type))("70")>, map1.get(Integer.valueOf(5))<delta.(type)>);
Assert.assertEquals(<(literal.(type))("70")>, map1.getAndPut(Integer.valueOf(5), <(literal.(type))("77")>, <(literal.(type))("50")>)<delta.(type)>);
Assert.assertEquals(<(literal.(type))("77")>, map1.get(Integer.valueOf(5))<delta.(type)>);
map1.removeKey(Integer.valueOf(5));
Assert.assertEquals(<(literal.(type))("20")>, map1.getAndPut(Integer.valueOf(5), <(literal.(type))("100")>, <(literal.(type))("20")>)<delta.(type)>);
Assert.assertEquals(<(literal.(type))("100")>, map1.get(Integer.valueOf(5))<delta.(type)>);
}

@Test
public void getIfAbsentPut_Function()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "copyright.stg"
import "copyrightAndOthers.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"
import "primitiveEquals.stg"
Expand All @@ -16,7 +16,7 @@ class(primitive) ::= <<
>>

body(type, name, wrapperName) ::= <<
<copyright()>
<copyrightAndOthers()>

package org.eclipse.collections.impl.map.mutable.primitive;

Expand Down Expand Up @@ -195,6 +195,13 @@ public class UnmodifiableObject<name>MapTest extends AbstractMutableObject<name>
this.map.getIfAbsentPut("10", <(literal.(type))("100")>);
}

@Override
@Test(expected = UnsupportedOperationException.class)
public void getAndPut()
{
this.map.getAndPut("10", <(literal.(type))("200")>, <(literal.(type))("100")>);
}

@Override
@Test
public void getIfAbsentPut_Function()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs.
* Copyright (c) 2022 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 @@ -727,6 +727,20 @@ public boolean getIfAbsentPut(K key, boolean value)
return value;
}

@Override
public boolean getAndPut(K key, boolean putValue, boolean defaultValue)
{
int index = this.probe(key);
if (ObjectBooleanHashMap.isNonSentinel(this.keys[index]) && ObjectBooleanHashMap.nullSafeEquals(this.toNonSentinel(this.keys[index]), key))
{
boolean existingValue = this.values.get(index);
this.values.set(index, putValue);
return existingValue;
}
this.addKeyValueAtIndex(key, putValue, index);
return defaultValue;
}

@Override
public boolean getIfAbsentPut(K key, BooleanFunction0 function)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs.
* Copyright (c) 2022 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 @@ -740,6 +740,20 @@ public boolean getIfAbsentPut(K key, boolean value)
return value;
}

@Override
public boolean getAndPut(K key, boolean putValue, boolean defaultValue)
{
int index = this.probe(key);
if (ObjectBooleanHashMapWithHashingStrategy.isNonSentinel(this.keys[index]) && this.nullSafeEquals(this.toNonSentinel(this.keys[index]), key))
{
boolean existingValue = this.values.get(index);
this.values.set(index, putValue);
return existingValue;
}
this.addKeyValueAtIndex(key, putValue, index);
return defaultValue;
}

@Override
public boolean getIfAbsentPut(K key, BooleanFunction0 function)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs.
* Copyright (c) 2022 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 @@ -97,6 +97,17 @@ public void getOrThrow()
Assert.assertFalse(map1.getOrThrow(null));
}

@Test
public void getAndPut()
{
MutableObjectBooleanMap<Integer> map1 = this.getEmptyMap();
Assert.assertTrue(map1.getAndPut(Integer.valueOf(1), false, true));
Assert.assertFalse(map1.getAndPut(Integer.valueOf(2), false, false));
Assert.assertFalse(map1.getAndPut(Integer.valueOf(1), true, true));
map1.remove(Integer.valueOf(1));
Assert.assertFalse(map1.getAndPut(Integer.valueOf(1), true, false));
}

@Override
public void containsKey()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs.
* Copyright (c) 2022 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 @@ -81,6 +81,13 @@ public void put()
this.map.put("0", true);
}

@Override
@Test(expected = UnsupportedOperationException.class)
public void getAndPut()
{
this.map.getAndPut("0", true, false);
}

@Override
@Test(expected = UnsupportedOperationException.class)
public void withKeysValues()
Expand Down

0 comments on commit 9617846

Please sign in to comment.