1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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.databoard.binding.factory;
14 import gnu.trove.map.hash.THashMap;
15 import gnu.trove.set.hash.THashSet;
17 import java.util.List;
21 import org.simantics.databoard.Datatypes;
22 import org.simantics.databoard.binding.Binding;
23 import org.simantics.databoard.binding.error.BindingConstructionException;
24 import org.simantics.databoard.binding.error.BindingException;
25 import org.simantics.databoard.binding.impl.HashMapBinding;
26 import org.simantics.databoard.binding.impl.HashSetBinding;
27 import org.simantics.databoard.binding.reflection.BindingProvider;
28 import org.simantics.databoard.binding.reflection.BindingRequest;
29 import org.simantics.databoard.binding.reflection.ClassBindingFactory;
30 import org.simantics.databoard.type.MapType;
32 public class TroveBindingsProvider implements BindingProvider {
35 public Binding provideBinding(ClassBindingFactory factory, BindingRequest request)
36 throws BindingConstructionException {
38 if ( request.getClazz().equals(THashSet.class) ) {
39 MapType type = new MapType();
40 type.valueType = Datatypes.VOID;
41 return new THashSetBinding(type, null);
44 if ( request.getClazz().equals(THashMap.class) ) {
45 MapType type = new MapType();
46 return new THashMapBinding(type, null, null);
52 static class THashMapBinding extends HashMapBinding {
54 public THashMapBinding(Binding keyBinding, Binding valueBinding) {
55 super(keyBinding, valueBinding);
58 public THashMapBinding(MapType mapType, Binding keyBinding,
59 Binding valueBinding) {
60 super(mapType, keyBinding, valueBinding);
64 public Object create() {
65 return new THashMap<Object, Object>();
69 public Object create(Object[] keys, Object[] values) {
70 if (keys.length!=values.length)
71 throw new IllegalArgumentException("Equal length arrays expected");
73 int len = keys.length;
74 THashMap<Object, Object> result = new THashMap<Object, Object>(len);
76 for (int i=0; i<len; i++) {
78 key = getComparableKey(result, key);
79 Object value = values[i];
80 result.put(key, value);
87 public Object create(List<Object> keys, List<Object> values) {
88 if (keys.size()!=values.size())
89 throw new IllegalArgumentException("Equal length arrays expected");
91 int len = keys.size();
92 THashMap<Object, Object> result = new THashMap<Object, Object>(len);
94 for (int i=0; i<len; i++) {
95 Object key = keys.get(i);
96 key = getComparableKey(result, key);
97 Object value = values.get(i);
98 result.put(key, value);
105 public Object create(Map<?, ?> initialMap) throws BindingException {
106 if (initialMap instanceof THashMap)
109 // Replace with TreeMap. Create comparator from binding.
110 THashMap<Object, Object> result = new THashMap<Object, Object>();
111 putAll(result, initialMap);
116 public boolean isInstance(Object obj) {
117 return obj instanceof THashMap;
122 static class THashSetBinding extends HashSetBinding {
124 public THashSetBinding(Binding elementBinding) {
125 super(elementBinding);
126 // TODO Auto-generated constructor stub
129 public THashSetBinding(MapType mapType, Binding elementBinding) {
130 super(mapType, elementBinding);
131 // TODO Auto-generated constructor stub
135 public Object create() throws BindingException {
136 return new THashSet<Object>();
139 public Object create(Set<?> initialSet) throws BindingException {
140 if (initialSet instanceof THashSet)
143 return new THashSet<Object>(initialSet);
146 @SuppressWarnings({ "unchecked", "rawtypes" })
148 public Object create(Map initialMap) throws BindingException {
149 return new THashSet<Object>(initialMap.keySet());
153 public Object create(Object[] keys, Object[] values)
154 throws BindingException {
155 THashSet<Object> result = new THashSet<Object>(keys.length);
156 for (int i=0; i<keys.length; i++)
162 public Object create(List<Object> keys, List<Object> values) {
163 THashSet<Object> result = new THashSet<Object>(keys.size());
164 for (int i=0; i<keys.size(); i++)
165 result.add(keys.get(i));
170 public boolean isInstance(Object obj) {
171 return obj instanceof THashSet;