X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Fmutable%2FMutableVariant.java;fp=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Fmutable%2FMutableVariant.java;h=0d6b6f1e7800bac95c45e2ea2e680e2acfe6905f;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/mutable/MutableVariant.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/mutable/MutableVariant.java new file mode 100644 index 000000000..0d6b6f1e7 --- /dev/null +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/mutable/MutableVariant.java @@ -0,0 +1,148 @@ +/******************************************************************************* + * Copyright (c) 2010 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.databoard.binding.mutable; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InvalidObjectException; +import java.io.ObjectStreamException; +import java.io.Serializable; + +import org.simantics.databoard.Accessors; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.accessor.error.AccessorConstructionException; +import org.simantics.databoard.accessor.error.AccessorException; +import org.simantics.databoard.accessor.java.JavaObject; +import org.simantics.databoard.accessor.reference.ChildReference; +import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.reflection.VoidBinding; +import org.simantics.databoard.type.Datatype; + +/** + * MutableVariant is a container to a data value of any type. + * The value and type can be changed. + * + * MutableVariant is hash-equals-comparable, even variants of bindings (and types). + * The hash function and comparison rules are defined in the manual. + * + * @see MutableVariantBinding is binding for Variant-class + * @author Toni Kalajainen + */ +public class MutableVariant extends Variant implements Serializable, Cloneable { + + private static final long serialVersionUID = 1L; + + public static MutableVariant ofInstance(Object instance) { + Binding binding = Bindings.getBindingUnchecked( instance.getClass() ); + return new MutableVariant(binding, instance); + } + + /** + * Constract a variant with a default value of empty record {} + */ + public MutableVariant() { + binding = VoidBinding.VOID_BINDING; + value = null; + } + + public MutableVariant(Variant v) { +// assert(isValid(binding, value)); + this.binding = v.getBinding(); + this.value = v.getValue(); + } + + public MutableVariant(Binding binding, Object value) { +// assert(isValid(binding, value)); + this.binding = binding; + this.value = value; + } + + /** + * Set value and binding from a variant. This method takes the references, + * and does not clone the value. + * + * @param v source variant + */ + public void setValue(Variant v) { + this.binding = v.getBinding(); + this.value = v.getValue(); + } + + public void setValue(Binding binding, Object newValue) { +// assert(isValid(binding, newValue)); + this.binding = binding; + this.value = newValue; + } + + public void readFrom(Binding binding, Object newValue) throws BindingException { + if (binding.isImmutable()) { + this.binding = binding; + this.value = newValue; + return; + } + + if (this.binding == binding) { + binding.readFrom(binding, newValue, this.value); + } else { + try { + this.value = binding.clone( newValue ); + this.binding = binding; + } catch (AdaptException e) { + throw new BindingException( e ); + } + } + } + + public MutableVariant clone() { + if (binding.isImmutable()) return new MutableVariant(binding, value); + Object newValue = Bindings.cloneUnchecked(value, binding, binding); + return new MutableVariant(binding, newValue); + } + + private void writeObject(java.io.ObjectOutputStream out) throws IOException { + Binding dataTypeBinding = Bindings.getBindingUnchecked( Datatype.class ); + Bindings.getSerializerUnchecked( dataTypeBinding ).serialize(type()); + Bindings.getSerializerUnchecked( binding ).serialize(value, out); + } + + private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { + Binding dataTypeBinding = Bindings.getBindingUnchecked( Datatype.class ); + Datatype type = (Datatype) Bindings.getSerializerUnchecked( dataTypeBinding ).deserialize((InputStream)in); + Binding binding = Bindings.getMutableBinding(type); + Object value = Bindings.getSerializerUnchecked( binding ).deserialize((InputStream)in); + this.binding = binding; + this.value = value; + } + + @SuppressWarnings("unused") + private void readObjectNoData() throws ObjectStreamException { + throw new InvalidObjectException("Don't know how to instantiate "+type()+" with no data"); + } + + public MutableVariant getComponent(ChildReference ref) throws AccessorConstructionException { + if ( ref == null ) return this; + JavaObject jo = (JavaObject) Accessors.getAccessor(this, ref); + return new MutableVariant( jo.getBinding(), jo.getObject() ); + } + + public void setComponent(ChildReference ref, Binding binding, Object value) throws AccessorException, AccessorConstructionException { + if ( ref == null ) { + setValue( binding, value ); + } + else { + JavaObject jo = (JavaObject) Accessors.getAccessor(this, ref); + jo.setValue( ref, binding, value ); + } + } +}