]> gerrit.simantics Code Review - simantics/platform.git/blob
3eb17213be03a74d8a6ac00da6dde81ea48dc7d1
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding.factory;
13
14 import gnu.trove.map.hash.THashMap;
15 import gnu.trove.set.hash.THashSet;
16
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20
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;
31
32 public class TroveBindingsProvider implements BindingProvider {
33
34         @Override
35         public Binding provideBinding(ClassBindingFactory factory, BindingRequest request)
36                         throws BindingConstructionException {
37                 
38         if ( request.getClazz().equals(THashSet.class) ) {
39             MapType type = new MapType();
40             type.valueType = Datatypes.VOID;            
41                 return new THashSetBinding(type, null);
42         }
43                 
44         if ( request.getClazz().equals(THashMap.class) ) {
45             MapType type = new MapType();
46                 return new THashMapBinding(type, null, null);
47         }
48                 
49                 return null;
50         }
51
52         static class THashMapBinding extends HashMapBinding {
53                 
54                 public THashMapBinding(Binding keyBinding, Binding valueBinding) {
55                         super(keyBinding, valueBinding);
56                 }
57
58                 public THashMapBinding(MapType mapType, Binding keyBinding,
59                                 Binding valueBinding) {
60                         super(mapType, keyBinding, valueBinding);
61                 }
62
63                 @Override
64                 public Object create() {                
65                         return new THashMap<Object, Object>();
66                 }
67                 
68                 @Override
69                 public Object create(Object[] keys, Object[] values) {
70                         if (keys.length!=values.length)
71                                 throw new IllegalArgumentException("Equal length arrays expected");
72                         
73                         int len = keys.length;
74                         THashMap<Object, Object> result = new THashMap<Object, Object>(len);
75                         
76                         for (int i=0; i<len; i++) {
77                                 Object key = keys[i];
78                                 key = getComparableKey(result, key);                    
79                                 Object value = values[i];
80                                 result.put(key, value);
81                         }
82                         
83                         return result;
84                 }
85                 
86                 @Override
87                 public Object create(List<Object> keys, List<Object> values) {
88                         if (keys.size()!=values.size())
89                                 throw new IllegalArgumentException("Equal length arrays expected");
90                         
91                         int len = keys.size();
92                         THashMap<Object, Object> result = new THashMap<Object, Object>(len);
93                         
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);
99                         }
100                         
101                         return result;
102                 }
103                 
104                 @Override
105                 public Object create(Map<?, ?> initialMap) throws BindingException {
106                     if (initialMap instanceof THashMap)
107                         return initialMap;
108                     
109                         // Replace with TreeMap. Create comparator from binding.
110                         THashMap<Object, Object> result = new THashMap<Object, Object>();
111                         putAll(result, initialMap);
112                         return result;
113                 }
114                 
115                 @Override
116                 public boolean isInstance(Object obj) {
117                         return obj instanceof THashMap;
118                 }
119                 
120         }
121         
122         static class THashSetBinding extends HashSetBinding {
123                 
124                 public THashSetBinding(Binding elementBinding) {
125                         super(elementBinding);
126                         // TODO Auto-generated constructor stub
127                 }
128
129                 public THashSetBinding(MapType mapType, Binding elementBinding) {
130                         super(mapType, elementBinding);
131                         // TODO Auto-generated constructor stub
132                 }
133
134                 @Override
135                 public Object create() throws BindingException {
136                         return new THashSet<Object>();
137                 }
138
139                 public Object create(Set<?> initialSet) throws BindingException {
140                     if (initialSet instanceof THashSet)
141                         return initialSet;
142                     
143                         return new THashSet<Object>(initialSet);
144                 }
145
146                 @SuppressWarnings({ "unchecked", "rawtypes" })
147         @Override
148                 public Object create(Map initialMap) throws BindingException {
149                         return new THashSet<Object>(initialMap.keySet());
150                 }
151
152                 @Override
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++)
157                                 result.add(keys[i]);
158                         return result;
159                 }
160                 
161                 @Override
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));
166                         return result;
167                 }
168                 
169                 @Override
170                 public boolean isInstance(Object obj) {
171                         return obj instanceof THashSet;
172                 }               
173                 
174         }
175         
176 }