X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Fimpl%2FLinkedListBinding.java;fp=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Fimpl%2FLinkedListBinding.java;h=04287ef07144b07ff9cbc70421ab042e03d688ec;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/LinkedListBinding.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/LinkedListBinding.java new file mode 100644 index 000000000..04287ef07 --- /dev/null +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/LinkedListBinding.java @@ -0,0 +1,201 @@ +/******************************************************************************* + * 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