/******************************************************************************* * 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.reflection; import java.lang.reflect.Array; import java.util.Iterator; import org.simantics.databoard.Bindings; import org.simantics.databoard.adapter.AdaptException; import org.simantics.databoard.adapter.Adapter; import org.simantics.databoard.adapter.AdapterConstructionException; import org.simantics.databoard.binding.ArrayBinding; import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.error.BindingException; import org.simantics.databoard.binding.error.UnsupportedOperationException; import org.simantics.databoard.type.ArrayType; /** * ReflectionArrayBinding binds T[] * * @author Toni Kalajainen */ public class GenericArrayBinding extends ArrayBinding { Class clazz; Class componentClass; public GenericArrayBinding(Class clazz, ArrayType type, Binding componentBinding) { super(type, componentBinding); this.clazz = clazz; this.type = type; this.componentClass = clazz.getComponentType(); } @Override public Object create() { return Array.newInstance(componentClass, 0); } @Override public Object create(int length, Iterator values) { Object result = Array.newInstance(componentClass, length); for (int i=0; iArray.getLength(array)) throw new BindingException("Index out of bounds"); Array.set(array, index, value); } @Override public int size(Object array) throws BindingException { if (!isInstance(array)) { if(array == null) throw new BindingException("Tried to calculate the size of a null array."); throw new BindingException("Unexpected class "+array.getClass().getSimpleName()+", "+clazz.getSimpleName()+" expected"); } return Array.getLength(array); } @Override public boolean isInstance(Object obj) { return clazz.isInstance(obj); } @Override public boolean isImmutable() { return false; } @Override public void add(Object array, int index, Object element) throws BindingException, IndexOutOfBoundsException { throw new UnsupportedOperationException(); } @Override public void remove(Object array, int index, int count) throws BindingException, IndexOutOfBoundsException { throw new UnsupportedOperationException(); } @Override public void setSize(Object array, int newSize) throws BindingException { int oldSize = Array.getLength(array); if (oldSize==newSize) return; throw new BindingException(clazz.getName()+" is length immutable"); } @Override public boolean isResizable() { return false; } @Override protected boolean baseEquals(Object obj) { return super.baseEquals(obj) && ((GenericArrayBinding)obj).clazz.equals(clazz); } @Override public int baseHashCode() { return super.baseHashCode() + 13 * clazz.hashCode(); } }