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;
14 import java.util.IdentityHashMap;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.databoard.Datatypes;
19 import org.simantics.databoard.accessor.reference.ChildReference;
20 import org.simantics.databoard.adapter.AdaptException;
21 import org.simantics.databoard.binding.error.BindingException;
22 import org.simantics.databoard.binding.error.RuntimeBindingException;
23 import org.simantics.databoard.binding.impl.BindingPrintContext;
24 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
25 import org.simantics.databoard.binding.impl.StringVariantBinding;
26 import org.simantics.databoard.binding.mutable.ImmutableVariantBinding;
27 import org.simantics.databoard.binding.mutable.MutableVariant;
28 import org.simantics.databoard.binding.mutable.MutableVariantBinding;
29 import org.simantics.databoard.binding.mutable.Variant;
30 import org.simantics.databoard.type.Datatype;
31 import org.simantics.databoard.type.VariantType;
32 import org.simantics.databoard.util.DataValueUtil;
33 import org.simantics.databoard.util.IdentityPair;
36 * This is the abstract base class for bindings of VariantType Java Objects.
37 * Variant is a container that has value of any Datatype.
39 * @see VariantType The Datatype
40 * @see ImmutableVariantBinding Binds variant to {@link Variant}
41 * @see MutableVariantBinding Binds variant to {@link MutableVariant}
42 * @see ObjectVariantBinding Binds variant to java.lang.Object
43 * @see StringVariantBinding Binds variant to java.lang.String (Filename and URL compatible)
44 * @author Toni Kalajainen
46 public abstract class VariantBinding extends Binding {
48 public VariantBinding()
51 this.type = Datatypes.VARIANT;
55 * Get the value in the variant. The returned value may represent internal
56 * value of <code>variant</code>.
58 * @param variant the variant object to read the content from
59 * @param contentBinding the format of return value
60 * @return value variant's content
61 * @throws BindingException
63 public abstract Object getContent(Object variant, Binding contentBinding)
64 throws BindingException;
67 * Get the value of the variant. The value is bound with the suggested
68 * binding, see {@link #getContentBinding(Object)}.
70 * @param variant the variant object
71 * @return value variant's content
72 * @throws BindingException
74 public abstract Object getContent(Object variant)
75 throws BindingException;
78 * Get the data type of the content.
80 * @param variant the variant object
81 * @return the data type
82 * @throws BindingException
84 public abstract Datatype getContentType(Object variant)
85 throws BindingException;
88 * Return a suggestion for the binding of the content of this variant.
90 * @param variant variant object
91 * @return binding a binding
93 public abstract Binding getContentBinding(Object variant)
94 throws BindingException;
97 * Create a new variant object.
98 * The <code>value</code> argument may be included in the result.
100 * @param contentBinding the binding of the content
101 * @param content content
102 * @return new variant
103 * @throws BindingException
105 public abstract Object create(Binding contentBinding, Object content)
106 throws BindingException;
109 * Create a new variant object.
111 * @param contentBinding binding of the content
112 * @param content content
113 * @return new variant
114 * @throws RuntimeBindingException
116 public Object createUnchecked(Binding contentBinding, Object content)
117 throws RuntimeBindingException {
119 return create(contentBinding, content);
120 } catch (BindingException e) {
121 throw new RuntimeBindingException(e);
126 public void readFrom(Binding srcBinding, Object src, Object dst)
127 throws BindingException {
129 VariantBinding sb = (VariantBinding) srcBinding;
130 Datatype newType = sb.getContentType(src);
131 Datatype oldType = getContentType(dst);
132 Binding scb = sb.getContentBinding(src);
133 Object sc = sb.getContent(src, scb);
134 if (newType.equals(oldType)) {
135 Binding dcb = getContentBinding(dst);
136 if (dcb.isImmutable()) {
137 Object dc = Bindings.clone(sc, scb, scb);
138 setContent(dst, scb, dc);
140 Object dc = getContent(dst, dcb);
141 dc = dcb.readFromTry(scb, sc, dc);
142 setContent(dst, dcb, dc);
145 Object dc = Bindings.clone(sc, scb, scb);
146 setContent(dst, scb, dc);
148 } catch (AdaptException e) {
149 throw new BindingException(e);
155 * Set the content of an variant.
157 * @param variant variant object
158 * @param contentBinding content's binding
159 * @param content new content
160 * @throws BindingException
162 public abstract void setContent(Object variant, Binding contentBinding, Object content)
163 throws BindingException;
166 public void accept(Visitor1 v, Object obj) {
171 public <T> T accept(Visitor<T> v) {
172 return v.visit(this);
176 public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
177 Datatype type = getContentType(value);
178 Binding binding = getContentBinding(value);
179 Object element = getContent(value, binding);
180 return Bindings.DATATYPE.deepHashValue(type, hashedObjects) + binding.deepHashValue(element, hashedObjects);
184 public int deepCompare(Object o1, Object o2,
185 Set<IdentityPair<Object, Object>> compareHistory)
186 throws BindingException {
188 Datatype t1 = getContentType(o1);
189 Datatype t2 = getContentType(o2);
190 int dif = Bindings.DATATYPE.compare(t1, t2);
191 if (dif!=0) return dif;
193 Binding bi1 = getContentBinding(o1);
194 Binding bi2 = getContentBinding(o2);
195 Object va1 = getContent(o1, bi1);
196 Object va2 = getContent(o2, bi2);
197 return DataValueUtil.compare(bi1, va1, bi2, va2);
201 protected void toString(Object value, BindingPrintContext ctx) throws BindingException {
202 Binding b = getContentBinding(value);
203 b.toString(getContent(value), ctx);
206 ctx.b.append( ctx.singleLine ? b.type.toSingleLineString() : b.type.toString() );
210 public Binding getComponentBinding(ChildReference path) {
211 if (path==null) return this;
212 throw new IllegalArgumentException();
216 public int getComponentCount() {
221 public Binding getComponentBinding(int index) {
222 throw new IllegalArgumentException();