You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/01/06 07:27:39 UTC

[isis] branch master updated: ISIS-2158: minor: extract map element toggling algorithm for reuse

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new f7e0c1c  ISIS-2158: minor: extract map element toggling algorithm for reuse
f7e0c1c is described below

commit f7e0c1c35619e45d96ddde204da3251dc4dab95b
Author: Andi Huber <ah...@apache.org>
AuthorDate: Mon Jan 6 08:27:28 2020 +0100

    ISIS-2158: minor: extract map element toggling algorithm for reuse
---
 .../isis/commons/internal/collections/_Maps.java   | 25 +++++++++++++++++++++-
 .../wicket/model/models/EntityCollectionModel.java |  4 +---
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/collections/_Maps.java b/core/commons/src/main/java/org/apache/isis/commons/internal/collections/_Maps.java
index 75aa3e2..c4b6cc9 100644
--- a/core/commons/src/main/java/org/apache/isis/commons/internal/collections/_Maps.java
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/collections/_Maps.java
@@ -42,6 +42,7 @@ import org.apache.isis.commons.internal.exceptions._Exceptions;
 
 import static org.apache.isis.commons.internal.base._With.requires;
 
+import lombok.NonNull;
 import lombok.Value;
 import lombok.val;
 
@@ -142,8 +143,30 @@ public final class _Maps {
         return new AbstractMap.SimpleEntry<K, V>(k, v);
     }
 
-    // -- TRANSFORMATIONS
+    // -- MODIFICATIONS
 
+    /**
+     * For a given {@code map} either adds or removes the specified {@code key} based on whether 
+     * the map contains the {@code key}.
+     * If this is an add operation, then given {@code value} is associated with the {@code key}. 
+     * @param <K>
+     * @param <V>
+     * @param input
+     * @param key
+     * @param value
+     * @return whether given map contains the {@code key} after the operation
+     */
+    public static <K, V> boolean toggleElement(
+            @NonNull Map<K, V> map, 
+            @NonNull K key, 
+            @NonNull V value) {
+        
+        val newValue = map.compute(key, (k, v) -> (v==null) ? value : null);
+        return newValue!=null;
+    }
+    
+    // -- TRANSFORMATIONS
+    
     public static <K, V> Map<K, V> filterKeys(
             @Nullable Map<K, V> input,
             Predicate<K> keyFilter, 
diff --git a/core/viewers/wicket/model/src/main/java/org/apache/isis/viewer/wicket/model/models/EntityCollectionModel.java b/core/viewers/wicket/model/src/main/java/org/apache/isis/viewer/wicket/model/models/EntityCollectionModel.java
index 1b998af..248afa1 100644
--- a/core/viewers/wicket/model/src/main/java/org/apache/isis/viewer/wicket/model/models/EntityCollectionModel.java
+++ b/core/viewers/wicket/model/src/main/java/org/apache/isis/viewer/wicket/model/models/EntityCollectionModel.java
@@ -517,10 +517,8 @@ implements LinksProvider, UiHintContainer {
         //XXX lombok issue, cannot use val here
         final ObjectMemento selectedAsMemento = super.getMementoService().mementoForObject(selectedAdapter);
         final String selectedKey = selectedAsMemento.asString(); 
-        final ObjectMemento newValue = 
-                toggledMementos.compute(selectedKey, (k, v) -> (v==null) ? selectedAsMemento : null);
         
-        final boolean isSelected = newValue!=null;
+        final boolean isSelected = _Maps.toggleElement(toggledMementos, selectedKey, selectedAsMemento); 
         return isSelected;
         
     }