+++ /dev/null
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.objmap.graph.rules.range;\r
-\r
-import java.lang.reflect.InvocationTargetException;\r
-import java.lang.reflect.Method;\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-\r
-import org.simantics.objmap.exceptions.MappingException;\r
-\r
-\r
-/**\r
- * Accessor for mapped collections. \r
- * Uses three methods:\r
- * - Getter: returns the collection.\r
- * - Adder: adds one item into the collection.\r
- * - Remover: removes one item from the collection. \r
- * \r
- * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
- *\r
- * @param <T>\r
- */\r
-public class CollectionAccessor<Range,T> implements IRangeAccessor<Range,Collection<T>> {\r
- \r
- private Method getter;\r
- private Method adder;\r
- private Method remover;\r
- \r
- public CollectionAccessor(Method getter, Method adder, Method remover) {\r
- this.getter = getter;\r
- this.adder = adder;\r
- this.remover = remover;\r
- }\r
- \r
- @SuppressWarnings("unchecked")\r
- public java.util.Collection<T> get(Object element) throws MappingException {\r
- try {\r
- return (Collection<T>) getter.invoke(element);\r
- } catch (IllegalArgumentException e) {\r
- throw new MappingException(e);\r
- } catch (IllegalAccessException e) {\r
- throw new MappingException(e);\r
- } catch (InvocationTargetException e) {\r
- throw new MappingException(e.getCause());\r
- }\r
- };\r
-\r
- @Override\r
- public boolean set(Range element, Collection<T> value)\r
- throws MappingException {\r
- java.util.Collection<T> current = get(element);\r
- Collection<T> adding = new ArrayList<T>();\r
- Collection<T> removing = new ArrayList<T>();\r
- for (T e : current) {\r
- if (!value.contains(e))\r
- removing.add(e);\r
- }\r
- for (T e : value) {\r
- if (!current.contains(e))\r
- adding.add(e);\r
- }\r
- \r
- try {\r
- for (T e : removing) {\r
- remover.invoke(element, e);\r
- }\r
- \r
- for (T e : adding) {\r
- adder.invoke(element, e);\r
- }\r
- } catch (IllegalArgumentException e) {\r
- throw new MappingException(e);\r
- } catch (IllegalAccessException e) {\r
- throw new MappingException(e);\r
- } catch (InvocationTargetException e) {\r
- throw new MappingException(e.getCause());\r
- }\r
- return removing.size() > 0 || adding.size() > 0;\r
- \r
- }\r
-}\r