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