Skip to content

Commit

Permalink
Add injectIntoKeyValue to objectPrimitiveMap. Closes #1217.
Browse files Browse the repository at this point in the history
Signed-off-by: Emilie Robichaud <emilie.robichaud@bnymellon.com>
  • Loading branch information
emilie-robichaud committed Apr 19, 2022
1 parent 36ab4c8 commit 413450d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
@@ -0,0 +1,30 @@
import "copyrightAndOthers.stg"

targetPath() ::= "org/eclipse/collections/api/block/function/primitive"

fileName(primitive) ::= "ObjectObject<primitive.name>ToObjectFunction"

class(primitive) ::= <<
<body(primitive.type, primitive.name, primitive.wrapperName)>
>>

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

package org.eclipse.collections.api.block.function.primitive;

import java.io.Serializable;

/**
* This file was automatically generated from template file objectObjectPrimitiveToObjectFunction.stg.
*
* @since 11.1.
*/
@FunctionalInterface
public interface ObjectObject<name>ToObjectFunction\<T1, T2, R>
extends Serializable
{
R valueOf(T1 object1, T2 object2, <type> value);
}

>>
Expand Up @@ -22,6 +22,7 @@ import org.eclipse.collections.api.block.predicate.primitive.Object<name>Predica
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.primitive.Object<name>Procedure;
import org.eclipse.collections.api.block.procedure.primitive.<name>Procedure;
import org.eclipse.collections.api.block.function.primitive.ObjectObject<name>ToObjectFunction;
import org.eclipse.collections.api.collection.primitive.Mutable<name>Collection;
import org.eclipse.collections.api.tuple.primitive.Object<name>Pair;

Expand Down Expand Up @@ -93,6 +94,20 @@ public interface Object<name>Map\<K> extends <name>Iterable
void forEachKeyValue(Object<name>Procedure\<? super K> procedure);
<if(!primitive.booleanPrimitive)><(flipUniqueValues.(name))(name)><endif>

/**
* Implements the injectInto pattern with each key/value pair of the map.
* @param value to be injected into the map
* @param function to apply to the injected value and key/value pairs
* @return result of injecting the injectedValue into the map
* @since 11.1
*/
default \<IV> IV injectIntoKeyValue(IV injectedValue, ObjectObject<name>ToObjectFunction\<? super IV, ? super K, ? extends IV> function)
{
IV[] result = (IV[]) new Object[]{injectedValue};
this.forEachKeyValue((key, value) -> result[0] = function.valueOf(result[0], key, value));
return result[0];
}

/**
* Return a copy of this map containing only the key/value pairs that match the predicate.
* @param predicate the predicate to determine which key/value pairs in this map should be
Expand Down
@@ -1,4 +1,4 @@
import "copyright.stg"
import "copyrightAndOthers.stg"

skipBoolean() ::= "true"

Expand All @@ -11,7 +11,7 @@ class(primitive) ::= <<
>>

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

package org.eclipse.collections.api.map.primitive;

Expand Down
@@ -1,4 +1,4 @@
import "copyright.stg"
import "copyrightAndOthers.stg"

skipBooleanKeys() ::= "true"

Expand All @@ -13,7 +13,7 @@ class(primitive1, primitive2, sameTwoPrimitives) ::= <<
>>

body(type1, type2, name1, name2) ::= <<
<copyright()>
<copyrightAndOthers()>

package org.eclipse.collections.api.map.primitive;

Expand Down
@@ -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.primitive;

Expand All @@ -27,8 +27,9 @@ import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFuncti
import org.eclipse.collections.api.iterator.<name>Iterator;
import org.eclipse.collections.api.list.primitive.Mutable<name>List;
import org.eclipse.collections.api.map.primitive.ImmutableObject<name>Map;
import org.eclipse.collections.api.map.primitive.Object<name>Map;
import org.eclipse.collections.api.set.primitive.Mutable<name>Set;
import org.eclipse.collections.api.map.primitive.MutableObject<name>Map;
import org.eclipse.collections.api.map.primitive.Object<name>Map;
import org.eclipse.collections.impl.bag.mutable.primitive.<name>HashBag;
import org.eclipse.collections.impl.factory.primitive.<name>Lists;
import org.eclipse.collections.impl.factory.primitive.Object<name>Maps;
Expand Down Expand Up @@ -334,6 +335,18 @@ public abstract class AbstractObject<name>MapTestCase
Assert.assertEquals(<(literal.(type))("9")>, sumValue[0]<delta.(type)>);
}

@Test
public void injectIntoKeyValue()
{
Object<name>Map\<String> map0 = this.newWithKeysValues("2", <(literal.(type))("3")>, "4", <(literal.(type))("5")>);

String result0 = map0.injectIntoKeyValue(new String("1"), (result, eachKey, eachValue) -> result + eachKey + String.valueOf(eachValue));
Assert.assertTrue(result0, "12<(toStringLiteral.(type))("3")>4<(toStringLiteral.(type))("5")>".equals(result0) || "14<(toStringLiteral.(type))("5")>2<(toStringLiteral.(type))("3")>".equals(result0));

Object<name>Map copy = map0.injectIntoKeyValue(Object<name>Maps.mutable.empty(), MutableObject<name>Map::withKeyValue);
Assert.assertEquals(map0, copy);
}

@Test
public void makeString()
{
Expand Down
@@ -1,4 +1,4 @@
import "copyright.stg"
import "copyrightAndOthers.stg"
import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"
Expand All @@ -18,7 +18,7 @@ class(primitive1, primitive2, sameTwoPrimitives) ::= <<
>>

body(type1, type2, name1, name2) ::= <<
<copyright()>
<copyrightAndOthers()>

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

Expand Down

0 comments on commit 413450d

Please sign in to comment.