sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 03/03: Replace Map.get/put/replace(…) by Map.compute(…) operations.
Date Thu, 28 Jan 2021 15:11:13 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit adde8f486564b9431769f51d48fa8a04a8ba1472
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Thu Jan 28 15:49:56 2021 +0100

    Replace Map.get/put/replace(…) by Map.compute(…) operations.
---
 .../java/org/apache/sis/portrayal/Observable.java  | 47 ++++++----------------
 1 file changed, 13 insertions(+), 34 deletions(-)

diff --git a/core/sis-portrayal/src/main/java/org/apache/sis/portrayal/Observable.java b/core/sis-portrayal/src/main/java/org/apache/sis/portrayal/Observable.java
index e68e4af..5fc33b6 100644
--- a/core/sis-portrayal/src/main/java/org/apache/sis/portrayal/Observable.java
+++ b/core/sis-portrayal/src/main/java/org/apache/sis/portrayal/Observable.java
@@ -16,12 +16,10 @@
  */
 package org.apache.sis.portrayal;
 
+import java.util.Map;
+import java.util.HashMap;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
-import java.util.Arrays;
-import java.util.ConcurrentModificationException;
-import java.util.HashMap;
-import java.util.Map;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.ArraysExt;
 
@@ -72,22 +70,9 @@ abstract class Observable {
         if (listeners == null) {
             listeners = new HashMap<>(4);       // Assume few properties will be listened.
         }
-        final PropertyChangeListener[] oldList = listeners.get(propertyName);
-        final PropertyChangeListener[] newList;
-        final boolean success;
-        if (oldList != null) {
-            final int n = oldList.length;
-            newList = Arrays.copyOf(oldList, n+1);
-            newList[n] = listener;
-            success = listeners.replace(propertyName, oldList, newList);
-        } else {
-            newList = new PropertyChangeListener[] {listener};
-            success = (listeners.putIfAbsent(propertyName, newList) == null);
-        }
-        if (!success) {
-            // Opportunistic safety against some multi-threading misuse.
-            throw new ConcurrentModificationException();
-        }
+        listeners.compute(propertyName, (key, oldList) ->
+                (oldList != null) ? ArraysExt.append(oldList, listener)
+                                  : new PropertyChangeListener[] {listener});
     }
 
     /**
@@ -104,23 +89,18 @@ abstract class Observable {
         ArgumentChecks.ensureNonEmpty("propertyName", propertyName);
         ArgumentChecks.ensureNonNull("listener", listener);
         if (listeners != null) {
-            final PropertyChangeListener[] oldList = listeners.get(propertyName);
-            if (oldList != null) {
+            listeners.computeIfPresent(propertyName, (key, oldList) -> {
                 for (int i=oldList.length; --i >= 0;) {
                     if (oldList[i] == listener) {
                         if (oldList.length != 1) {
-                            final PropertyChangeListener[] newList = ArraysExt.remove(oldList,
i, 1);
-                            if (listeners.replace(propertyName, oldList, newList)) {
-                                return;
-                            }
-                        } else if (listeners.remove(propertyName, oldList)) {
-                            return;
+                            return ArraysExt.remove(oldList, i, 1);
+                        } else {
+                            return null;
                         }
-                        // Opportunistic safety against some multi-threading misuse.
-                        throw new ConcurrentModificationException();
                     }
                 }
-            }
+                return oldList;
+            });
         }
     }
 
@@ -150,17 +130,16 @@ abstract class Observable {
     }
 
     /**
-     * Notifies all registered listeners that a property of the given name changed its value.
+     * Notifies all registered listeners that a property changed its value.
      * It is caller responsibility to verify that the event source and property name are
valid.
      *
-     * @param  event the event to forward, can not be null.
+     * @param  event  the event to forward. Can not be null.
      *
      * @see PropertyChangeEvent
      * @see PropertyChangeListener
      */
     protected void firePropertyChange(final PropertyChangeEvent event) {
         ArgumentChecks.ensureNonNull("event", event);
-
         if (listeners != null) {
             final PropertyChangeListener[] list = listeners.get(event.getPropertyName());
             if (list != null) {


Mime
View raw message