]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/FloatArrayBinding.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / FloatArrayBinding.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
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
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding.impl;
13
14 import java.lang.reflect.Array;
15 import java.util.IdentityHashMap;
16 import java.util.Iterator;
17 import java.util.Set;
18
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;
28
29 /**
30  * Binds Float[] Type to float[] class.
31  */
32 public final class FloatArrayBinding extends ArrayBinding {
33
34         public static FloatArrayBinding createFrom(ArrayType type) {
35                 return new FloatArrayBinding(type, new FloatBindingDefault( (FloatType) type.componentType ));
36         }
37
38         public FloatArrayBinding(ArrayType type, Binding componentBinding) {
39                 super(type, componentBinding);
40         }
41
42         @Override
43         public Object create() {
44                 return new float[0];
45         }
46         
47         @Override
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();
52                 return array;
53         }
54         
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];
59                 return result;
60         }               
61         
62         /**
63          * Create an array object.
64          * Note! The argument is consumed (is used in the result).
65          * 
66          * @param array
67          * @return an object that contains the array
68          */
69         public Object create(float[] array) {
70                 return array;
71         }
72
73         @Override
74     public Object createDefault()
75     throws BindingException
76     {
77                 NumberType nt = (NumberType) type().componentType;
78                 if (nt.getRange() == null) {
79                 return new float[ type().minLength() ];
80                 }
81                 return super.createDefault();
82     }   
83         
84         @Override
85         public void readFrom(Binding srcBinding, Object src, Object dst)
86                         throws BindingException {
87                 // Src Binding
88                 ArrayBinding sb = (ArrayBinding) srcBinding;
89                 // Src Component Binding
90                 FloatBinding scb = (FloatBinding) sb.getComponentBinding();
91                 
92                 float[] d = (float[]) dst;
93                 if (d.length != sb.size(src)) throw new BindingException("float[] is length immutable");
94                 
95                 for (int i=0; i<d.length; i++) {
96                         d[i] = scb.getValue_( sb.get(src, i) );
97                 }
98         }
99         
100         @Override
101         public Object readFromTry(Binding srcBinding, Object src, Object dst)
102                         throws BindingException {
103                 // Src Binding
104                 ArrayBinding sb = (ArrayBinding) srcBinding;
105                 // Src Component Binding
106                 FloatBinding scb = (FloatBinding) sb.getComponentBinding();
107                 
108                 float[] d = (float[]) dst;
109                 int srcSize = sb.size(src);
110                 if (d.length != srcSize) d = new float[ srcSize ];
111                 
112                 for (int i=0; i<d.length; i++) {
113                         d[i] = scb.getValue_( sb.get(src, i) );
114                 }
115                 
116                 return d;
117         }       
118                 
119         
120         @Override
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];
124         }
125
126         @Override
127         public void getAll(Object array, Object[] result) throws BindingException {
128                 float[] a = (float[])array;
129                 for (int i=0; i<a.length; i++)
130                         result[i] = a[i];
131         }
132         
133         @Override
134         public void set(Object array, int index, Object value)
135                         throws BindingException {
136                 float[] a = (float[])array;
137                 a[index] = (Float) value;
138         }       
139         
140         @Override
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;
144         }
145
146         @Override
147         public boolean isInstance(Object obj) {
148                 return obj instanceof float[];
149         }
150         
151         @Override
152         public boolean isImmutable() {
153                 return false;
154         }
155         
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;
159         }       
160         
161     @Override
162     public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
163         int result = 1;
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;
170         }
171         return result;
172     }
173
174     @Override
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;
180                 // Compare Lengths
181                 int l1 = a1.length;
182                 int l2 = a2.length;
183                 int dif = l1 - l2;
184                 if (dif!=0) return dif;
185                 // Compare elements
186                 for (int i=0; i<l1; i++) {
187                         float v1 = a1[i];
188                         float v2 = a2[i];
189                         dif = Float.compare(v1, v2);
190                         if (dif!=0) return dif;
191                 }
192                 return 0;
193     }    
194
195     @Override
196     public void add(Object array, int index, Object element)
197                 throws BindingException, IndexOutOfBoundsException {
198         throw new UnsupportedOperationException();
199     }
200     
201         @Override
202         public void remove(Object array, int index, int count) throws BindingException {
203         throw new UnsupportedOperationException();
204         }
205     
206         @Override
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");
211         }       
212
213         @Override
214         public boolean isResizable() {
215                 return false;
216         }
217         
218 }