]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/IntegerBindingDefault.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / IntegerBindingDefault.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.impl;
13
14 import org.simantics.databoard.binding.IntegerBinding;
15 import org.simantics.databoard.binding.error.BindingException;
16 import org.simantics.databoard.binding.error.UnsupportedOperationException;
17 import org.simantics.databoard.type.IntegerType;
18
19 /**
20  * Binds Integer Type to java.lang.Integer
21  *
22  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
23  */
24 public class IntegerBindingDefault extends IntegerBinding {
25
26         public IntegerBindingDefault(IntegerType type) {
27                 super( type );
28         }
29         
30         public Object create(int value) {
31                 return Integer.valueOf(value);
32         }
33         
34         public Object create(Integer value) {
35                 return value;
36         }
37                 
38         @Override
39         public Object create(Number value) 
40         {
41                 if (value.getClass()==Integer.class) return value;
42                 return Integer.valueOf( value.intValue() );
43         }
44
45         @Override
46         public Object create(String value) throws BindingException {
47                 try {
48                         return Integer.parseInt(value);
49                 } catch (java.lang.NumberFormatException e) {
50                         throw new BindingException( e );
51                 }
52         }
53                         
54         public Integer getValue(Object obj) 
55         throws BindingException
56         {
57                 if (obj.getClass()!=Integer.class)
58                         throw new BindingException("Unexpected class "+obj.getClass().getSimpleName()+", Integer expected");            
59                 return ((Integer)obj);
60         }
61         
62         public int getValue_(Object obj)
63         throws BindingException
64         {
65                 if (obj.getClass()!=Integer.class)
66                         throw new BindingException("Unexpected class "+obj.getClass().getSimpleName()+", Integer expected");            
67                 return ((Integer)obj);
68         }
69
70         @Override
71         public void setValue(Object obj, Number value) throws BindingException {
72                 throw new UnsupportedOperationException("Cannot change the value of immutable java.lang.Integer");
73         }
74
75         public void setValue(Object obj, int value) throws BindingException {
76                 throw new UnsupportedOperationException("Cannot change the value of immutable java.lang.Integer");
77         }
78         
79         @Override
80         public boolean isInstance(Object obj) {
81                 return obj instanceof Integer;
82         }       
83         
84         @Override
85         public boolean isImmutable() {
86                 return true;
87         }
88         
89     @Override
90     public int compare(Object o1, Object o2)
91     {
92         int i1 = (Integer) o1;
93         int i2 = (Integer) o2;
94         return (i1<i2 ? -1 : (i1==i2 ? 0 : 1));
95     }
96         
97         
98 }
99