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.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;
30 * Binds Byte[] type to byte[] class.
32 public final class ByteArrayBinding extends ArrayBinding {
34 public static ByteArrayBinding createFrom(ArrayType type) {
35 return new ByteArrayBinding(type, new ByteBindingDefault((ByteType) type.componentType));
38 public ByteArrayBinding(ArrayType type, Binding componentBinding) {
39 super(type, componentBinding);
43 public Object create() {
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();
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];
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(byte[] array) {
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++)
82 public Object createDefault()
83 throws BindingException
85 NumberType nt = (NumberType) type().componentType;
86 if (nt.getRange() == null) {
87 return new byte[ type().minLength() ];
89 return super.createDefault();
94 public void readFrom(Binding srcBinding, Object src, Object dst)
95 throws BindingException {
97 ArrayBinding sb = (ArrayBinding) srcBinding;
98 // Src Component Binding
99 ByteBinding scb = (ByteBinding) sb.getComponentBinding();
101 byte[] d = (byte[]) dst;
102 if (d.length != sb.size(src)) throw new BindingException("byte[] is length immutable");
104 for (int i=0; i<d.length; i++) {
105 d[i] = scb.getValue_( sb.get(src, i) );
110 public Object readFromTry(Binding srcBinding, Object src, Object dst)
111 throws BindingException {
113 ArrayBinding sb = (ArrayBinding) srcBinding;
114 // Src Component Binding
115 ByteBinding scb = (ByteBinding) sb.getComponentBinding();
117 byte[] d = (byte[]) dst;
118 int srcSize = sb.size(src);
119 if (d.length != srcSize) d = new byte[ srcSize ];
121 for (int i=0; i<d.length; i++) {
122 d[i] = scb.getValue_( sb.get(src, i) );
128 public Object get(Object array, int index) throws BindingException
130 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", byte[] expected");
131 return ((byte[])array)[index];
135 public void getAll(Object array, Object[] result) throws BindingException {
136 byte[] a = (byte[])array;
137 for (int i=0; i<a.length; i++)
142 public void set(Object array, int index, Object value)
143 throws BindingException {
144 byte[] a = (byte[])array;
145 a[index] = (Byte) value;
149 public int size(Object array)
150 throws BindingException
152 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", byte[] expected");
153 return ((byte[])array).length;
157 public boolean isInstance(Object obj) {
158 return obj instanceof byte[];
162 public boolean isImmutable() {
166 public byte[] getArray(Object array)
167 throws BindingException
169 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", byte[] expected");
170 return (byte[]) array;
174 public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
176 byte[] array = (byte[]) value;
177 for (int i=0; i<array.length; i++) {
178 result = 31*result + (int)array[i];
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;
193 if (dif!=0) return dif;
195 for (int i=0; i<l1; i++) {
197 if (dif!=0) return dif;
203 public void add(Object array, int index, Object element)
204 throws BindingException, IndexOutOfBoundsException {
205 throw new UnsupportedOperationException();
209 public void remove(Object array, int index, int count) throws BindingException {
210 throw new UnsupportedOperationException();
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");
221 public boolean isResizable() {