]> gerrit.simantics Code Review - simantics/platform.git/blob
e635e12a03d27a70a5e37a19d8a63e1c692c6f52
[simantics/platform.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * 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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.binding.factory;\r
13 \r
14 import gnu.trove.map.hash.THashMap;\r
15 import gnu.trove.set.hash.THashSet;\r
16 \r
17 import java.util.List;\r
18 import java.util.Map;\r
19 import java.util.Set;\r
20 \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
31 \r
32 public class TroveBindingsProvider implements BindingProvider {\r
33 \r
34         @Override\r
35         public Binding provideBinding(ClassBindingFactory factory, BindingRequest request)\r
36                         throws BindingConstructionException {\r
37                 \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
42         }\r
43                 \r
44         if ( request.getClazz().equals(THashMap.class) ) {\r
45             MapType type = new MapType();\r
46                 return new THashMapBinding(type, null, null);\r
47         }\r
48                 \r
49                 return null;\r
50         }\r
51 \r
52         static class THashMapBinding extends HashMapBinding {\r
53                 \r
54                 public THashMapBinding(Binding keyBinding, Binding valueBinding) {\r
55                         super(keyBinding, valueBinding);\r
56                 }\r
57 \r
58                 public THashMapBinding(MapType mapType, Binding keyBinding,\r
59                                 Binding valueBinding) {\r
60                         super(mapType, keyBinding, valueBinding);\r
61                 }\r
62 \r
63                 @Override\r
64                 public Object create() {                \r
65                         return new THashMap<Object, Object>();\r
66                 }\r
67                 \r
68                 @Override\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
72                         \r
73                         int len = keys.length;\r
74                         THashMap<Object, Object> result = new THashMap<Object, Object>(len);\r
75                         \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
81                         }\r
82                         \r
83                         return result;\r
84                 }\r
85                 \r
86                 @Override\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
90                         \r
91                         int len = keys.size();\r
92                         THashMap<Object, Object> result = new THashMap<Object, Object>(len);\r
93                         \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
99                         }\r
100                         \r
101                         return result;\r
102                 }\r
103                 \r
104                 @Override\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
109                         return result;\r
110                 }\r
111                 \r
112                 @Override\r
113                 public boolean isInstance(Object obj) {\r
114                         return obj instanceof THashMap;\r
115                 }\r
116                 \r
117         }\r
118         \r
119         static class THashSetBinding extends HashSetBinding {\r
120                 \r
121                 public THashSetBinding(Binding elementBinding) {\r
122                         super(elementBinding);\r
123                         // TODO Auto-generated constructor stub\r
124                 }\r
125 \r
126                 public THashSetBinding(MapType mapType, Binding elementBinding) {\r
127                         super(mapType, elementBinding);\r
128                         // TODO Auto-generated constructor stub\r
129                 }\r
130 \r
131                 @Override\r
132                 public Object create() throws BindingException {\r
133                         return new THashSet<Object>();\r
134                 }\r
135 \r
136                 public Object create(Set<?> initialSet) throws BindingException {\r
137                         return new THashSet<Object>(initialSet);\r
138                 }\r
139 \r
140                 @Override\r
141                 public Object create(Map initialMap) throws BindingException {\r
142                         return new THashSet<Object>(initialMap.keySet());\r
143                 }\r
144 \r
145                 @Override\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
151                         return result;\r
152                 }\r
153                 \r
154                 @Override\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
159                         return result;\r
160                 }\r
161                 \r
162                 @Override\r
163                 public boolean isInstance(Object obj) {\r
164                         return obj instanceof THashSet;\r
165                 }               \r
166                 \r
167         }\r
168         \r
169 }\r