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.util.Collection;
15 import java.util.IdentityHashMap;
16 import java.util.Iterator;
17 import java.util.LinkedList;
18 import java.util.List;
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;
28 * CollectionListBinding binds ArrayType to java.util.LinkedList
30 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
32 public class LinkedListBinding extends ArrayBinding {
34 public LinkedListBinding(ArrayType type, Binding componentBinding) {
35 super(type, componentBinding);
36 if (type==null) throw new IllegalArgumentException("null arg");
41 public Object create() {
42 return new LinkedList<Object>();
46 * Create new ArrayList
49 public Object create(int length, Iterator<Object> values) {
50 LinkedList<Object> result = new LinkedList<Object>();
51 while (values.hasNext()) result.add(values.next());
55 public Object create(Collection<Object> collection)
56 throws BindingException {
57 return new LinkedList<Object>( collection );
60 public Object create(Object[] values) {
61 LinkedList<Object> array = new LinkedList<Object>();
62 for (int i=0; i<values.length; i++)
67 @SuppressWarnings("unchecked")
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);
75 @SuppressWarnings("unchecked")
77 public void getAll(Object array, Object[] result) throws BindingException {
78 List<Object> list = (List<Object>) array;
80 for (Object o : list) {
85 @SuppressWarnings("unchecked")
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);
93 @SuppressWarnings("unchecked")
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;
102 public boolean isInstance(Object obj) {
103 return obj instanceof LinkedList<?>;
106 @SuppressWarnings("unchecked")
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);
114 @SuppressWarnings("unchecked")
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();
123 if (index==list.size()) {
127 Iterator<Object> iter = list.iterator();
128 for (int i=0; i<index; i++)
130 for (int i=0; i<count; i++) {
137 public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
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);
148 @SuppressWarnings("unchecked")
150 public int deepCompare(Object o1, Object o2,
151 Set<IdentityPair<Object, Object>> compareHistory)
152 throws BindingException {
157 if (dif!=0) return dif;
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;
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;
178 if (oldSize>newSize) {
179 for (int i=oldSize-1; i<=newSize; i--)
184 int c = newSize - oldSize;
185 for (int i=0; i<c; i++) {
186 list.add( componentBinding.createDefault() );
191 public boolean isImmutable() {
196 public boolean isResizable() {