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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.element;
\r
14 import java.util.HashMap;
\r
15 import java.util.Map;
\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
21 * @author Tuukka Lehtonen
\r
23 * @see IElementClassProvider
\r
24 * @see ElementClasses
\r
26 public class ElementClassProviders {
\r
29 * Create an element class provider that provides only a single static
\r
30 * ElementClass regardless of the specified key argument.
\r
32 * @param clazz the static element class to provide
\r
33 * @return a provider for the specified element class
\r
35 public static IElementClassProvider staticProvider(final ElementClass clazz) {
\r
36 return new IElementClassProvider() {
\r
38 public ElementClass get(Object key) {
\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
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
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
57 public ElementClass get(Object key) {
\r
58 return copy.get(key);
\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
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
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
83 return mappedProvider(map);
\r