]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/element/ElementClassProviders.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / ElementClassProviders.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/ElementClassProviders.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/ElementClassProviders.java
new file mode 100644 (file)
index 0000000..94af84b
--- /dev/null
@@ -0,0 +1,86 @@
+/*******************************************************************************\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
+package org.simantics.g2d.element;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+/**\r
+ * An IElementClassProvider provides an abstract way of requesting an\r
+ * ElementClass for a specifiable key. The key is purposefully left abstract.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ * \r
+ * @see IElementClassProvider\r
+ * @see ElementClasses\r
+ */\r
+public class ElementClassProviders {\r
+\r
+    /**\r
+     * Create an element class provider that provides only a single static\r
+     * ElementClass regardless of the specified key argument.\r
+     * \r
+     * @param clazz the static element class to provide\r
+     * @return a provider for the specified element class\r
+     */\r
+    public static IElementClassProvider staticProvider(final ElementClass clazz) {\r
+        return new IElementClassProvider() {\r
+            @Override\r
+            public ElementClass get(Object key) {\r
+                return clazz;\r
+            }\r
+        };\r
+    }\r
+\r
+    /**\r
+     * Create an element class provider that based on the specified map. The\r
+     * provider will directly access the map with the received keys. The\r
+     * argument map will be copied.\r
+     * \r
+     * @param map the map to use for element class provision\r
+     * @return <code>null</code> if there is no provider for the specified key\r
+     */\r
+    public static IElementClassProvider mappedProvider(Map<Object, ElementClass> map) {\r
+        // Copy the map as a safety measure\r
+        final Map<Object, ElementClass> copy = new HashMap<Object, ElementClass>(map);\r
+        return new IElementClassProvider() {\r
+            @Override\r
+            public ElementClass get(Object key) {\r
+                return copy.get(key);\r
+            }\r
+        };\r
+    }\r
+\r
+    /**\r
+     * Does the same as {@link #mappedProvider(Map)}, the map is simply provided\r
+     * differently. The specified array must contain\r
+     * <code>[key, ElementClass, key, ElementClass, ...]</code>.\r
+     * \r
+     * @param map the map to use for element class provision\r
+     * @return <code>null</code> if there is no provider for the specified key\r
+     */\r
+    public static IElementClassProvider mappedProvider(Object... keyClassPairs) {\r
+        if (keyClassPairs.length % 2 != 0)\r
+            throw new IllegalArgumentException();\r
+        Map<Object, ElementClass> map = new HashMap<Object, ElementClass>();\r
+        int n = keyClassPairs.length / 2;\r
+        for (int i = 0; i < n; ++i) {\r
+            Object key = keyClassPairs[i * 2];\r
+            Object elementClass = keyClassPairs[i*2+1];\r
+            if (!(elementClass instanceof ElementClass))\r
+                throw new IllegalArgumentException("not an ElementClass instance: "  + elementClass);\r
+            map.put(key, (ElementClass) elementClass);\r
+        }\r
+        return mappedProvider(map);\r
+    }\r
+\r
+}\r