]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/reflection/EnumClassBinding.java
Fixing several binding-related bugs
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / reflection / EnumClassBinding.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
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
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding.reflection;
13
14 import org.simantics.databoard.binding.Binding;
15 import org.simantics.databoard.binding.UnionBinding;
16 import org.simantics.databoard.binding.error.BindingException;
17 import org.simantics.databoard.type.UnionType;
18
19 /**
20  * This class binds a java Enum-class with DataBoard's UnionType.
21  *
22  *
23  * @author Toni Kalajainen
24  */
25 public class EnumClassBinding extends UnionBinding {
26
27     Class<Enum<?>> clazz;
28     Enum<?>[] tags;
29     
30     public EnumClassBinding(UnionType type, Class<Enum<?>> clazz) {
31         this.type = type;
32         this.tags = clazz.getEnumConstants();
33         this.clazz = clazz;
34         
35         componentBindings = new Binding[ tags.length ];
36         for (int i=0; i<tags.length; i++) {
37             componentBindings[i] = new ConstantBinding(type.components[i].type, tags[i]);
38         }
39     }
40     
41     @Override
42     public Object create(int tag, Object value) {
43         return tags[tag];
44     }
45     
46     @Override
47     public void setValue(Object union, int tag, Object value)
48         throws BindingException {
49         throw new BindingException("Cannot change enum instance value");
50     }
51     
52     @Override
53     public int getTag(Object obj) throws BindingException {
54         for (int i=0; i<tags.length; i++)
55             if (obj==tags[i]) return i;
56                 throw new BindingException(obj+" is not an element of "+clazz.getSimpleName());
57     }
58     
59     @Override
60     public Object getValue(Object obj) {
61         return obj;
62     }
63     
64     @Override
65     public boolean isInstance(Object obj) {
66         return clazz.isInstance(obj);
67     }
68     
69     @Override
70     public boolean isImmutable() {
71         return true;
72     }
73
74     @Override
75     protected boolean baseEquals(Object obj) {
76         EnumClassBinding o = (EnumClassBinding)obj;
77         return super.baseEquals(obj) && o.clazz.equals(clazz);
78     }
79     
80     @Override
81     public int baseHashCode() {
82         return super.baseHashCode() + 13 * clazz.hashCode();
83     }
84 }