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.impl;
14 import java.lang.reflect.Array;
15 import java.util.IdentityHashMap;
16 import java.util.Iterator;
19 import org.simantics.databoard.binding.ArrayBinding;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.databoard.binding.DoubleBinding;
22 import org.simantics.databoard.binding.error.BindingException;
23 import org.simantics.databoard.binding.error.UnsupportedOperationException;
24 import org.simantics.databoard.type.ArrayType;
25 import org.simantics.databoard.type.DoubleType;
26 import org.simantics.databoard.type.NumberType;
27 import org.simantics.databoard.util.IdentityPair;
30 * Binds Double[] type to double[] Class.
32 public final class DoubleArrayBinding extends ArrayBinding {
34 public static DoubleArrayBinding createFrom(ArrayType type) {
35 return new DoubleArrayBinding(type, new DoubleBindingDefault((DoubleType) type.componentType));
38 public DoubleArrayBinding(ArrayType type, Binding componentBinding) {
39 super(type, componentBinding);
43 public Object create() {
48 public Object create(int length, Iterator<Object> values) {
49 double[] array = new double[length];
50 for(int i=0;i<length;++i)
51 array[i] = (Double)values.next();
55 public Object create(Object[] values) {
56 double[] result = new double[values.length];
57 for (int i=0; i<values.length; i++)
58 result[i] = (Double) values[i];
63 public Object createDefault()
64 throws BindingException
66 NumberType nt = (NumberType) type().componentType;
67 if (nt.getRange() == null) {
68 return new double[ type().minLength() ];
70 return super.createDefault();
74 public void readFrom(Binding srcBinding, Object src, Object dst)
75 throws BindingException {
77 ArrayBinding sb = (ArrayBinding) srcBinding;
78 // Src Component Binding
79 DoubleBinding scb = (DoubleBinding) sb.getComponentBinding();
81 double[] d = (double[]) dst;
82 if (d.length != sb.size(src)) throw new BindingException("double[] is length immutable");
84 for (int i=0; i<d.length; i++) {
85 d[i] = scb.getValue_( sb.get(src, i) );
90 public Object readFromTry(Binding srcBinding, Object src, Object dst) throws BindingException {
92 ArrayBinding sb = (ArrayBinding) srcBinding;
93 // Src Component Binding
94 DoubleBinding scb = (DoubleBinding) sb.getComponentBinding();
96 double[] d = (double[]) dst;
97 int srcSize = sb.size(src);
98 if (d.length != srcSize) d = new double[ srcSize ];
100 for (int i=0; i<d.length; i++) {
101 d[i] = scb.getValue_( sb.get(src, i) );
108 * Create an array object.
109 * Note! The argument is consumed (is used in the result).
112 * @return an object that contains the array
114 public Object create(double[] array) {
119 public Object get(Object array, int index) throws BindingException {
120 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", double[] expected");
121 return ((double[])array)[index];
125 public void getAll(Object array, Object[] result) throws BindingException {
126 double[] a = (double[])array;
127 for (int i=0; i<a.length; i++)
132 public void set(Object array, int index, Object value)
133 throws BindingException {
134 double[] a = (double[])array;
135 a[index] = (Double) value;
139 public int size(Object array) throws BindingException {
140 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", double[] expected");
141 return ((double[])array).length;
145 public boolean isInstance(Object obj) {
146 return obj instanceof double[];
150 public boolean isImmutable() {
154 public double[] getArray(Object array) throws BindingException {
155 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", double[] expected");
156 return (double[]) array;
160 public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
162 double[] array = (double[]) value;
163 for (int i=0; i<array.length; i++) {
164 double element = array[i];
165 long bits = Double.doubleToLongBits(element);
166 int elementHash = (int)(bits ^ (bits >>> 32));
167 result = 31*result + elementHash;
173 public int deepCompare(Object o1, Object o2,
174 Set<IdentityPair<Object, Object>> compareHistory)
175 throws BindingException {
176 double[] a1 = (double[]) o1;
177 double[] a2 = (double[]) o2;
182 if (dif!=0) return dif;
184 for (int i=0; i<l1; i++) {
187 dif = Double.compare(v1, v2);
188 if (dif!=0) return dif;
194 public void add(Object array, int index, Object element)
195 throws BindingException, IndexOutOfBoundsException {
196 throw new UnsupportedOperationException();
200 public void remove(Object array, int index, int count) throws BindingException {
201 throw new UnsupportedOperationException();
205 public void setSize(Object array, int newSize) throws BindingException {
206 int oldSize = Array.getLength(array);
207 if (oldSize==newSize) return;
208 throw new BindingException("double[] is length immutable");
212 public boolean isResizable() {