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.Iterator;
18 import org.simantics.databoard.binding.ArrayBinding;
19 import org.simantics.databoard.binding.Binding;
20 import org.simantics.databoard.binding.BooleanBinding;
21 import org.simantics.databoard.binding.error.BindingException;
22 import org.simantics.databoard.binding.error.UnsupportedOperationException;
23 import org.simantics.databoard.type.ArrayType;
24 import org.simantics.databoard.type.BooleanType;
25 import org.simantics.databoard.util.IdentityPair;
28 * Binds the type Boolean[]-type to boolean[] Class.
30 public final class BooleanArrayBinding extends ArrayBinding {
32 public static BooleanArrayBinding createFrom(ArrayType type) {
33 return new BooleanArrayBinding(type, new BooleanBindingDefault((BooleanType) type.componentType));
36 public BooleanArrayBinding(ArrayType type, Binding componentBinding) {
37 super(type, componentBinding);
41 public Object create() {
42 return new boolean[0];
46 public Object create(int length, Iterator<Object> values) {
47 boolean[] array = new boolean[length];
48 for(int i=0;i<length;++i)
49 array[i] = (Boolean)values.next();
53 public Object create(Object[] values) {
54 boolean[] result = new boolean[values.length];
55 for (int i=0; i<values.length; i++)
56 result[i] = (Boolean) values[i];
61 * Create an array object.
62 * Note! The argument is consumed (is used in the result).
65 * @return an object that contains the array
67 public Object create(boolean[] array) {
72 public void readFrom(Binding srcBinding, Object src, Object dst)
73 throws BindingException {
75 ArrayBinding sb = (ArrayBinding) srcBinding;
76 // Src Component Binding
77 BooleanBinding scb = (BooleanBinding) sb.getComponentBinding();
79 boolean[] d = (boolean[]) dst;
80 if (d.length != sb.size(src)) throw new BindingException("boolean[] is length immutable");
82 for (int i=0; i<d.length; i++) {
83 d[i] = scb.getValue_( sb.get(src, i) );
88 public Object readFromTry(Binding srcBinding, Object src, Object dst)
89 throws BindingException {
91 ArrayBinding sb = (ArrayBinding) srcBinding;
92 // Src Component Binding
93 BooleanBinding scb = (BooleanBinding) sb.getComponentBinding();
95 boolean[] d = (boolean[]) dst;
96 int srcSize = sb.size(src);
97 if (d.length != srcSize) d = new boolean[ srcSize ];
99 for (int i=0; i<d.length; i++) {
100 d[i] = scb.getValue_( sb.get(src, i) );
107 public Object get(Object array, int index) throws BindingException {
108 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", boolean[] expected");
109 return ((boolean[])array)[index];
113 public void getAll(Object array, Object[] result) throws BindingException {
114 boolean[] a = (boolean[])array;
115 for (int i=0; i<a.length; i++)
120 public void set(Object array, int index, Object value)
121 throws BindingException {
122 boolean[] a = (boolean[])array;
123 a[index] = (Boolean) value;
127 public int size(Object array) throws BindingException {
128 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", boolean[] expected");
129 return ((boolean[])array).length;
133 public boolean isInstance(Object obj) {
134 return obj instanceof boolean[];
138 public boolean isImmutable() {
142 public boolean[] getArray(Object array) throws BindingException {
143 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", boolean[] expected");
144 return (boolean[]) array;
148 public int deepCompare(Object o1, Object o2,
149 Set<IdentityPair<Object, Object>> compareHistory)
150 throws BindingException {
151 boolean[] a1 = (boolean[]) o1;
152 boolean[] a2 = (boolean[]) o2;
157 if (dif!=0) return dif;
159 for (int i=0; i<l1; i++) {
162 dif = (v2 == v1 ? 0 : (v1 ? 1 : -1));
163 if (dif!=0) return dif;
169 public void add(Object array, int index, Object element)
170 throws BindingException, IndexOutOfBoundsException {
171 throw new UnsupportedOperationException();
175 public void remove(Object array, int index, int count) throws BindingException {
176 throw new UnsupportedOperationException();
180 public void setSize(Object array, int newSize) throws BindingException {
181 int oldSize = Array.getLength(array);
182 if (oldSize==newSize) return;
183 throw new BindingException("boolean[] is length immutable");
187 public boolean isResizable() {