]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/mutable/UnionTaggedObjectBinding.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / mutable / UnionTaggedObjectBinding.java
1 package org.simantics.databoard.binding.mutable;
2
3 import org.simantics.databoard.binding.Binding;
4 import org.simantics.databoard.binding.UnionBinding;
5 import org.simantics.databoard.binding.error.BindingException;
6 import org.simantics.databoard.type.UnionType;
7
8 /**
9  * Binds UnionType to TaggedObject.class
10  *
11  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
12  */
13 public class UnionTaggedObjectBinding extends UnionBinding {
14
15         public UnionTaggedObjectBinding(UnionType type, Binding[] componentBindings)
16         {
17                 this.componentBindings = componentBindings;
18         if (type==null) throw new IllegalArgumentException("null arg");
19                 this.type = type;
20         }
21
22         @Override
23         public Object create(int tag, Object value) {
24                 TaggedObject result = new TaggedObject();
25                 result.tag = tag;
26                 result.value = value;
27                 return result;
28         }
29         
30         @Override
31         public void setValue(Object union, int tag, Object value)
32         throws BindingException {
33                 TaggedObject result = (TaggedObject)union;
34                 result.tag = tag;
35                 result.value = value;
36         }
37
38         @Override
39         public int getTag(Object obj) throws BindingException {
40                 if (!isInstance(obj)) throw new BindingException("Unexpected class "+obj.getClass().getSimpleName()+", TaggedObject expected");
41                 TaggedObject to = (TaggedObject) obj;
42                 return to.tag;
43         }
44
45         @Override
46         public Object getValue(Object obj) throws BindingException {
47                 if (!isInstance(obj)) throw new BindingException("Unexpected class "+obj.getClass().getSimpleName()+", TaggedObject expected");
48                 TaggedObject to = (TaggedObject) obj;
49                 return to.value;
50         }
51
52         @Override
53         public boolean isInstance(Object obj) {
54                 return obj instanceof TaggedObject;
55         }
56         
57 }