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