1 /*******************************************************************************
2 * Copyright (c) 2010 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.impl;
16 import org.simantics.databoard.adapter.AdaptException;
17 import org.simantics.databoard.adapter.AdapterFactory;
18 import org.simantics.databoard.binding.Binding;
19 import org.simantics.databoard.binding.RecordBinding;
20 import org.simantics.databoard.binding.UnionBinding;
21 import org.simantics.databoard.binding.VariantBinding;
22 import org.simantics.databoard.binding.error.BindingConstructionException;
23 import org.simantics.databoard.binding.error.BindingException;
24 import org.simantics.databoard.binding.factory.BindingRepository;
25 import org.simantics.databoard.binding.reflection.BindingRequest;
26 import org.simantics.databoard.binding.reflection.ClassBindingFactory;
27 import org.simantics.databoard.binding.reflection.VoidBinding;
28 import org.simantics.databoard.type.Datatype;
29 import org.simantics.databoard.type.RecordType;
30 import org.simantics.databoard.type.UnionType;
33 * ObjectVariantBinding binds VariantType and Object.class.
34 * The implementation is immutable as Object.class is not a container, it is
37 * ObjectVariantBinding understands many classes
38 * Collections: ArrayList, List, LinkedList, int[], .., other arrays [], Map, TreeMap, HashMap, Set, HashSet, TreeSet,
40 * immutable primitives: Integer, Boolean, ...
41 * mutable primitives: MutableInteger, MutableBoolean, ...
42 * classes: Record-classes, Bean-Classes, Immutable-Classes
43 * <code>null</code> is bound to a void.
45 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
47 public class ObjectVariantBinding extends VariantBinding {
49 ClassBindingFactory bindingFactory;
50 AdapterFactory adapterFactory;
52 public ObjectVariantBinding(ClassBindingFactory bindingFactory, AdapterFactory adapterFactory) {
53 this.bindingFactory = bindingFactory;
54 this.adapterFactory = adapterFactory;
57 public ClassBindingFactory getBindingFactory() {
58 return bindingFactory;
61 public BindingRepository getRepository() {
62 return bindingFactory.getRepository();
66 public Object create(Binding srcBinding, Object value) throws BindingException {
67 if (srcBinding instanceof VoidBinding) return null;
71 //Annotation[] annotations = new Annotation[ srcBinding.getComponentCount() ];
72 BindingRequest request = new BindingRequest( value.getClass() );
73 //request.componentBinding = srcBinding.getComponentCount()>0?srcBinding.getComponentBinding(0):null;
74 Binding dstBinding = bindingFactory.getBinding( request );
75 return adapterFactory.adapt(value, srcBinding, dstBinding);
76 } catch (BindingConstructionException e) {
77 throw new BindingException(e);
78 } catch (AdaptException e) {
79 throw new BindingException(e);
84 public Binding getContentBinding(Object variant) throws BindingException {
86 if (variant==null) return VoidBinding.VOID_BINDING;
87 BindingRequest request = new BindingRequest(variant.getClass());
88 return bindingFactory.construct(request);
89 } catch (BindingConstructionException e) {
90 throw new BindingException(e);
95 public Datatype getContentType(Object variant) throws BindingException {
96 return getContentBinding(variant).type();
100 public Object getContent(Object variant, Binding binding)
101 throws BindingException {
103 Binding binding1 = getContentBinding(variant);
104 Object value1 = variant;
106 Binding binding2 = binding;
107 Object value2 = adapterFactory.adapt(value1, binding1, binding2);
110 } catch (AdaptException e) {
111 throw new BindingException(e);
116 public Object getContent(Object variant)
117 throws BindingException {
122 public void setContent(Object variant, Binding binding, Object value)
123 throws BindingException {
124 if (variant==value) return;
126 Binding binding1 = getContentBinding(variant);
127 Datatype type1 = binding1.type();
129 Binding binding2 = binding;
130 Datatype type2 = binding2.type();
132 // New value cannot be assigned to Object from different data type
133 if (!type1.equals(type2)) {
134 throw new BindingException("Cannot assign "+value.getClass()+" to "+variant.getClass());
137 // Can only assing to union or record of previous type
138 if (type1 instanceof RecordType) {
139 RecordType recordType = (RecordType) type1;
140 int count = recordType.getComponentCount();
141 RecordBinding rb1 = (RecordBinding) binding1;
142 RecordBinding rb2 = (RecordBinding) binding2;
143 Object resultValues[] = new Object[count];
144 for (int i=0; i<count; i++) {
145 Binding cb1 = rb1.componentBindings[i];
146 Binding cb2 = rb2.componentBindings[i];
148 Object componentValue2 = rb2.getComponent(value, i);
149 Object componentValue1 = adapterFactory.adapt(componentValue2, cb2, cb1);
150 resultValues[i] = componentValue1;
152 rb1.setComponents(variant, resultValues);
153 } else if (type1 instanceof UnionType) {
154 UnionBinding ub1 = (UnionBinding) binding1;
155 UnionBinding ub2 = (UnionBinding) binding2;
156 int tag = ub2.getTag(value);
157 Binding cb1 = ub1.getComponentBindings()[tag];
158 Binding cb2 = ub2.getComponentBindings()[tag];
160 Object componentValue2 = ub2.getValue(value);
161 Object componentValue1 = adapterFactory.adapt(componentValue2, cb2, cb1);
163 ub1.setValue(variant, tag, componentValue1);
165 throw new BindingException("Cannot assign "+value.getClass()+" to "+variant.getClass());
168 } catch (AdaptException e) {
169 throw new BindingException(e);
175 public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
176 getContentBinding(obj).assertInstaceIsValid(obj);
180 public boolean isInstance(Object obj) {
185 public boolean isImmutable() {
190 protected boolean baseEquals(Object obj) {
191 ObjectVariantBinding o = (ObjectVariantBinding)obj;
192 return super.baseEquals( obj ) && o.adapterFactory == adapterFactory && o.bindingFactory == bindingFactory;