/******************************************************************************* * Copyright (c) 2010 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.databoard.binding.impl; import java.util.Collection; import java.util.IdentityHashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Set; import org.simantics.databoard.binding.ArrayBinding; import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.error.BindingException; import org.simantics.databoard.type.ArrayType; import org.simantics.databoard.util.IdentityPair; /** * CollectionListBinding binds ArrayType to java.util.LinkedList * * @author Toni Kalajainen */ public class LinkedListBinding extends ArrayBinding { public LinkedListBinding(ArrayType type, Binding componentBinding) { super(type, componentBinding); if (type==null) throw new IllegalArgumentException("null arg"); this.type = type; } @Override public Object create() { return new LinkedList(); } /** * Create new ArrayList */ @Override public Object create(int length, Iterator values) { LinkedList result = new LinkedList(); while (values.hasNext()) result.add(values.next()); return result; } public Object create(Collection collection) throws BindingException { return new LinkedList( collection ); } public Object create(Object[] values) { LinkedList array = new LinkedList(); for (int i=0; i list = (List) array; return list.get(index); } @SuppressWarnings("unchecked") @Override public void getAll(Object array, Object[] result) throws BindingException { List list = (List) array; int index = 0; for (Object o : list) { result[index++] = o; } } @SuppressWarnings("unchecked") @Override public void set(Object array, int index, Object value) throws BindingException { List list = (List) array; list.set(index, value); } @SuppressWarnings("unchecked") @Override public int size(Object array) throws BindingException { if (!isInstance(array)) throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", java.util.List expected"); List list = (List) array; return list.size(); } @Override public boolean isInstance(Object obj) { return obj instanceof LinkedList; } @SuppressWarnings("unchecked") @Override public void add(Object array, int index, Object element) throws BindingException, IndexOutOfBoundsException { List list = (List) array; list.add(index, element); } @SuppressWarnings("unchecked") @Override public void remove(Object array, int index, int count) throws BindingException { LinkedList list = (LinkedList) array; if (index<0 || index>=list.size()) throw new IndexOutOfBoundsException(); if (index==0) { list.removeFirst(); return; } if (index==list.size()) { list.removeLast(); return; } Iterator iter = list.iterator(); for (int i=0; i hashedObjects) throws BindingException { int result = 1; LinkedList list = (LinkedList) value; Iterator iter = list.iterator(); while (iter.hasNext()) { Object element = iter.next(); result = 31*result + componentBinding.deepHashValue(element, hashedObjects); } return result; } @SuppressWarnings("unchecked") @Override public int deepCompare(Object o1, Object o2, Set> compareHistory) throws BindingException { // Compare Lengths int l1 = size(o1); int l2 = size(o2); int dif = l1 - l2; if (dif!=0) return dif; // Compare elements Binding c = getComponentBinding(); Iterator i1 = ((LinkedList) o1).iterator(); Iterator i2 = ((LinkedList) o2).iterator(); while(i1.hasNext()) { Object e1 = i1.next(); Object e2 = i2.next(); dif = c.deepCompare(e1, e2, compareHistory); if (dif!=0) return dif; } return 0; } @Override public void setSize(Object array, int newSize) throws BindingException { @SuppressWarnings("unchecked") List list = (List) array; int oldSize = list.size(); if (oldSize==newSize) return; if (oldSize>newSize) { for (int i=oldSize-1; i<=newSize; i--) list.remove(i); return; } int c = newSize - oldSize; for (int i=0; i