1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.binding;
14 import java.util.IdentityHashMap;
\r
15 import java.util.Set;
\r
17 import org.simantics.databoard.Bindings;
\r
18 import org.simantics.databoard.Datatypes;
\r
19 import org.simantics.databoard.accessor.reference.ChildReference;
\r
20 import org.simantics.databoard.adapter.AdaptException;
\r
21 import org.simantics.databoard.binding.error.BindingException;
\r
22 import org.simantics.databoard.binding.error.RuntimeBindingException;
\r
23 import org.simantics.databoard.binding.impl.BindingPrintContext;
\r
24 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
\r
25 import org.simantics.databoard.binding.impl.StringVariantBinding;
\r
26 import org.simantics.databoard.binding.mutable.ImmutableVariantBinding;
\r
27 import org.simantics.databoard.binding.mutable.MutableVariant;
\r
28 import org.simantics.databoard.binding.mutable.MutableVariantBinding;
\r
29 import org.simantics.databoard.binding.mutable.Variant;
\r
30 import org.simantics.databoard.type.Datatype;
\r
31 import org.simantics.databoard.type.VariantType;
\r
32 import org.simantics.databoard.util.DataValueUtil;
\r
33 import org.simantics.databoard.util.IdentityPair;
\r
36 * This is the abstract base class for bindings of VariantType Java Objects.
\r
37 * Variant is a container that has value of any Datatype.
39 * @see VariantType The Datatype
\r
40 * @see ImmutableVariantBinding Binds variant to {@link Variant}
\r
41 * @see MutableVariantBinding Binds variant to {@link MutableVariant}
\r
42 * @see ObjectVariantBinding Binds variant to java.lang.Object
\r
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
\r
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.
\r
80 * @param variant the variant object
\r
81 * @return the data type
\r
82 * @throws BindingException
\r
84 public abstract Datatype getContentType(Object variant)
85 throws BindingException;
88 * Return a suggestion for the binding of the content of this variant.
\r
90 * @param variant variant object
\r
91 * @return binding a binding
\r
93 public abstract Binding getContentBinding(Object variant)
\r
94 throws BindingException;
\r
97 * Create a new variant object.
\r
98 * The <code>value</code> argument may be included in the result.
100 * @param contentBinding the binding of the content
\r
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.
\r
111 * @param contentBinding binding of the content
\r
112 * @param content content
\r
113 * @return new variant
\r
114 * @throws RuntimeBindingException
\r
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)
\r
127 throws BindingException {
\r
129 VariantBinding sb = (VariantBinding) srcBinding;
\r
130 Datatype newType = sb.getContentType(src);
\r
131 Datatype oldType = getContentType(dst);
\r
132 Binding scb = sb.getContentBinding(src);
\r
133 Object sc = sb.getContent(src, scb);
\r
134 if (newType.equals(oldType)) {
\r
135 Binding dcb = getContentBinding(dst);
\r
136 if (dcb.isImmutable()) {
\r
137 Object dc = Bindings.clone(sc, scb, scb);
\r
138 setContent(dst, scb, dc);
\r
140 Object dc = getContent(dst, dcb);
\r
141 dc = dcb.readFromTry(scb, sc, dc);
\r
142 setContent(dst, dcb, dc);
\r
145 Object dc = Bindings.clone(sc, scb, scb);
\r
146 setContent(dst, scb, dc);
\r
148 } catch (AdaptException e) {
\r
149 throw new BindingException(e);
\r
155 * Set the content of an variant.
157 * @param variant variant object
158 * @param contentBinding content's binding
\r
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 Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class);
180 Object element = getContent(value, binding);
181 return dataTypeBinding.deepHashValue(type, hashedObjects) + binding.deepHashValue(element, hashedObjects);
185 public int deepCompare(Object o1, Object o2,
186 Set<IdentityPair<Object, Object>> compareHistory)
187 throws BindingException {
189 Datatype t1 = getContentType(o1);
190 Datatype t2 = getContentType(o2);
191 Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class);
192 int dif = dataTypeBinding.compare(t1, t2);
193 if (dif!=0) return dif;
195 Binding bi1 = getContentBinding(o1);
196 Binding bi2 = getContentBinding(o2);
197 Object va1 = getContent(o1, bi1);
198 Object va2 = getContent(o2, bi2);
199 return DataValueUtil.compare(bi1, va1, bi2, va2);
203 protected void toString(Object value, BindingPrintContext ctx) throws BindingException {
\r
204 Binding b = getContentBinding(value);
\r
205 b.toString(getContent(value), ctx);
\r
207 ctx.b.append(" : ");
\r
208 ctx.b.append( ctx.singleLine ? b.type.toSingleLineString() : b.type.toString() );
\r
212 public Binding getComponentBinding(ChildReference path) {
\r
213 if (path==null) return this;
\r
214 throw new IllegalArgumentException();
\r
218 public int getComponentCount() {
\r
223 public Binding getComponentBinding(int index) {
\r
224 throw new IllegalArgumentException();
\r