]> gerrit.simantics Code Review - simantics/platform.git/blob
e43b7bc736bca5986125f93f22d9b8cc72a26a61
[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 java.util.HashMap;\r
15 import java.util.HashSet;\r
16 import java.util.LinkedList;\r
17 import java.util.List;\r
18 import java.util.Map;\r
19 import java.util.PriorityQueue;\r
20 import java.util.Set;\r
21 import java.util.TreeMap;\r
22 import java.util.TreeSet;\r
23 import java.util.UUID;\r
24 \r
25 import org.simantics.databoard.Datatypes;\r
26 import org.simantics.databoard.binding.Binding;\r
27 import org.simantics.databoard.binding.error.BindingConstructionException;\r
28 import org.simantics.databoard.binding.impl.ArrayListBinding;\r
29 import org.simantics.databoard.binding.impl.BooleanArrayBinding;\r
30 import org.simantics.databoard.binding.impl.ByteArrayBinding;\r
31 import org.simantics.databoard.binding.impl.DoubleArrayBinding;\r
32 import org.simantics.databoard.binding.impl.FloatArrayBinding;\r
33 import org.simantics.databoard.binding.impl.HashMapBinding;\r
34 import org.simantics.databoard.binding.impl.HashSetBinding;\r
35 import org.simantics.databoard.binding.impl.IntArrayBinding;\r
36 import org.simantics.databoard.binding.impl.LinkedListBinding;\r
37 import org.simantics.databoard.binding.impl.LongArrayBinding;\r
38 import org.simantics.databoard.binding.impl.ObjectArrayBinding;\r
39 import org.simantics.databoard.binding.impl.PriorityQueueBinding;\r
40 import org.simantics.databoard.binding.impl.ThrowableBinding;\r
41 import org.simantics.databoard.binding.impl.TreeMapBinding;\r
42 import org.simantics.databoard.binding.impl.TreeSetBinding;\r
43 import org.simantics.databoard.binding.impl.UUIDBinding;\r
44 import org.simantics.databoard.binding.reflection.BindingProvider;\r
45 import org.simantics.databoard.binding.reflection.BindingRequest;\r
46 import org.simantics.databoard.binding.reflection.ClassBindingFactory;\r
47 import org.simantics.databoard.binding.reflection.GenericArrayBinding;\r
48 import org.simantics.databoard.type.ArrayType;\r
49 import org.simantics.databoard.type.MapType;\r
50 \r
51 /**\r
52  * This provider constructs Bindings for java.util classes.\r
53  * \r
54  * @author toni.kalajainen\r
55  */\r
56 public class JavaUtilBindingsProvider implements BindingProvider {\r
57 \r
58         @Override\r
59         public Binding provideBinding(ClassBindingFactory factory, BindingRequest request) throws BindingConstructionException {\r
60 \r
61                 if ( UUID.class.isAssignableFrom( request.getClazz() ) ) {\r
62                         return new UUIDBinding();\r
63                 }\r
64                 \r
65                 if ( Throwable.class.isAssignableFrom( request.getClazz() ) ) {\r
66                         @SuppressWarnings("unchecked")\r
67                         Binding b = new ThrowableBinding( (Class<Throwable>) request.getClazz() ); \r
68                         return b;\r
69                 }\r
70                 \r
71         if (request.getClazz().isArray()) {\r
72                 Class<?> c = request.getClazz();\r
73                 if (c.equals(boolean[].class)) return new BooleanArrayBinding(new ArrayType(), null);\r
74                 else if (c.equals(byte[].class)) return new ByteArrayBinding(new ArrayType(), null);\r
75                 else if (c.equals(double[].class)) return new DoubleArrayBinding(new ArrayType(), null);\r
76                 else if (c.equals(float[].class)) return new FloatArrayBinding(new ArrayType(), null);\r
77                 else if (c.equals(int[].class)) return new IntArrayBinding(new ArrayType(), null);\r
78                 else if (c.equals(long[].class)) return new LongArrayBinding(new ArrayType(), null);\r
79                 else if (c.equals(Object[].class)) return new ObjectArrayBinding(new ArrayType(), null);\r
80                 else return new GenericArrayBinding(request.getClazz(), new ArrayType(), null);\r
81         } else if (LinkedList.class.isAssignableFrom(request.getClazz())) {\r
82                         return new LinkedListBinding(new ArrayType(), null);\r
83         } else if (List.class.isAssignableFrom(request.getClazz())) {\r
84                         return new ArrayListBinding(new ArrayType(), null);\r
85         } else if (PriorityQueue.class.isAssignableFrom(request.getClazz())) {\r
86                 return new PriorityQueueBinding(new ArrayType(), null);\r
87         }\r
88 \r
89         if (TreeSet.class.isAssignableFrom(request.getClazz())) {\r
90             MapType type = new MapType();\r
91             type.valueType = Datatypes.VOID;\r
92                 return new TreeSetBinding(type, null);\r
93         }\r
94 \r
95         if (HashSet.class.isAssignableFrom(request.getClazz())) {\r
96             MapType type = new MapType();\r
97             type.valueType = Datatypes.VOID;\r
98                 return new HashSetBinding(type, null);\r
99         }\r
100         \r
101         if (Set.class.isAssignableFrom(request.getClazz())) {\r
102             MapType type = new MapType();\r
103             type.valueType = Datatypes.VOID;\r
104                 return new TreeSetBinding(type, null);\r
105         }\r
106                 \r
107         if (TreeMap.class.isAssignableFrom(request.getClazz())) {\r
108                 return new TreeMapBinding(new MapType(), null, null);\r
109         } \r
110 \r
111         if (HashMap.class.isAssignableFrom(request.getClazz())) {\r
112                 return new HashMapBinding(new MapType(), null, null);\r
113         }\r
114 \r
115         if (Map.class.isAssignableFrom(request.getClazz())) {\r
116                 return new HashMapBinding(new MapType(), null, null);\r
117         }\r
118                         \r
119                 return null;\r
120         }\r
121 \r
122 }\r