/******************************************************************************* * Copyright (c) 2012, 2013 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.objmap.graph.rules.range; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import org.simantics.objmap.exceptions.MappingException; import org.simantics.utils.datastructures.Pair; /** * Accessor for mapped collections. * Uses three methods: * - Getter: returns the collection. * - Adder: adds one item into the collection. * - Remover: removes one item from the collection. * * @author Marko Luukkainen * * @param */ public class ListAccessor implements IRangeAccessor> { private Method getter; private Method adder; private Method remover; public ListAccessor(Method getter, Method adder, Method remover) { this.getter = getter; this.adder = adder; this.remover = remover; } @SuppressWarnings("unchecked") public java.util.Collection get(Object element) throws MappingException { try { return (Collection) getter.invoke(element); } catch (IllegalArgumentException e) { throw new MappingException(e); } catch (IllegalAccessException e) { throw new MappingException(e); } catch (InvocationTargetException e) { throw new MappingException(e.getCause()); } }; @Override public boolean set(Range element, Collection value) throws MappingException { java.util.Collection current = get(element); Collection> adding = new ArrayList>(); Collection removing = new ArrayList(); for (T e : current) { if (!value.contains(e)) removing.add(e); } int i = 0; for (T e : value) { if (!current.contains(e)) adding.add(new Pair(i, e)); i++; } try { for (T e : removing) { remover.invoke(element, e); } for (Pair e : adding) { adder.invoke(element, e.first,e.second); } } catch (IllegalArgumentException e) { throw new MappingException(e); } catch (IllegalAccessException e) { throw new MappingException(e); } catch (InvocationTargetException e) { throw new MappingException(e.getCause()); } return removing.size() > 0 || adding.size() > 0; } }