]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/ByteArrayBinding.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / ByteArrayBinding.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.ByteBinding;
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.ByteType;
26 import org.simantics.databoard.type.NumberType;
27 import org.simantics.databoard.util.IdentityPair;
28
29 /**
30  * Binds Byte[] type to byte[] class.
31  */
32 public final class ByteArrayBinding extends ArrayBinding {
33         
34         public static ByteArrayBinding createFrom(ArrayType type) {
35                 return new ByteArrayBinding(type, new ByteBindingDefault((ByteType) type.componentType));
36         }
37
38         public ByteArrayBinding(ArrayType type, Binding componentBinding) {
39                 super(type, componentBinding);
40         }
41         
42         @Override
43         public Object create() {
44                 return new byte[0];
45         }
46         
47         @Override
48         public Object create(int length, Iterator<Object> values) {
49                 byte[] array = new byte[length];
50                 for(int i=0;i<length;++i)
51                         array[i] = (Byte)values.next();
52                 return array;
53         }
54
55         public Object create(Object[] values) {
56                 byte[] result = new byte[values.length];
57                 for (int i=0; i<values.length; i++)
58                         result[i] = (Byte) 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(byte[] array) {
70                 return array;
71         }
72
73         public Object create(Byte[] array) {
74                 int len = array.length;
75                 byte[] result = new byte[len];
76                 for (int i=0; i<len; i++)
77                         result[i] = array[i];
78                 return result;
79         }
80         
81         @Override
82     public Object createDefault()
83     throws BindingException
84     {
85                 NumberType nt = (NumberType) type().componentType;
86                 if (nt.getRange() == null) {
87                 return new byte[ type().minLength() ];
88                 }
89                 return super.createDefault();
90     }   
91         
92
93         @Override
94         public void readFrom(Binding srcBinding, Object src, Object dst)
95                         throws BindingException {
96                 // Src Binding
97                 ArrayBinding sb = (ArrayBinding) srcBinding;
98                 // Src Component Binding
99                 ByteBinding scb = (ByteBinding) sb.getComponentBinding();
100                 
101                 byte[] d = (byte[]) dst;
102                 if (d.length != sb.size(src)) throw new BindingException("byte[] is length immutable");
103                 
104                 for (int i=0; i<d.length; i++) {
105                         d[i] = scb.getValue_( sb.get(src, i) );
106                 }
107         }       
108         
109         @Override
110         public Object readFromTry(Binding srcBinding, Object src, Object dst)
111                         throws BindingException {
112                 // Src Binding
113                 ArrayBinding sb = (ArrayBinding) srcBinding;
114                 // Src Component Binding
115                 ByteBinding scb = (ByteBinding) sb.getComponentBinding();
116                 
117                 byte[] d = (byte[]) dst;
118                 int srcSize = sb.size(src);
119                 if (d.length != srcSize) d = new byte[ srcSize ];
120                 
121                 for (int i=0; i<d.length; i++) {
122                         d[i] = scb.getValue_( sb.get(src, i) );
123                 }
124                 return d; 
125         }               
126         
127         @Override
128         public Object get(Object array, int index) throws BindingException 
129         {
130                 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", byte[] expected");
131                 return ((byte[])array)[index];
132         }
133         
134         @Override
135         public void getAll(Object array, Object[] result) throws BindingException {
136                 byte[] a = (byte[])array;
137                 for (int i=0; i<a.length; i++)
138                         result[i] = a[i];
139         }
140
141         @Override
142         public void set(Object array, int index, Object value)
143                         throws BindingException {
144                 byte[] a = (byte[])array;
145                 a[index] = (Byte) value;
146         }
147
148         @Override
149         public int size(Object array) 
150         throws BindingException 
151         {
152                 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", byte[] expected");
153                 return ((byte[])array).length;
154         }
155
156         @Override
157         public boolean isInstance(Object obj) {
158                 return obj instanceof byte[];
159         }
160         
161         @Override
162         public boolean isImmutable() {
163                 return false;
164         }       
165         
166         public byte[] getArray(Object array)
167         throws BindingException
168         {
169                 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", byte[] expected");
170                 return (byte[]) array;
171         }
172
173     @Override
174     public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
175         int result = 1;
176         byte[] array = (byte[]) value;
177         for (int i=0; i<array.length; i++) {
178                 result = 31*result + (int)array[i];
179         }
180         return result;
181     }
182     
183     @Override
184     public int deepCompare(Object o1, Object o2,
185                 Set<IdentityPair<Object, Object>> compareHistory)
186                 throws BindingException {
187         byte[] b1 = (byte[]) o1;
188         byte[] b2 = (byte[]) o2;
189                 // Compare Lengths
190                 int l1 = b1.length;
191                 int l2 = b2.length;
192                 int dif = l1 - l2;
193                 if (dif!=0) return dif;
194                 // Compare elements
195                 for (int i=0; i<l1; i++) {
196                         dif = b1[i] - b2[i];
197                         if (dif!=0) return dif;
198                 }
199                 return 0;
200     }    
201
202     @Override
203     public void add(Object array, int index, Object element)
204                 throws BindingException, IndexOutOfBoundsException {
205         throw new UnsupportedOperationException();
206     }
207     
208         @Override
209         public void remove(Object array, int index, int count) throws BindingException {
210         throw new UnsupportedOperationException();
211         }
212     
213         @Override
214         public void setSize(Object array, int newSize) throws BindingException {
215                 int oldSize = Array.getLength(array);
216                 if (oldSize==newSize) return;
217                 throw new BindingException("byte[] is length immutable");
218         }
219
220         @Override
221         public boolean isResizable() {
222                 return false;
223         }
224         
225 }