+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.binding.impl;
+
+import org.simantics.databoard.binding.ByteBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.error.UnsupportedOperationException;\r
+import org.simantics.databoard.type.ByteType;\r
+
+/**
+ * Binds ByteType to java.lang.Byte-class
+ *
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+public class ByteBindingDefault extends ByteBinding {
+
+ public ByteBindingDefault(ByteType type) {
+ super(type);
+ }
+
+ public Object create(byte value) {
+ return Byte.valueOf(value);
+ }
+
+ public Object create(Byte value) {
+ return value;
+ }
+
+ @Override
+ public Object create(Number value)
+ {
+ if (value.getClass()==Byte.class) return value;
+ return Byte.valueOf( value.byteValue() );
+ }
+
+ @Override
+ public Object create(String value) throws BindingException {
+ try {\r
+ return Byte.parseByte(value);\r
+ } catch (java.lang.NumberFormatException e) {\r
+ throw new BindingException( e );\r
+ }\r
+ }
+
+ public Byte getValue(Object obj)
+ throws BindingException
+ {
+ if (obj.getClass()!=Byte.class) throw new BindingException("Unexpected class "+obj.getClass().getSimpleName()+", Byte expected");
+ return ((Byte)obj);
+ }
+
+ public byte getValue_(Object obj)
+ throws BindingException
+ {
+ if (obj.getClass()!=Byte.class) throw new BindingException("Unexpected class "+obj.getClass().getSimpleName()+", Byte expected");
+ return ((Byte)obj);
+ }
+
+ @Override
+ public void setValue(Object obj, Number value) throws BindingException {
+ throw new UnsupportedOperationException("Cannot change the value of immutable java.lang.Byte");
+ }
+
+ public void setValue(Object obj, byte value) throws BindingException {
+ throw new UnsupportedOperationException("Cannot change the value of immutable java.lang.Byte");
+ }
+
+ @Override
+ public boolean isInstance(Object obj) {
+ return obj instanceof Byte;
+ }
+
+ @Override
+ public boolean isImmutable() {
+ return true;
+ }
+\r
+ @Override\r
+ public int compare(Object o1, Object o2)\r
+ {\r
+ Byte d1 = (Byte) o1;\r
+ Byte d2 = (Byte) o2;\r
+ int d = d1-d2;\r
+ return d == 0 ? 0 : (d<0?-1:1);\r
+ }\r
+ \r
+
+}
+