Skip to content

Commit

Permalink
Add injectIntoKeyValue to primitiveObjectMap. Partially addresses #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 18, 2022
1 parent 601540c commit f8bd58f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
@@ -0,0 +1,30 @@
import "copyrightAndOthers.stg"

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

fileName(primitive) ::= "Object<primitive.name>ObjectToObjectFunction"

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 objectPrimitiveObjectToObjectFunction.stg.
*
* @since 11.1.
*/
@FunctionalInterface
public interface Object<name>ObjectToObjectFunction\<T1, T2, R>
extends Serializable
{
R valueOf(T1 object1, <type> value, T2 object2);
}

>>
Expand Up @@ -22,6 +22,7 @@ import org.eclipse.collections.api.block.predicate.primitive.<name>ObjectPredica
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.primitive.<name>ObjectProcedure;
import org.eclipse.collections.api.block.procedure.primitive.<name>Procedure;
import org.eclipse.collections.api.block.function.primitive.Object<name>ObjectToObjectFunction;
import org.eclipse.collections.api.set.primitive.Mutable<name>Set;
import org.eclipse.collections.api.tuple.primitive.<name>ObjectPair;

Expand Down Expand Up @@ -73,6 +74,20 @@ public interface <name>ObjectMap\<V> extends PrimitiveObjectMap\<V>
*/
void forEachKeyValue(<name>ObjectProcedure\<? super V> procedure);

/**
* 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, Object<name>ObjectToObjectFunction\<? super IV, ? super V, ? 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
Expand Up @@ -47,6 +47,7 @@ import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.primitive.Immutable<name>ObjectMap;
import org.eclipse.collections.api.map.primitive.Mutable<name>ObjectMap;
import org.eclipse.collections.api.map.primitive.<name>ObjectMap;
import org.eclipse.collections.api.map.primitive.ObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.ObjectLongMap;
Expand Down Expand Up @@ -1264,6 +1265,18 @@ public abstract class Abstract<name>ObjectMapTestCase
Assert.assertTrue(concat[3], "<(toStringLiteral.(type))("0")>zero<(toStringLiteral.(type))("5")>five".equals(concat[3]) || "<(toStringLiteral.(type))("5")>five<(toStringLiteral.(type))("0")>zero".equals(concat[3]));
}

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

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

<name>ObjectMap copy = map0.injectIntoKeyValue(<name>ObjectMaps.mutable.empty(), Mutable<name>ObjectMap::withKeyValue);
Assert.assertEquals(map0, copy);
}

@Test
public void size()
{
Expand Down

0 comments on commit f8bd58f

Please sign in to comment.