1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.element;
14 import java.util.HashMap;
18 * An IElementClassProvider provides an abstract way of requesting an
19 * ElementClass for a specifiable key. The key is purposefully left abstract.
21 * @author Tuukka Lehtonen
23 * @see IElementClassProvider
26 public class ElementClassProviders {
29 * Create an element class provider that provides only a single static
30 * ElementClass regardless of the specified key argument.
32 * @param clazz the static element class to provide
33 * @return a provider for the specified element class
35 public static IElementClassProvider staticProvider(final ElementClass clazz) {
36 return new IElementClassProvider() {
38 public ElementClass get(Object key) {
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.
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
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() {
57 public ElementClass get(Object key) {
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>.
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
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);
83 return mappedProvider(map);