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

jtulach pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans-html4j.git

commit af4a74110d2eda1b5c8be42ff250b43ba9cf5d97
Author: Jaroslav Tulach <[email protected]>
AuthorDate: Thu Feb 14 06:21:26 2019 +0100

    Encapsulate the multi-js-object operations into separate class
---
 .../main/java/org/netbeans/html/ko4j/Knockout.java | 17 +++---
 .../main/java/org/netbeans/html/ko4j/MapObjs.java  | 67 ++++++++++++++++++++++
 2 files changed, 77 insertions(+), 7 deletions(-)

diff --git a/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java 
b/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java
index cc41bb8..912d12d 100644
--- a/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java
+++ b/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java
@@ -61,7 +61,7 @@ final class Knockout  {
 
     private PropertyBinding[] props;
     private FunctionBinding[] funcs;
-    private final Map<Fn.Presenter,Object> objs = new HashMap<Fn.Presenter, 
Object>();
+    private Object objs;
     private final Object copyFrom;
     private final Object strong;
 
@@ -80,10 +80,10 @@ final class Knockout  {
 
     final Object js() {
         final Fn.Presenter c = Fn.activePresenter();
-        Object js = objs.get(c);
+        Object js = MapObjs.get(objs, c);
         if (js == null) {
             js = initObjs(c, copyFrom);
-            objs.put(c, js);
+            objs = MapObjs.put(objs, c, js);
         }
         return js;
     }
@@ -119,7 +119,9 @@ final class Knockout  {
             if (ko == null) {
                 return;
             }
-            Object js = ko.objs.remove(Fn.activePresenter());
+            Object[] both = MapObjs.remove(ko.objs, Fn.activePresenter());
+            Object js = both[0];
+            ko.objs = both[1];
             clean(js);
             ko.props = null;
             ko.funcs = null;
@@ -149,9 +151,10 @@ final class Knockout  {
     }
 
     final void valueHasMutated(final String propertyName, Object oldValue, 
Object newValue) {
-        for (Map.Entry<Fn.Presenter, Object> e : objs.entrySet()) {
-            Fn.Presenter p = e.getKey();
-            final Object o = e.getValue();
+        Object[] all = MapObjs.toArray(objs);
+        for (int i = 0; i < all.length; i += 2) {
+            Fn.Presenter p = (Fn.Presenter) all[i];
+            final Object o = all[i + 1];
             if (p != Fn.activePresenter()) {
                 if (p instanceof Executor) {
                     ((Executor) p).execute(new Runnable() {
diff --git a/ko4j/src/main/java/org/netbeans/html/ko4j/MapObjs.java 
b/ko4j/src/main/java/org/netbeans/html/ko4j/MapObjs.java
new file mode 100644
index 0000000..ef3ec28
--- /dev/null
+++ b/ko4j/src/main/java/org/netbeans/html/ko4j/MapObjs.java
@@ -0,0 +1,67 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.netbeans.html.ko4j;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.netbeans.html.boot.spi.Fn;
+
+final class MapObjs {
+    static Object put(Object now, Fn.Presenter key, Object js) {
+        Map<Fn.Presenter, Object> map = (Map<Fn.Presenter, Object>) now;
+        if (map == null) {
+            map = new HashMap<Fn.Presenter, Object>();
+        }
+        map.put(key, js);
+        return map;
+    }
+
+    static Object get(Object now, Fn.Presenter key) {
+        if (now instanceof Map) {
+            Map<?,?> map = (Map<?,?>) now;
+            return map.get(key);
+        }
+        return null;
+    }
+
+    static Object[] remove(Object now, Fn.Presenter key) {
+        Map<?,?> map = (Map<?,?>) now;
+        Object prev = map.remove(key);
+        return new Object[] { prev, map };
+    }
+
+    static Object[] toArray(Object now) {
+        if (now instanceof Map) {
+            Map<?, ?> map = (Map<?, ?>) now;
+            Object[] res = new Object[map.size() * 2];
+            int at = 0;
+            for (Map.Entry<? extends Object, ? extends Object> entry : 
map.entrySet()) {
+                Object key = entry.getKey();
+                Object value = entry.getValue();
+
+                res[at] = key;
+                res[at + 1] = value;
+                at += 2;
+            }
+            return res;
+        }
+        return new Object[0];
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to