From 308cb80c292dc0122e598d00c4bfab5e55b9d84c Mon Sep 17 00:00:00 2001
From: Raphael Girardot <raphael.girardot@synchrotron-soleil.fr>
Date: Wed, 9 Oct 2013 12:28:01 +0000
Subject: [PATCH] Use Map instead of Collection for IView storage

---
 .../fr/soleil/docking/view/ViewFactory.java   | 70 +++++++++++++------
 1 file changed, 48 insertions(+), 22 deletions(-)

diff --git a/dockingcore/src/main/java/fr/soleil/docking/view/ViewFactory.java b/dockingcore/src/main/java/fr/soleil/docking/view/ViewFactory.java
index c7856bf..b6c063b 100644
--- a/dockingcore/src/main/java/fr/soleil/docking/view/ViewFactory.java
+++ b/dockingcore/src/main/java/fr/soleil/docking/view/ViewFactory.java
@@ -8,35 +8,36 @@ import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.prefs.Preferences;
 
 import javax.swing.Action;
 
 import fr.soleil.docking.action.ViewAction;
-import fr.soleil.lib.project.ObjectUtils;
 
+/**
+ * Basic implementation of {@link IViewFactory}
+ * 
+ * @author girardot
+ */
 public class ViewFactory implements IViewFactory {
 
-    protected final Collection<IView> views;
+    protected final Map<Object, IView> views;
     protected final PropertyChangeSupport support;
 
     public ViewFactory() {
         super();
-        views = Collections.newSetFromMap(new ConcurrentHashMap<IView, Boolean>());
+        views = new ConcurrentHashMap<Object, IView>();
         support = new PropertyChangeSupport(this);
     }
 
     @Override
     public IView getView(Object id) {
         IView result = null;
-        for (IView view : views) {
-            if (ObjectUtils.sameObject(id, view.getId())) {
-                result = view;
-                break;
-            }
+        if (id != null) {
+            result = views.get(id);
         }
         return result;
     }
@@ -74,19 +75,38 @@ public class ViewFactory implements IViewFactory {
 
     @Override
     public IView addView(IView view) {
-        if (views.add(view)) {
-            support.firePropertyChange(VIEWS, null, view);
-        } else {
-            view = null;
+        IView added = null;
+        if (view != null) {
+            Object id = view.getId();
+            if (id != null) {
+                if (!views.containsKey(id)) {
+                    views.put(id, view);
+                    if (views.get(id) == view) {
+                        added = view;
+                    }
+                }
+            }
         }
-        return view;
+        if (added != null) {
+            synchronized (support) {
+                support.firePropertyChange(VIEWS, null, view);
+            }
+        }
+        return added;
     }
 
     @Override
     public IView removeView(Object id) {
-        IView toRemove = getView(id);
-        if (!removeView(toRemove)) {
+        IView toRemove;
+        if (id == null) {
             toRemove = null;
+        } else {
+            toRemove = views.remove(id);
+            if (toRemove != null) {
+                synchronized (support) {
+                    support.firePropertyChange(VIEWS, toRemove, null);
+                }
+            }
         }
         return toRemove;
     }
@@ -97,9 +117,13 @@ public class ViewFactory implements IViewFactory {
         if (view == null) {
             result = false;
         } else {
-            result = views.remove(view);
-            if (result) {
-                support.firePropertyChange(VIEWS, view, null);
+            Object id = view.getId();
+            IView temp = getView(id);
+            if (temp == view) {
+                result = true;
+                removeView(id);
+            } else {
+                result = false;
             }
         }
         return result;
@@ -108,8 +132,10 @@ public class ViewFactory implements IViewFactory {
     @Override
     public List<Action> getActionList() {
         List<Action> result = new ArrayList<Action>(views.size());
-        for (IView view : views) {
-            result.add(new ViewAction(view));
+        for (IView view : views.values()) {
+            if (view != null) {
+                result.add(new ViewAction(view));
+            }
         }
         return result;
     }
@@ -121,7 +147,7 @@ public class ViewFactory implements IViewFactory {
 
     @Override
     public Collection<IView> getViews() {
-        return views;
+        return views.values();
     }
 
     @Override
-- 
GitLab