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;
16 import org.simantics.databoard.accessor.reference.ChildReference;
17 import org.simantics.databoard.binding.error.BindingException;
18 import org.simantics.databoard.binding.error.RuntimeBindingException;
19 import org.simantics.databoard.binding.impl.BindingPrintContext;
20 import org.simantics.databoard.type.NumberType;
21 import org.simantics.databoard.util.Range;
24 * Super class for number types.
26 * @author Toni Kalajainen
28 public abstract class NumberBinding extends Binding {
31 * Create value by converting it from any Number instance to a Number
32 * instance of this Binding type.
34 * NOTE WARNING! Using this method may lose precision or value in the conversion.
35 * E.g. Double to Integer, or Long to Byte
38 * @return the value in the format of the binding type
40 public abstract Object create(Number value)
41 throws BindingException;
44 * Creates a value from its string representation
48 public abstract Object create(String value)
49 throws BindingException;
57 public NumberType type() {
58 return (NumberType) type;
62 * Get numeric value of an object
66 * @throws BindingException thrown if obj is incorrect class
68 public Number getValue(Object obj) throws BindingException {
72 public abstract void setValue(Object obj, Number value) throws BindingException;
75 * Assert the obj is a valid number.
78 * 1. The value is within the range defined in the NumberType
80 * @throws BindingException
83 public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
84 if (!isInstance(obj)) throw new BindingException("Not a correct instance");
85 NumberType type = type();
86 Range range = type.getRange();
88 Number value = getValue(obj);
89 if (!range.contains(value)) {
90 throw new BindingException("Value ("+value+") out of range ("+range+")");
95 public Object createUnchecked(String value) throws RuntimeBindingException {
98 } catch (BindingException e) {
99 return new RuntimeBindingException(e);
102 public Object createUnchecked(Number value) throws RuntimeBindingException {
104 return create(value);
105 } catch (BindingException e) {
106 return new RuntimeBindingException(e);
111 protected void toString(Object value, BindingPrintContext ctx) throws BindingException {
112 ctx.b.append(getValue(value).toString());
116 public void readFrom(Binding srcBinding, Object src, Object dst)
117 throws BindingException {
118 Number v = ((NumberBinding)srcBinding).getValue(src);
123 public Binding getComponentBinding(ChildReference path) {
124 if (path==null) return this;
125 throw new IllegalArgumentException();
129 public int getComponentCount() {
134 public Binding getComponentBinding(int index) {
135 throw new IllegalArgumentException();