]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/hints/HintContext.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / hints / HintContext.java
index c9946d0a2d67d67f621698a2e393987ff84f99f6..e5633b05539f201ad08720922310037893de88a7 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2015 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *     Semantum Oy - added getHintsUnsafe\r
- *******************************************************************************/\r
-/*\r
- *\r
- * @author Toni Kalajainen\r
- */\r
-package org.simantics.utils.datastructures.hints;\r
-\r
-import gnu.trove.map.hash.THashMap;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.Map.Entry;\r
-\r
-/**\r
- * \r
- * @author Toni Kalajainen\r
- */\r
-public class HintContext extends AbstractHintObservable implements IHintContext, Cloneable {\r
-\r
-    protected Map<Key, Object> hints = new THashMap<Key, Object>();\r
-\r
-    @Override\r
-    public void clearWithoutNotification() {\r
-        synchronized (this) {\r
-            hints.clear();\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public synchronized boolean containsHint(Key key) {\r
-        return hints.get(key) != null;\r
-    }\r
-\r
-    @SuppressWarnings("unchecked")\r
-    @Override\r
-    public <E> E getHint(Key key) {\r
-        if (key == null)\r
-            throw new IllegalArgumentException("key is null");\r
-        synchronized (this) {\r
-            return (E) hints.get(key);\r
-        }\r
-    }\r
-\r
-    @SuppressWarnings("unchecked")\r
-    @Override\r
-    public <E> E removeHint(Key key) {\r
-        if (key == null)\r
-            throw new IllegalArgumentException("key is null");\r
-\r
-        Runnable notification;\r
-        Object oldValue = null;\r
-        synchronized(this) {\r
-            oldValue = hints.remove(key);\r
-            if (oldValue==null) return null;\r
-            notification = createFireKeyRemovedRunnable(this, key, oldValue);\r
-        }\r
-        notification.run();\r
-        return (E) oldValue;\r
-    }\r
-\r
-    /**\r
-     * Set a set of hints\r
-     * @param hints\r
-     */\r
-    public void removeHints(Collection<? extends Key> keys) {\r
-        List<Runnable> notifications = new ArrayList<Runnable>(hints.size());\r
-        synchronized (this) {\r
-            // Remove first\r
-            for (Key key : keys) {\r
-                Object oldValue = this.hints.remove(key);\r
-                if (oldValue == null)\r
-                    continue;\r
-                Runnable notification = createFireKeyRemovedRunnable(this, key, oldValue);\r
-                notifications.add( notification );\r
-            }\r
-        }\r
-\r
-        // Notify then\r
-        for (Runnable r : notifications)\r
-            r.run();\r
-    }\r
-\r
-    @Override\r
-    public void setHint(Key key, Object value) {\r
-        if (key == null)\r
-            throw new IllegalArgumentException("key is null");\r
-        if (value == null)\r
-            throw new IllegalArgumentException("value is null");\r
-        if (!key.isValueAccepted(value))\r
-            throw new RuntimeException("Value \""+value+"\" is not accepted with key "+key.getClass().getName());\r
-\r
-        Runnable notification;\r
-        synchronized(this) {\r
-            Object oldValue = hints.put(key, value);\r
-            notification = createFireKeyChangedRunnable(this, key, oldValue, value);\r
-        }\r
-        notification.run();\r
-    }\r
-\r
-    /**\r
-     * Set a set of hints\r
-     * @param hints\r
-     */\r
-    @Override\r
-    public void setHints(Map<Key, Object> hints) {\r
-        List<Runnable> notifications = new ArrayList<Runnable>(hints.size());\r
-        synchronized (this) {\r
-            // Insert first\r
-            for (Entry<Key, Object> e : hints.entrySet()) {\r
-                Key key = e.getKey();\r
-                Object value = e.getValue();\r
-                if (value == null)\r
-                    throw new IllegalArgumentException("a value is null for key " + e.getKey());\r
-                Object oldValue = this.hints.put(key, value);\r
-\r
-                Runnable notification = createFireKeyChangedRunnable(this, key,\r
-                        oldValue, value);\r
-                notifications.add( notification );\r
-            }\r
-        }\r
-\r
-        // Notify then\r
-        for (Runnable r : notifications)\r
-            r.run();\r
-    }\r
-\r
-    public Object setHintWithoutNotification(Key key, Object value) {\r
-        if (key == null)\r
-            throw new IllegalArgumentException("key is null");\r
-        if (value == null)\r
-            throw new IllegalArgumentException("value is null");\r
-        if (!key.isValueAccepted(value))\r
-            throw new RuntimeException("Value \""+value+"\" is not accepted with key "+key.getClass().getName());\r
-\r
-        synchronized(this) {\r
-            return hints.put(key, value);\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Removes the specified hint without sending notifications about changes.\r
-     * \r
-     * @param <E>\r
-     * @param key\r
-     * @return removed hint value\r
-     */\r
-    @SuppressWarnings("unchecked")\r
-    public <E> E removeHintWithoutNotification(Key key) {\r
-        if (key == null)\r
-            throw new IllegalArgumentException("key is null");\r
-\r
-        Object oldValue = null;\r
-        synchronized(this) {\r
-            oldValue = hints.remove(key);\r
-        }\r
-        return (E) oldValue;\r
-    }\r
-\r
-    /**\r
-     * Replace the current hints with the specified set of hints. Hints\r
-     * that were previously included but are not contained by the new map will\r
-     * not be preserved by this operation.\r
-     * \r
-     * @param hints the new hints to set\r
-     */\r
-    public void replaceHintsWithoutNotification(Map<Key, Object> hints) {\r
-        Map<Key, Object> copy = new HashMap<Key, Object>(hints);\r
-        synchronized (this) {\r
-            this.hints = copy;\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Replace the current set of hints with the new specified set of hints.\r
-     * Notifications are sent for removed and changed hints.\r
-     * \r
-     * @param hints\r
-     */\r
-    public void replaceHints(Map<Key, Object> newHints) {\r
-        List<Runnable> notifications = new ArrayList<Runnable>(Math.max(this.hints.size(), newHints.size()));\r
-        synchronized (this) {\r
-            // Calculate removed keys\r
-            Set<Key> removedKeys = new HashSet<Key>(this.hints.keySet());\r
-            removedKeys.removeAll(newHints.keySet());\r
-\r
-            // Remove keys\r
-            for (Key key : removedKeys) {\r
-                Object oldValue = this.hints.remove(key);\r
-                if (oldValue == null)\r
-                    continue;\r
-\r
-                Runnable notification = createFireKeyRemovedRunnable(this, key, oldValue);\r
-                notifications.add( notification );\r
-            }\r
-\r
-            // Replace/set existing/new hints\r
-            for (Entry<Key, Object> e : newHints.entrySet()) {\r
-                Key key = e.getKey();\r
-                Object value = e.getValue();\r
-                if (value == null)\r
-                    throw new IllegalArgumentException("a value is null for key " + e.getKey());\r
-                Object oldValue = this.hints.put(key, value);\r
-                if (value.equals(oldValue))\r
-                    continue;\r
-\r
-                Runnable notification = createFireKeyChangedRunnable(this, key,\r
-                        oldValue, value);\r
-                notifications.add( notification );\r
-            }\r
-        }\r
-\r
-        // Notify then\r
-        for (Runnable r : notifications)\r
-            r.run();\r
-    }\r
-\r
-    /**\r
-     * Set a set of hints without notifying any generic hint or key-specific\r
-     * listeners. This method will only replace the possible previous values of\r
-     * the hints included in the specified map. Hints not included in the map\r
-     * will be preserved as such.\r
-     * \r
-     * @param hints the new hints to set\r
-     */\r
-    public void setHintsWithoutNotification(Map<Key, Object> hints) {\r
-        synchronized (this) {\r
-            for (Entry<Key, Object> e : hints.entrySet()) {\r
-                Key key = e.getKey();\r
-                Object value = e.getValue();\r
-                if (value == null)\r
-                    throw new IllegalArgumentException("a value is null for key " + e.getKey());\r
-                this.hints.put(key, value);\r
-            }\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Compares two object for equality.\r
-     * <p>\r
-     * Some times it is annoying to compare two objects if their\r
-     * value may be null.\r
-     * \r
-     * @param o1 obj1\r
-     * @param o2 obj2\r
-     * @return true if equal or both null\r
-     */\r
-    public static boolean objectEquals(Object o1, Object o2) {\r
-        if (o1==o2) return true;\r
-        if (o1==null && o2==null) return true;\r
-        if (o1==null || o2==null) return false;\r
-        return o1.equals(o2);\r
-    }\r
-\r
-    @Override\r
-    public synchronized Map<Key, Object> getHints() {\r
-        return new HashMap<Key, Object>(hints);\r
-    }\r
-\r
-    @Override\r
-    public Map<Key, Object> getHintsUnsafe() {\r
-        return hints;\r
-    }\r
-\r
-    @SuppressWarnings("unchecked")\r
-    @Override\r
-    public synchronized <E extends Key> Map<E, Object> getHintsOfClass(Class<E> clazz) {\r
-        Map<E, Object> result = new HashMap<E, Object>();\r
-        for (Entry<Key, Object> e : hints.entrySet()) {\r
-            Key key = e.getKey();\r
-            if (clazz.isAssignableFrom(key.getClass()))\r
-                result.put((E)key, e.getValue());\r
-        }\r
-        return result;\r
-    }\r
-\r
-    public synchronized int size()\r
-    {\r
-        return hints.size();\r
-    }\r
-\r
-    @Override\r
-    public Object clone() {\r
-        try {\r
-            return super.clone();\r
-        } catch (CloneNotSupportedException e) {\r
-            throw new Error(e);\r
-        }\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2015 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - added getHintsUnsafe
+ *******************************************************************************/
+/*
+ *
+ * @author Toni Kalajainen
+ */
+package org.simantics.utils.datastructures.hints;
+
+import gnu.trove.map.hash.THashMap;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+/**
+ * 
+ * @author Toni Kalajainen
+ */
+public class HintContext extends AbstractHintObservable implements IHintContext, Cloneable {
+
+    protected Map<Key, Object> hints = new THashMap<Key, Object>();
+
+    @Override
+    public void clearWithoutNotification() {
+        synchronized (this) {
+            hints.clear();
+        }
+    }
+
+    @Override
+    public synchronized boolean containsHint(Key key) {
+        return hints.get(key) != null;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public <E> E getHint(Key key) {
+        if (key == null)
+            throw new IllegalArgumentException("key is null");
+        synchronized (this) {
+            return (E) hints.get(key);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public <E> E removeHint(Key key) {
+        if (key == null)
+            throw new IllegalArgumentException("key is null");
+
+        Runnable notification;
+        Object oldValue = null;
+        synchronized(this) {
+            oldValue = hints.remove(key);
+            if (oldValue==null) return null;
+            notification = createFireKeyRemovedRunnable(this, key, oldValue);
+        }
+        notification.run();
+        return (E) oldValue;
+    }
+
+    /**
+     * Set a set of hints
+     * @param hints
+     */
+    public void removeHints(Collection<? extends Key> keys) {
+        List<Runnable> notifications = new ArrayList<Runnable>(hints.size());
+        synchronized (this) {
+            // Remove first
+            for (Key key : keys) {
+                Object oldValue = this.hints.remove(key);
+                if (oldValue == null)
+                    continue;
+                Runnable notification = createFireKeyRemovedRunnable(this, key, oldValue);
+                notifications.add( notification );
+            }
+        }
+
+        // Notify then
+        for (Runnable r : notifications)
+            r.run();
+    }
+
+    @Override
+    public void setHint(Key key, Object value) {
+        if (key == null)
+            throw new IllegalArgumentException("key is null");
+        if (value == null)
+            throw new IllegalArgumentException("value is null");
+        if (!key.isValueAccepted(value))
+            throw new RuntimeException("Value \""+value+"\" is not accepted with key "+key.getClass().getName());
+
+        Runnable notification;
+        synchronized(this) {
+            Object oldValue = hints.put(key, value);
+            notification = createFireKeyChangedRunnable(this, key, oldValue, value);
+        }
+        notification.run();
+    }
+
+    /**
+     * Set a set of hints
+     * @param hints
+     */
+    @Override
+    public void setHints(Map<Key, Object> hints) {
+        List<Runnable> notifications = new ArrayList<Runnable>(hints.size());
+        synchronized (this) {
+            // Insert first
+            for (Entry<Key, Object> e : hints.entrySet()) {
+                Key key = e.getKey();
+                Object value = e.getValue();
+                if (value == null)
+                    throw new IllegalArgumentException("a value is null for key " + e.getKey());
+                Object oldValue = this.hints.put(key, value);
+
+                Runnable notification = createFireKeyChangedRunnable(this, key,
+                        oldValue, value);
+                notifications.add( notification );
+            }
+        }
+
+        // Notify then
+        for (Runnable r : notifications)
+            r.run();
+    }
+
+    public Object setHintWithoutNotification(Key key, Object value) {
+        if (key == null)
+            throw new IllegalArgumentException("key is null");
+        if (value == null)
+            throw new IllegalArgumentException("value is null");
+        if (!key.isValueAccepted(value))
+            throw new RuntimeException("Value \""+value+"\" is not accepted with key "+key.getClass().getName());
+
+        synchronized(this) {
+            return hints.put(key, value);
+        }
+    }
+
+    /**
+     * Removes the specified hint without sending notifications about changes.
+     * 
+     * @param <E>
+     * @param key
+     * @return removed hint value
+     */
+    @SuppressWarnings("unchecked")
+    public <E> E removeHintWithoutNotification(Key key) {
+        if (key == null)
+            throw new IllegalArgumentException("key is null");
+
+        Object oldValue = null;
+        synchronized(this) {
+            oldValue = hints.remove(key);
+        }
+        return (E) oldValue;
+    }
+
+    /**
+     * Replace the current hints with the specified set of hints. Hints
+     * that were previously included but are not contained by the new map will
+     * not be preserved by this operation.
+     * 
+     * @param hints the new hints to set
+     */
+    public void replaceHintsWithoutNotification(Map<Key, Object> hints) {
+        Map<Key, Object> copy = new HashMap<Key, Object>(hints);
+        synchronized (this) {
+            this.hints = copy;
+        }
+    }
+
+    /**
+     * Replace the current set of hints with the new specified set of hints.
+     * Notifications are sent for removed and changed hints.
+     * 
+     * @param hints
+     */
+    public void replaceHints(Map<Key, Object> newHints) {
+        List<Runnable> notifications = new ArrayList<Runnable>(Math.max(this.hints.size(), newHints.size()));
+        synchronized (this) {
+            // Calculate removed keys
+            Set<Key> removedKeys = new HashSet<Key>(this.hints.keySet());
+            removedKeys.removeAll(newHints.keySet());
+
+            // Remove keys
+            for (Key key : removedKeys) {
+                Object oldValue = this.hints.remove(key);
+                if (oldValue == null)
+                    continue;
+
+                Runnable notification = createFireKeyRemovedRunnable(this, key, oldValue);
+                notifications.add( notification );
+            }
+
+            // Replace/set existing/new hints
+            for (Entry<Key, Object> e : newHints.entrySet()) {
+                Key key = e.getKey();
+                Object value = e.getValue();
+                if (value == null)
+                    throw new IllegalArgumentException("a value is null for key " + e.getKey());
+                Object oldValue = this.hints.put(key, value);
+                if (value.equals(oldValue))
+                    continue;
+
+                Runnable notification = createFireKeyChangedRunnable(this, key,
+                        oldValue, value);
+                notifications.add( notification );
+            }
+        }
+
+        // Notify then
+        for (Runnable r : notifications)
+            r.run();
+    }
+
+    /**
+     * Set a set of hints without notifying any generic hint or key-specific
+     * listeners. This method will only replace the possible previous values of
+     * the hints included in the specified map. Hints not included in the map
+     * will be preserved as such.
+     * 
+     * @param hints the new hints to set
+     */
+    public void setHintsWithoutNotification(Map<Key, Object> hints) {
+        synchronized (this) {
+            for (Entry<Key, Object> e : hints.entrySet()) {
+                Key key = e.getKey();
+                Object value = e.getValue();
+                if (value == null)
+                    throw new IllegalArgumentException("a value is null for key " + e.getKey());
+                this.hints.put(key, value);
+            }
+        }
+    }
+
+    /**
+     * Compares two object for equality.
+     * <p>
+     * Some times it is annoying to compare two objects if their
+     * value may be null.
+     * 
+     * @param o1 obj1
+     * @param o2 obj2
+     * @return true if equal or both null
+     */
+    public static boolean objectEquals(Object o1, Object o2) {
+        if (o1==o2) return true;
+        if (o1==null && o2==null) return true;
+        if (o1==null || o2==null) return false;
+        return o1.equals(o2);
+    }
+
+    @Override
+    public synchronized Map<Key, Object> getHints() {
+        return new HashMap<Key, Object>(hints);
+    }
+
+    @Override
+    public Map<Key, Object> getHintsUnsafe() {
+        return hints;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public synchronized <E extends Key> Map<E, Object> getHintsOfClass(Class<E> clazz) {
+        Map<E, Object> result = new HashMap<E, Object>();
+        for (Entry<Key, Object> e : hints.entrySet()) {
+            Key key = e.getKey();
+            if (clazz.isAssignableFrom(key.getClass()))
+                result.put((E)key, e.getValue());
+        }
+        return result;
+    }
+
+    public synchronized int size()
+    {
+        return hints.size();
+    }
+
+    @Override
+    public Object clone() {
+        try {
+            return super.clone();
+        } catch (CloneNotSupportedException e) {
+            throw new Error(e);
+        }
+    }
+
+}