--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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
+ *******************************************************************************/\r
+/*\r
+ *\r
+ * @author Toni Kalajainen\r
+ */\r
+package org.simantics.utils.datastructures;\r
+\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.Map;\r
+import java.util.Set;\r
+import java.util.Map.Entry;\r
+\r
+\r
+/**\r
+ * Context dependent Key to Key Bijection Mapping. \r
+ *\r
+ * @param <Context>\r
+ * @param <Key>\r
+ */\r
+public class MappingWithContext<Context, Key> {\r
+\r
+ public static class IntersectingContextsException extends Exception {\r
+ private static final long serialVersionUID = 3411795376917295313L;\r
+ public IntersectingContextsException(Object c, Object key1, Object key2) {\r
+ super("The two keys "+key1+" and "+key2+" are intersecting in context "+c);\r
+ }\r
+ };\r
+ \r
+ Map<Context, BijectionMap<Key, Key>> maps = \r
+ new HashMap<Context, BijectionMap<Key, Key>>();\r
+ \r
+ /**\r
+ * Add mapping. null context applies always.\r
+ * \r
+ * @param context\r
+ * @param leftKey\r
+ * @param rightKey\r
+ */\r
+ public synchronized void addMapping(Context context, Key leftKey, Key rightKey)\r
+ {\r
+ BijectionMap<Key, Key> map = getOrCreateMap(context);\r
+ map.map(leftKey, rightKey); \r
+ }\r
+\r
+ /**\r
+ * Get right value with left key\r
+ * @param contexts effective contexts\r
+ * @param leftKey\r
+ * @return a single right value or null\r
+ * @throws IntersectingContextsException\r
+ */\r
+ public synchronized Key getAtMostOneMappingWithLeftKey(Context[] contexts, Key leftKey)\r
+ throws IntersectingContextsException\r
+ {\r
+ Key result = null;\r
+ Context resultContext = null;\r
+ for (Context c : contexts)\r
+ {\r
+ BijectionMap<Key, Key> map = maps.get(c);\r
+ if (map==null) continue;\r
+ Key value = map.getRight(leftKey);\r
+ if (value==null) continue;\r
+ if (result!=null) throw new IntersectingContextsException(resultContext, value, result);\r
+ result = value;\r
+ resultContext = c;\r
+ }\r
+\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * Get left value with right key\r
+ * @param contexts effective contexts\r
+ * @param rightKey\r
+ * @return a single left value or null\r
+ * @throws IntersectingContextsException\r
+ */\r
+ public synchronized Key getAtMostOneMappingWithRightKey(Context[] contexts, Key rightKey)\r
+ throws IntersectingContextsException\r
+ {\r
+ Key result = null;\r
+ Context resultContext = null;\r
+ for (Context c : contexts)\r
+ {\r
+ BijectionMap<Key, Key> map = maps.get(c);\r
+ if (map==null) continue;\r
+ Key value = map.getLeft(rightKey);\r
+ if (value==null) continue;\r
+ if (result!=null) throw new IntersectingContextsException(resultContext, value, result);\r
+ result = value;\r
+ resultContext = c;\r
+ }\r
+\r
+ return result;\r
+ }\r
+ \r
+ /**\r
+ * Get all right values with left key\r
+ * @param contexts effective contexts\r
+ * @param leftKey\r
+ * @return a single right value or null\r
+ * @throws IntersectingContextsException\r
+ */\r
+ public synchronized Set<Key> getMappingWithLeftKey(Context[] contexts, Key leftKey)\r
+ throws IntersectingContextsException\r
+ {\r
+ Set<Key> result = new HashSet<Key>();\r
+ for (Context c : contexts)\r
+ {\r
+ BijectionMap<Key, Key> map = maps.get(c);\r
+ if (map==null) continue;\r
+ Key value = map.getRight(leftKey);\r
+ if (value==null) continue;\r
+ result.add(value);\r
+ }\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * Get all left values with right key\r
+ * @param contexts effective contexts\r
+ * @param rightKey\r
+ * @return a single left value or null\r
+ * @throws IntersectingContextsException\r
+ */\r
+ public synchronized Set<Key> getMappingWithRightKey(Context[] contexts, Key rightKey)\r
+ throws IntersectingContextsException\r
+ {\r
+ Set<Key> result = new HashSet<Key>();\r
+ for (Context c : contexts)\r
+ {\r
+ BijectionMap<Key, Key> map = maps.get(c);\r
+ if (map==null) continue;\r
+ Key value = map.getLeft(rightKey);\r
+ if (value==null) continue;\r
+ result.add(value);\r
+ }\r
+ return result;\r
+ } \r
+ \r
+ private synchronized BijectionMap<Key, Key> getOrCreateMap(Context context)\r
+ {\r
+ BijectionMap<Key, Key> result = maps.get(context);\r
+ if (result!=null) return result;\r
+ result = new BijectionMap<Key, Key>();\r
+ maps.put(context, result); \r
+ return result; \r
+ }\r
+ \r
+ public synchronized void addMapToContext(Context context, BijectionMap<Key, Key> map)\r
+ {\r
+ BijectionMap<Key, Key> m = getOrCreateMap(context);\r
+ m.addAll(map);\r
+ }\r
+ \r
+ public synchronized Set<Context> getContexts()\r
+ {\r
+ return Collections.unmodifiableSet(maps.keySet());\r
+ }\r
+ \r
+ public synchronized Set<Key> getLeftKeys(Context context)\r
+ {\r
+ BijectionMap<Key, Key> map = maps.get(context);\r
+ if (map==null) return null;\r
+ return Collections.unmodifiableSet(map.getLeftSet()); \r
+ }\r
+ \r
+ public synchronized Set<Key> getRightKeys(Context context)\r
+ {\r
+ BijectionMap<Key, Key> map = maps.get(context);\r
+ if (map==null) return null;\r
+ return Collections.unmodifiableSet(map.getRightSet()); \r
+ }\r
+\r
+ public synchronized Set<Key> getAllLeftKeys()\r
+ {\r
+ Set<Key> result = new HashSet<Key>();\r
+ for (Context context : getContexts())\r
+ result.addAll(getLeftKeys(context));\r
+ return result;\r
+ }\r
+ \r
+ public synchronized Set<Key> getAllRightKeys()\r
+ {\r
+ Set<Key> result = new HashSet<Key>();\r
+ for (Context context : getContexts())\r
+ result.addAll(getRightKeys(context));\r
+ return result;\r
+ }\r
+ \r
+ @Override\r
+ public String toString() {\r
+ int count = 0;\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("[");\r
+ for (Entry<Context, BijectionMap<Key, Key>> e : maps.entrySet())\r
+ {\r
+ if (count++>0) sb.append(", ");\r
+ sb.append((e.getKey()==null?"null":e.getKey().toString()));\r
+ sb.append("=");\r
+ sb.append(e.getValue().toString()); \r
+ }\r
+ sb.append("]");\r
+ return sb.toString();\r
+ } \r
+ \r
+}\r