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.FloatBinding;
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.FloatType;
26 import org.simantics.databoard.type.NumberType;
27 import org.simantics.databoard.util.IdentityPair;
30 * Binds Float[] Type to float[] class.
32 public final class FloatArrayBinding extends ArrayBinding {
34 public static FloatArrayBinding createFrom(ArrayType type) {
35 return new FloatArrayBinding(type, new FloatBindingDefault( (FloatType) type.componentType ));
38 public FloatArrayBinding(ArrayType type, Binding componentBinding) {
39 super(type, componentBinding);
43 public Object create() {
48 public Object create(int length, Iterator<Object> values) {
49 float[] array = new float[length];
50 for(int i=0;i<length;++i)
51 array[i] = (Float)values.next();
55 public Object create(Object[] values) {
56 float[] result = new float[values.length];
57 for (int i=0; i<values.length; i++)
58 result[i] = (Float) values[i];
63 * Create an array object.
64 * Note! The argument is consumed (is used in the result).
67 * @return an object that contains the array
69 public Object create(float[] array) {
74 public Object createDefault()
75 throws BindingException
77 NumberType nt = (NumberType) type().componentType;
78 if (nt.getRange() == null) {
79 return new float[ type().minLength() ];
81 return super.createDefault();
85 public void readFrom(Binding srcBinding, Object src, Object dst)
86 throws BindingException {
88 ArrayBinding sb = (ArrayBinding) srcBinding;
89 // Src Component Binding
90 FloatBinding scb = (FloatBinding) sb.getComponentBinding();
92 float[] d = (float[]) dst;
93 if (d.length != sb.size(src)) throw new BindingException("float[] is length immutable");
95 for (int i=0; i<d.length; i++) {
96 d[i] = scb.getValue_( sb.get(src, i) );
101 public Object readFromTry(Binding srcBinding, Object src, Object dst)
102 throws BindingException {
104 ArrayBinding sb = (ArrayBinding) srcBinding;
105 // Src Component Binding
106 FloatBinding scb = (FloatBinding) sb.getComponentBinding();
108 float[] d = (float[]) dst;
109 int srcSize = sb.size(src);
110 if (d.length != srcSize) d = new float[ srcSize ];
112 for (int i=0; i<d.length; i++) {
113 d[i] = scb.getValue_( sb.get(src, i) );
121 public Object get(Object array, int index) throws BindingException {
122 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", byte[] expected");
123 return ((float[])array)[index];
127 public void getAll(Object array, Object[] result) throws BindingException {
128 float[] a = (float[])array;
129 for (int i=0; i<a.length; i++)
134 public void set(Object array, int index, Object value)
135 throws BindingException {
136 float[] a = (float[])array;
137 a[index] = (Float) value;
141 public int size(Object array) throws BindingException {
142 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", byte[] expected");
143 return ((float[])array).length;
147 public boolean isInstance(Object obj) {
148 return obj instanceof float[];
152 public boolean isImmutable() {
156 public float[] getArray(Object array) throws BindingException {
157 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", byte[] expected");
158 return (float[]) array;
162 public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
164 float[] array = (float[]) value;
165 for (int i=0; i<array.length; i++) {
166 float element = array[i];
167 long bits = Float.floatToIntBits(element);
168 int elementHash = (int)(bits ^ (bits >>> 32));
169 result = 31*result + elementHash;
175 public int deepCompare(Object o1, Object o2,
176 Set<IdentityPair<Object, Object>> compareHistory)
177 throws BindingException {
178 float[] a1 = (float[]) o1;
179 float[] a2 = (float[]) o2;
184 if (dif!=0) return dif;
186 for (int i=0; i<l1; i++) {
189 dif = Float.compare(v1, v2);
190 if (dif!=0) return dif;
196 public void add(Object array, int index, Object element)
197 throws BindingException, IndexOutOfBoundsException {
198 throw new UnsupportedOperationException();
202 public void remove(Object array, int index, int count) throws BindingException {
203 throw new UnsupportedOperationException();
207 public void setSize(Object array, int newSize) throws BindingException {
208 int oldSize = Array.getLength(array);
209 if (oldSize==newSize) return;
210 throw new BindingException("float[] is length immutable");
214 public boolean isResizable() {