--- /dev/null
+/*******************************************************************************\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