]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/BooleanArrayBinding.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / BooleanArrayBinding.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.Iterator;
16 import java.util.Set;
17
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;
26
27 /**
28  * Binds the type Boolean[]-type to boolean[] Class.
29  */
30 public final class BooleanArrayBinding extends ArrayBinding {
31         
32         public static BooleanArrayBinding createFrom(ArrayType type) {
33                 return new BooleanArrayBinding(type, new BooleanBindingDefault((BooleanType) type.componentType));
34         }
35
36         public BooleanArrayBinding(ArrayType type, Binding componentBinding) {
37                 super(type, componentBinding);
38         }
39         
40         @Override
41         public Object create() {
42                 return new boolean[0];
43         }
44         
45         @Override
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();
50                 return array;
51         }
52         
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];
57                 return result;
58         }       
59
60         /**
61          * Create an array object.
62          * Note! The argument is consumed (is used in the result).
63          * 
64          * @param array
65          * @return an object that contains the array
66          */
67         public Object create(boolean[] array) {
68                 return array;
69         }
70         
71         @Override
72         public void readFrom(Binding srcBinding, Object src, Object dst)
73                         throws BindingException {
74                 // Src Binding
75                 ArrayBinding sb = (ArrayBinding) srcBinding;
76                 // Src Component Binding
77                 BooleanBinding scb = (BooleanBinding) sb.getComponentBinding();
78                 
79                 boolean[] d = (boolean[]) dst;
80                 if (d.length != sb.size(src)) throw new BindingException("boolean[] is length immutable");
81                 
82                 for (int i=0; i<d.length; i++) {
83                         d[i] = scb.getValue_( sb.get(src, i) );
84                 }
85         }
86         
87         @Override
88         public Object readFromTry(Binding srcBinding, Object src, Object dst)
89                         throws BindingException {
90                 // Src Binding
91                 ArrayBinding sb = (ArrayBinding) srcBinding;
92                 // Src Component Binding
93                 BooleanBinding scb = (BooleanBinding) sb.getComponentBinding();
94                 
95                 boolean[] d = (boolean[]) dst;
96                 int srcSize = sb.size(src); 
97                 if (d.length != srcSize) d = new boolean[ srcSize ];
98                 
99                 for (int i=0; i<d.length; i++) {
100                         d[i] = scb.getValue_( sb.get(src, i) );
101                 }
102                 
103                 return d;
104         }
105                 
106         @Override
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];
110         }
111
112         @Override
113         public void getAll(Object array, Object[] result) throws BindingException {
114                 boolean[] a = (boolean[])array;
115                 for (int i=0; i<a.length; i++)
116                         result[i] = a[i];
117         }
118         
119         @Override
120         public void set(Object array, int index, Object value)
121                         throws BindingException {
122                 boolean[] a = (boolean[])array;
123                 a[index] = (Boolean) value;
124         }
125         
126         @Override
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;
130         }
131
132         @Override
133         public boolean isInstance(Object obj) {
134                 return obj instanceof boolean[];
135         }
136         
137         @Override
138         public boolean isImmutable() {
139                 return false;
140         }
141
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;
145         }       
146         
147     @Override
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;
153                 // Compare Lengths
154                 int l1 = a1.length;
155                 int l2 = a2.length;
156                 int dif = l1 - l2;
157                 if (dif!=0) return dif;
158                 // Compare elements
159                 for (int i=0; i<l1; i++) {
160                         boolean v1 = a1[i];
161                         boolean v2 = a2[i];
162                         dif = (v2 == v1 ? 0 : (v1 ? 1 : -1));
163                         if (dif!=0) return dif;
164                 }
165                 return 0;
166     }    
167     
168     @Override
169     public void add(Object array, int index, Object element)
170                 throws BindingException, IndexOutOfBoundsException {
171         throw new UnsupportedOperationException();
172     }
173     
174         @Override
175         public void remove(Object array, int index, int count) throws BindingException {
176         throw new UnsupportedOperationException();
177         }
178
179         @Override
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");
184         }       
185
186         @Override
187         public boolean isResizable() {
188                 return false;
189         }
190         
191 }