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