]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/LinkedListBinding.java
Fixing several binding-related bugs
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / LinkedListBinding.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.util.Collection;
15 import java.util.IdentityHashMap;
16 import java.util.Iterator;
17 import java.util.LinkedList;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.simantics.databoard.binding.ArrayBinding;
22 import org.simantics.databoard.binding.Binding;
23 import org.simantics.databoard.binding.error.BindingException;
24 import org.simantics.databoard.type.ArrayType;
25 import org.simantics.databoard.util.IdentityPair;
26
27 /**
28  * CollectionListBinding binds ArrayType to java.util.LinkedList
29  *
30  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
31  */
32 public class LinkedListBinding extends ArrayBinding {
33
34         public LinkedListBinding(ArrayType type, Binding componentBinding) {
35                 super(type, componentBinding);
36                 if (type==null) throw new IllegalArgumentException("null arg");
37                 this.type = type;
38         }
39         
40         @Override
41         public Object create() {
42                 return new LinkedList<Object>();
43         }
44         
45         /**
46          * Create new ArrayList
47          */
48         @Override
49         public Object create(int length, Iterator<Object> values) {
50                 LinkedList<Object> result = new LinkedList<Object>(); 
51                 while (values.hasNext()) result.add(values.next());             
52                 return result;
53         }
54         
55     public Object create(Collection<Object> collection)
56     throws BindingException {
57         return new LinkedList<Object>( collection );
58     }   
59
60         public Object create(Object[] values) {
61                 LinkedList<Object> array = new LinkedList<Object>();
62                 for (int i=0; i<values.length; i++)
63                         array.add(values[i]);
64                 return array;
65         }
66         
67         @SuppressWarnings("unchecked")
68         @Override
69         public Object get(Object array, int index) throws BindingException {
70                 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", java.util.List expected");
71                 List<Object> list = (List<Object>) array;
72                 return list.get(index);
73         }
74         
75         @SuppressWarnings("unchecked")
76     @Override
77         public void getAll(Object array, Object[] result) throws BindingException {
78                 List<Object> list = (List<Object>) array;
79                 int index = 0;
80                 for (Object o : list) {
81                         result[index++] = o;
82                 }
83         }
84         
85         @SuppressWarnings("unchecked")
86     @Override
87         public void set(Object array, int index, Object value)
88                         throws BindingException {
89                 List<Object> list = (List<Object>) array;
90                 list.set(index, value);
91         }
92
93         @SuppressWarnings("unchecked")
94         @Override
95         public int size(Object array) throws BindingException {         
96                 if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", java.util.List expected");           
97                 List<Object> list = (List<Object>) array;
98                 return list.size();
99         }
100
101         @Override
102         public boolean isInstance(Object obj) {
103                 return obj instanceof LinkedList<?>;
104         }       
105         
106     @SuppressWarnings("unchecked")
107     @Override
108     public void add(Object array, int index, Object element)
109                 throws BindingException, IndexOutOfBoundsException {
110                 List<Object> list = (List<Object>) array;
111                 list.add(index, element);
112     }   
113         
114         @SuppressWarnings("unchecked")
115         @Override
116         public void remove(Object array, int index, int count) throws BindingException {
117                 LinkedList<Object> list = (LinkedList<Object>) array;
118                 if (index<0 || index>=list.size()) throw new IndexOutOfBoundsException();
119                 if (index==0) {
120                         list.removeFirst();
121                         return;
122                 }
123                 if (index==list.size()) {
124                         list.removeLast();
125                         return;
126                 }
127                 Iterator<Object> iter = list.iterator();
128                 for (int i=0; i<index; i++)
129                         iter.next();
130                 for (int i=0; i<count; i++) {
131                         iter.next();
132                         iter.remove();
133                 }
134         }
135         
136     @Override
137     public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
138         int result = 1;
139         LinkedList<?> list = (LinkedList<?>) value;
140         Iterator<?> iter = list.iterator();
141         while (iter.hasNext()) {
142                 Object element = iter.next();
143                 result = 31*result + componentBinding.deepHashValue(element, hashedObjects);
144         }
145         return result;
146     }
147     
148     @SuppressWarnings("unchecked")
149     @Override
150     public int deepCompare(Object o1, Object o2,
151                 Set<IdentityPair<Object, Object>> compareHistory)
152                 throws BindingException {
153                 // Compare Lengths
154                 int l1 = size(o1);
155                 int l2 = size(o2);
156                 int dif = l1 - l2;
157                 if (dif!=0) return dif;
158                 // Compare elements
159                 Binding c = getComponentBinding();
160                 Iterator<Object> i1 = ((LinkedList<Object>) o1).iterator();
161                 Iterator<Object> i2 = ((LinkedList<Object>) o2).iterator();
162                 while(i1.hasNext()) {
163                         Object e1 = i1.next();
164                         Object e2 = i2.next();                  
165                         dif = c.deepCompare(e1, e2, compareHistory);
166                         if (dif!=0) return dif;
167                 }
168                 return 0;
169     }    
170     
171         @Override
172         public void setSize(Object array, int newSize) throws BindingException {
173                 @SuppressWarnings("unchecked")
174                 List<Object> list = (List<Object>) array;
175                 int oldSize = list.size();
176                 if (oldSize==newSize) return;
177                 
178                 if (oldSize>newSize) {
179                         for (int i=oldSize-1; i<=newSize; i--)
180                                 list.remove(i);
181                         return;
182                 } 
183                 
184                 int c = newSize - oldSize;
185                 for (int i=0; i<c; i++) {
186                         list.add( componentBinding.createDefault() );
187                 }
188         }               
189         
190         @Override
191         public boolean isImmutable() {
192                 return false;
193         }
194
195         @Override
196         public boolean isResizable() {
197                 return true;
198         }
199         
200 }
201