package org.simantics.databoard.binding.mutable; import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.UnionBinding; import org.simantics.databoard.binding.error.BindingException; import org.simantics.databoard.type.UnionType; /** * Binds UnionType to TaggedObject.class * * @author Toni Kalajainen */ public class UnionTaggedObjectBinding extends UnionBinding { public UnionTaggedObjectBinding(UnionType type, Binding[] componentBindings) { this.componentBindings = componentBindings; if (type==null) throw new IllegalArgumentException("null arg"); this.type = type; } @Override public Object create(int tag, Object value) { TaggedObject result = new TaggedObject(); result.tag = tag; result.value = value; return result; } @Override public void setValue(Object union, int tag, Object value) throws BindingException { TaggedObject result = (TaggedObject)union; result.tag = tag; result.value = value; } @Override public int getTag(Object obj) throws BindingException { if (!isInstance(obj)) throw new BindingException("Unexpected class "+obj.getClass().getSimpleName()+", TaggedObject expected"); TaggedObject to = (TaggedObject) obj; return to.tag; } @Override public Object getValue(Object obj) throws BindingException { if (!isInstance(obj)) throw new BindingException("Unexpected class "+obj.getClass().getSimpleName()+", TaggedObject expected"); TaggedObject to = (TaggedObject) obj; return to.value; } @Override public boolean isInstance(Object obj) { return obj instanceof TaggedObject; } }