diff --git a/main/coreplugins/io.sarl.lang.core/src/io/sarl/lang/scoping/batch/SARLMapExtensions.java b/main/coreplugins/io.sarl.lang.core/src/io/sarl/lang/scoping/batch/SARLMapExtensions.java index 6909802c57..6b16a61c3b 100644 --- a/main/coreplugins/io.sarl.lang.core/src/io/sarl/lang/scoping/batch/SARLMapExtensions.java +++ b/main/coreplugins/io.sarl.lang.core/src/io/sarl/lang/scoping/batch/SARLMapExtensions.java @@ -148,6 +148,22 @@ public static V operator_remove(Map map, K key) { return map.remove(key); } + /** Remove the given pair into the map. + * + *

If the given key is inside the map, but is not mapped to the given value, the + * map will not be changed. + * + * @param - type of the map keys. + * @param - type of the map values. + * @param map - the map to update. + * @param entry - the entry (key, value) to remove from the map. + * @return {@code true} if the pair was removed. + */ + @Inline(value = "$1.remove($2.getKey(), $2.getValue())", statementExpression = true) + public static boolean operator_remove(Map map, Pair entry) { + return map.remove(entry.getKey(), entry.getValue()); + } + /** Remove pairs with the given keys from the map. * * @@ -162,6 +178,25 @@ public static void operator_remove(Map map, Iterable key } } + /** Remove the given pair from a given map for obtaining a new map. + * + *

The replied map is a view on the given map. It means that any change + * in the original map is reflected to the result of this operation. + * + * @param - type of the map keys. + * @param - type of the map values. + * @param left - the map to consider. + * @param right - the entry (key, value) to remove from the map. + * @return an immutable map with the content of the map and with the given entry. + * @throws IllegalArgumentException - when the right operand key exists in the left operand. + */ + @Pure + @Inline(value = "SARLMapExtensions.operator_minus($1, $2.getKey())", + imported = { SARLMapExtensions.class, Collections.class }) + public static Map operator_minus(Map left, final Pair right) { + return operator_minus(left, right.getKey()); + } + /** Replies the elements of the given map except the pair with the given key. * *

The replied map is a view on the given map. It means that any change @@ -200,7 +235,7 @@ public boolean apply(K input) { * @return the map with the content of the left map except the pairs of the right map. */ @Pure - @Inline(value = "SARLMapExtensions.operator_minus(left, right.keySet())", imported = SARLMapExtensions.class) + @Inline(value = "SARLMapExtensions.operator_minus($0, $1.keySet())", imported = SARLMapExtensions.class) public static Map operator_minus(Map left, Map right) { return operator_minus(left, right.keySet()); } diff --git a/tests/io.sarl.lang.core.tests/src/io/sarl/lang/core/tests/scoping/batch/SARLMapExtensionsTest.java b/tests/io.sarl.lang.core.tests/src/io/sarl/lang/core/tests/scoping/batch/SARLMapExtensionsTest.java index 237191b550..e0ee9fc56e 100644 --- a/tests/io.sarl.lang.core.tests/src/io/sarl/lang/core/tests/scoping/batch/SARLMapExtensionsTest.java +++ b/tests/io.sarl.lang.core.tests/src/io/sarl/lang/core/tests/scoping/batch/SARLMapExtensionsTest.java @@ -24,7 +24,7 @@ import static io.sarl.lang.scoping.batch.SARLMapExtensions.operator_minus; import static io.sarl.lang.scoping.batch.SARLMapExtensions.operator_plus; import static io.sarl.lang.scoping.batch.SARLMapExtensions.operator_remove; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; @@ -98,6 +98,42 @@ public void operator_addMapPair_1() { assertEquals(v, this.map.get("k2")); } + @Test + public void operator_removeMapPair_0() { + String v = UUID.randomUUID().toString(); + boolean o; + + o = operator_remove(this.map, new Pair("k3", v)); + assertFalse(o); + assertEquals(2, this.map.size()); + assertEquals(this.value1, this.map.get("k1")); + assertEquals(this.value2, this.map.get("k2")); + } + + @Test + public void operator_removeMapPair_1() { + String v = UUID.randomUUID().toString(); + boolean o; + + o = operator_remove(this.map, new Pair("k2", v)); + assertFalse(o); + assertEquals(2, this.map.size()); + assertEquals(this.value1, this.map.get("k1")); + assertEquals(this.value2, this.map.get("k2")); + } + + @Test + public void operator_removeMapPair_2() { + String v = UUID.randomUUID().toString(); + boolean o; + + o = operator_remove(this.map, new Pair("k2", new String(this.value2))); + assertTrue(o); + assertEquals(1, this.map.size()); + assertEquals(this.value1, this.map.get("k1")); + assertNull(this.map.get("k2")); + } + @Test public void operator_plusMapPair_0() { String v = UUID.randomUUID().toString(); @@ -131,6 +167,65 @@ public void operator_plusMapPair_1() { assertNull(o.get("k3")); } + @Test + public void operator_minusMapPair_0() { + String v = UUID.randomUUID().toString(); + Map o; + + o = operator_minus(this.map, new Pair("k3", v)); + + assertNotNull(o); + assertNotSame(this.map, o); + + assertEquals(2, this.map.size()); + assertEquals(this.value1, this.map.get("k1")); + assertEquals(this.value2, this.map.get("k2")); + assertNull(this.map.get("k3")); + + assertEquals(2, o.size()); + assertEquals(this.value1, o.get("k1")); + assertEquals(this.value2, o.get("k2")); + assertNull(o.get("k3")); + } + + @Test + public void operator_minusMapPair_1() { + String v = UUID.randomUUID().toString(); + Map o = operator_minus(this.map, new Pair("k2", v)); + + assertNotNull(o); + assertNotSame(this.map, o); + + assertEquals(2, this.map.size()); + assertEquals(this.value1, this.map.get("k1")); + assertEquals(this.value2, this.map.get("k2")); + assertNull(this.map.get("k3")); + + assertEquals(1, o.size()); + assertEquals(this.value1, o.get("k1")); + assertNull(o.get("k2")); + assertNull(o.get("k3")); + } + + @Test + public void operator_minusMapPair_3() { + String v = UUID.randomUUID().toString(); + Map o = operator_minus(this.map, new Pair("k2", new String(this.value2))); + + assertNotNull(o); + assertNotSame(this.map, o); + + assertEquals(2, this.map.size()); + assertEquals(this.value1, this.map.get("k1")); + assertEquals(this.value2, this.map.get("k2")); + assertNull(this.map.get("k3")); + + assertEquals(1, o.size()); + assertEquals(this.value1, o.get("k1")); + assertNull(o.get("k2")); + assertNull(o.get("k3")); + } + @Test public void operator_plusMapMap_0() { String v1 = UUID.randomUUID().toString(); diff --git a/tests/io.sarl.lang.tests/src/io/sarl/lang/tests/general/parsing/general/SARLMapExtensionsParsingTest.java b/tests/io.sarl.lang.tests/src/io/sarl/lang/tests/general/parsing/general/SARLMapExtensionsParsingTest.java index ed11dc3d7b..16b284eb0e 100644 --- a/tests/io.sarl.lang.tests/src/io/sarl/lang/tests/general/parsing/general/SARLMapExtensionsParsingTest.java +++ b/tests/io.sarl.lang.tests/src/io/sarl/lang/tests/general/parsing/general/SARLMapExtensionsParsingTest.java @@ -536,7 +536,7 @@ public void operator_removeMapK_1() throws Exception { validate(mas).assertError( XbasePackage.eINSTANCE.getXFeatureCall(), IssueCodes.INCOMPATIBLE_TYPES, - "Type mismatch: cannot convert from int to Iterable"); + "Type mismatch: cannot convert from int to Pair"); } @Test @@ -629,7 +629,7 @@ public void operator_minusMapK_2() throws Exception { validate(mas).assertError( XbasePackage.eINSTANCE.getXFeatureCall(), IssueCodes.INCOMPATIBLE_TYPES, - "Type mismatch: cannot convert from int to Map"); + "Type mismatch: cannot convert from int to Pair"); } @Test