1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.binding;
14 import java.util.IdentityHashMap;
17 import org.simantics.databoard.binding.error.BindingException;
18 import org.simantics.databoard.binding.error.RuntimeBindingException;
19 import org.simantics.databoard.type.ByteType;
20 import org.simantics.databoard.util.IdentityPair;
23 * This is a binding of Byte Type and a Java Object.
26 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
28 public abstract class ByteBinding extends NumberBinding {
30 public ByteBinding(ByteType type) {
35 public ByteType type() {
36 return (ByteType) type;
39 public abstract Object create(byte value) throws BindingException;
40 public abstract Object create(Byte value) throws BindingException;
41 public abstract Object create(String value) throws BindingException;
42 public abstract Object create(Number value) throws BindingException;
43 public abstract Byte getValue(Object obj) throws BindingException;
44 public abstract byte getValue_(Object obj) throws BindingException;
45 public abstract void setValue(Object obj, Number value) throws BindingException;
46 public abstract void setValue(Object obj, byte value) throws BindingException;
47 public abstract boolean isInstance(Object obj);
50 public void accept(Visitor1 v, Object obj) {
55 public <T> T accept(Visitor<T> v) {
61 public int deepCompare(Object o1, Object o2,
62 Set<IdentityPair<Object, Object>> compareHistory)
63 throws BindingException {
64 byte v1 = getValue_(o1);
65 byte v2 = getValue_(o2);
66 return (int)v1 - (int)v2;
70 public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
71 byte v = getValue_(value);
75 public Object createUnchecked(byte value) throws RuntimeBindingException {
78 } catch (BindingException e) {
79 return new RuntimeBindingException(e);
82 public Object createUnchecked(Byte value) throws RuntimeBindingException {
85 } catch (BindingException e) {
86 return new RuntimeBindingException(e);
92 public void assertInstaceIsValid(Object obj)
93 throws BindingException {
94 if (!isInstance(obj)) throw new BindingException("Not a boolean instance");
95 ByteType type = getDataType();
96 Byte min = type.getMin();
97 Byte max = type.getMax();
98 if (min!=null || max!=null) {
99 Byte value = getValue(obj);
100 if (min!=null && value<min) throw new BindingException("The value is("+value+") below the minimum ("+min+")");
101 if (max!=null && value>max) throw new BindingException("The value is("+value+") over the maximum ("+max+")");