Skip to content

Commit

Permalink
[lang] Add operator "-=' and '-' on Map and Pair.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Apr 22, 2017
1 parent a72c967 commit 1a436c2
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 4 deletions.
Expand Up @@ -148,6 +148,22 @@ public static <K, V> V operator_remove(Map<K, V> map, K key) {
return map.remove(key);
}

/** Remove the given pair into the map.
*
* <p>If the given key is inside the map, but is not mapped to the given value, the
* map will not be changed.
*
* @param <K> - type of the map keys.
* @param <V> - 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 <K, V> boolean operator_remove(Map<K, V> map, Pair<? extends K, ? extends V> entry) {
return map.remove(entry.getKey(), entry.getValue());
}

/** Remove pairs with the given keys from the map.
*
*
Expand All @@ -162,6 +178,25 @@ public static <K, V> void operator_remove(Map<K, V> map, Iterable<? super K> key
}
}

/** Remove the given pair from a given map for obtaining a new map.
*
* <p>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 <K> - type of the map keys.
* @param <V> - 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 <K, V> Map<K, V> operator_minus(Map<K, V> left, final Pair<? extends K, ? extends V> right) {
return operator_minus(left, right.getKey());
}

/** Replies the elements of the given map except the pair with the given key.
*
* <p>The replied map is a view on the given map. It means that any change
Expand Down Expand Up @@ -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 <K, V> Map<K, V> operator_minus(Map<K, V> left, Map<? extends K, ? extends V> right) {
return operator_minus(left, right.keySet());
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String>("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<String, String>("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<String, String>("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();
Expand Down Expand Up @@ -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<String, String> o;

o = operator_minus(this.map, new Pair<String, String>("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<String, String> o = operator_minus(this.map, new Pair<String, String>("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<String, String> o = operator_minus(this.map, new Pair<String, String>("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();
Expand Down
Expand Up @@ -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<? super String>");
"Type mismatch: cannot convert from int to Pair");
}

@Test
Expand Down Expand Up @@ -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<? extends String, ? extends Integer>");
"Type mismatch: cannot convert from int to Pair");
}

@Test
Expand Down

0 comments on commit 1a436c2

Please sign in to comment.