X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2FNumberBinding.java;fp=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2FNumberBinding.java;h=e5cf14e20e907ac7ce4df2539a4c4c2ad3426c56;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/NumberBinding.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/NumberBinding.java new file mode 100644 index 000000000..e5cf14e20 --- /dev/null +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/NumberBinding.java @@ -0,0 +1,139 @@ +/******************************************************************************* + * Copyright (c) 2010 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.databoard.binding; + +import java.util.Set; + +import org.simantics.databoard.accessor.reference.ChildReference; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.error.RuntimeBindingException; +import org.simantics.databoard.binding.impl.BindingPrintContext; +import org.simantics.databoard.type.NumberType; +import org.simantics.databoard.util.Range; + +/** + * Super class for number types. + * + * @author Toni Kalajainen + */ +public abstract class NumberBinding extends Binding { + + /** + * Create value by converting it from any Number instance to a Number + * instance of this Binding type. + * + * NOTE WARNING! Using this method may lose precision or value in the conversion. + * E.g. Double to Integer, or Long to Byte + * + * @param value + * @return the value in the format of the binding type + */ + public abstract Object create(Number value) + throws BindingException; + + /** + * Creates a value from its string representation + * @param value + * @return number + */ + public abstract Object create(String value) + throws BindingException; + + /** + * Get Data type + * + * @return data type + */ + @Override + public NumberType type() { + return (NumberType) type; + } + + /** + * Get numeric value of an object + * + * @param obj object + * @return Number + * @throws BindingException thrown if obj is incorrect class + */ + public Number getValue(Object obj) throws BindingException { + return ((Number)obj); + } + + public abstract void setValue(Object obj, Number value) throws BindingException; + + /** + * Assert the obj is a valid number. + * + * This asserts + * 1. The value is within the range defined in the NumberType + * + * @throws BindingException + */ + @Override + public void assertInstaceIsValid(Object obj, Set validInstances) throws BindingException { + if (!isInstance(obj)) throw new BindingException("Not a correct instance"); + NumberType type = type(); + Range range = type.getRange(); + if (range!=null) { + Number value = getValue(obj); + if (!range.contains(value)) { + throw new BindingException("Value ("+value+") out of range ("+range+")"); + } + } + } + + public Object createUnchecked(String value) throws RuntimeBindingException { + try { + return create(value); + } catch (BindingException e) { + return new RuntimeBindingException(e); + } + } + public Object createUnchecked(Number value) throws RuntimeBindingException { + try { + return create(value); + } catch (BindingException e) { + return new RuntimeBindingException(e); + } + } + + @Override + protected void toString(Object value, BindingPrintContext ctx) throws BindingException { + ctx.b.append(getValue(value).toString()); + } + + @Override + public void readFrom(Binding srcBinding, Object src, Object dst) + throws BindingException { + Number v = ((NumberBinding)srcBinding).getValue(src); + setValue(dst, v); + } + + @Override + public Binding getComponentBinding(ChildReference path) { + if (path==null) return this; + throw new IllegalArgumentException(); + } + + @Override + public int getComponentCount() { + return 0; + } + + @Override + public Binding getComponentBinding(int index) { + throw new IllegalArgumentException(); + } + +} +