]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/reflection/ConstantBinding.java
Improved Bindings.getBinding(Class) caching for Datatype.class
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / reflection / ConstantBinding.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.RecordBinding;
16 import org.simantics.databoard.binding.error.BindingException;
17 import org.simantics.databoard.type.Datatype;
18
19 public class ConstantBinding extends RecordBinding {
20
21     Object value;
22     
23     public ConstantBinding(Datatype type, Object constantValue) {
24         this.type = type;
25         this.componentBindings = new Binding[0];
26         this.value = constantValue;
27     }
28
29     @Override
30     public Object create(Object... value) throws BindingException {
31         return this.value;
32     }
33
34     @Override
35     public Object getComponent(Object obj, int index) throws BindingException {
36         throw new BindingException();
37     }
38
39     @Override
40     public Object createPartial() throws BindingException {
41         return value;
42     }
43
44     @Override
45     public void setComponents(Object obj, Object... value) throws BindingException {
46 //        throw new BindingException();
47     }
48     
49     @Override
50     public void setComponent(Object obj, int index, Object value)
51                 throws BindingException {
52 //        throw new BindingException();
53     }
54
55     @Override
56     public boolean isInstance(Object obj) {
57         return obj==value;
58     }
59     
60         @Override
61         public boolean isImmutable() {
62                 return true;
63         }
64         
65         @Override
66         protected boolean baseEquals( Object obj ) {
67             return super.baseEquals( obj ) && value == ((ConstantBinding)obj).value;
68         }
69         
70         @Override
71         public int baseHashCode() {
72             return super.baseHashCode() + 7 * value.hashCode(); 
73         }    
74 }
75