X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Fimpl%2FUnsignedByteBinding.java;fp=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Fimpl%2FUnsignedByteBinding.java;h=33225ddc70c2b495a50e95a12ec29ddd705661e2;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/UnsignedByteBinding.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/UnsignedByteBinding.java new file mode 100644 index 000000000..33225ddc7 --- /dev/null +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/UnsignedByteBinding.java @@ -0,0 +1,147 @@ +package org.simantics.databoard.binding.impl; + +import org.simantics.databoard.binding.ByteBinding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.primitives.UnsignedByte; +import org.simantics.databoard.type.ByteType; + +/** + * Binding of {@link UnsignedByte} to byte type. + * + * @author Toni Kalajainen + */ +public abstract class UnsignedByteBinding extends ByteBinding { + + UnsignedByteBinding(ByteType type) { + super(type); + } + + public static class Immutable extends UnsignedByteBinding { + + public Immutable(ByteType type) { + super(type); + } + + @Override + public Object create(Number value) throws BindingException { + try { + return UnsignedByte.valueOf(value.longValue()); + } catch (java.lang.IllegalArgumentException e) { + throw new BindingException( e ); + } + } + + @Override + public Object create(String value) throws BindingException { + try { + return UnsignedByte.valueOf( Long.valueOf(value) ); + } catch (java.lang.IllegalArgumentException e) { + throw new BindingException( e ); + } + } + + @Override + public void setValue(Object obj, Number value) throws BindingException { + throw new BindingException("UnsignedByte is immutable class"); + } + + @Override + public boolean isImmutable() { + return true; + } + + @Override + public boolean isInstance(Object obj) { + return obj instanceof UnsignedByte.Immutable; + } + + @Override + public Object create(byte value) throws BindingException { + try { + return UnsignedByte.valueOf(value); + } catch (java.lang.IllegalArgumentException e) { + throw new BindingException( e ); + } + } + + @Override + public Object create(Byte value) throws BindingException { + try { + return UnsignedByte.valueOf(value); + } catch (java.lang.IllegalArgumentException e) { + throw new BindingException( e ); + } + } + + @Override + public void setValue(Object obj, byte value) throws BindingException { + throw new BindingException("UnsignedByte is immutable class"); + } + + } + + public static class Mutable extends UnsignedByteBinding { + + public Mutable(ByteType type) { + super(type); + } + + @Override + public Object create(Number value) throws BindingException { + return new UnsignedByte.Mutable(value.longValue()); + } + + @Override + public Object create(String value) throws BindingException { + return new UnsignedByte.Mutable(value); + } + + @Override + public void setValue(Object obj, Number value) throws BindingException { + UnsignedByte.Mutable bb = (UnsignedByte.Mutable) obj; + bb.setBits(value.intValue()); + } + + @Override + public boolean isImmutable() { + return false; + } + + @Override + public boolean isInstance(Object obj) { + return obj instanceof UnsignedByte.Mutable; + } + + @Override + public Object create(byte value) throws BindingException { + return new UnsignedByte.Mutable(value); + } + + @Override + public Object create(Byte value) throws BindingException { + return new UnsignedByte.Mutable(value); + } + + @Override + public void setValue(Object obj, byte value) throws BindingException { + UnsignedByte.Mutable bb = (UnsignedByte.Mutable) obj; + bb.setBits(value); + } + } + + @Override + public Byte getValue(Object obj) throws BindingException { + return (byte) ((UnsignedByte)obj).toBits(); + } + + @Override + public byte getValue_(Object obj) throws BindingException { + return (byte) ((UnsignedByte)obj).toBits(); + } + + @Override + public String toString(Object value) throws BindingException { + return value.toString(); + } + +}