]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/ElementClassProviders.java
Merge commit 'bd5bc6e45f700e755b61bd112631796631330ecb'
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / ElementClassProviders.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.element;\r
13 \r
14 import java.util.HashMap;\r
15 import java.util.Map;\r
16 \r
17 /**\r
18  * An IElementClassProvider provides an abstract way of requesting an\r
19  * ElementClass for a specifiable key. The key is purposefully left abstract.\r
20  * \r
21  * @author Tuukka Lehtonen\r
22  * \r
23  * @see IElementClassProvider\r
24  * @see ElementClasses\r
25  */\r
26 public class ElementClassProviders {\r
27 \r
28     /**\r
29      * Create an element class provider that provides only a single static\r
30      * ElementClass regardless of the specified key argument.\r
31      * \r
32      * @param clazz the static element class to provide\r
33      * @return a provider for the specified element class\r
34      */\r
35     public static IElementClassProvider staticProvider(final ElementClass clazz) {\r
36         return new IElementClassProvider() {\r
37             @Override\r
38             public ElementClass get(Object key) {\r
39                 return clazz;\r
40             }\r
41         };\r
42     }\r
43 \r
44     /**\r
45      * Create an element class provider that based on the specified map. The\r
46      * provider will directly access the map with the received keys. The\r
47      * argument map will be copied.\r
48      * \r
49      * @param map the map to use for element class provision\r
50      * @return <code>null</code> if there is no provider for the specified key\r
51      */\r
52     public static IElementClassProvider mappedProvider(Map<Object, ElementClass> map) {\r
53         // Copy the map as a safety measure\r
54         final Map<Object, ElementClass> copy = new HashMap<Object, ElementClass>(map);\r
55         return new IElementClassProvider() {\r
56             @Override\r
57             public ElementClass get(Object key) {\r
58                 return copy.get(key);\r
59             }\r
60         };\r
61     }\r
62 \r
63     /**\r
64      * Does the same as {@link #mappedProvider(Map)}, the map is simply provided\r
65      * differently. The specified array must contain\r
66      * <code>[key, ElementClass, key, ElementClass, ...]</code>.\r
67      * \r
68      * @param map the map to use for element class provision\r
69      * @return <code>null</code> if there is no provider for the specified key\r
70      */\r
71     public static IElementClassProvider mappedProvider(Object... keyClassPairs) {\r
72         if (keyClassPairs.length % 2 != 0)\r
73             throw new IllegalArgumentException();\r
74         Map<Object, ElementClass> map = new HashMap<Object, ElementClass>();\r
75         int n = keyClassPairs.length / 2;\r
76         for (int i = 0; i < n; ++i) {\r
77             Object key = keyClassPairs[i * 2];\r
78             Object elementClass = keyClassPairs[i*2+1];\r
79             if (!(elementClass instanceof ElementClass))\r
80                 throw new IllegalArgumentException("not an ElementClass instance: "  + elementClass);\r
81             map.put(key, (ElementClass) elementClass);\r
82         }\r
83         return mappedProvider(map);\r
84     }\r
85 \r
86 }\r