X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fgraph%2Frules%2Frange%2FCollectionAccessor.java;fp=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fgraph%2Frules%2Frange%2FCollectionAccessor.java;h=c3569ed4242f78e93cd0007663004299de2413d1;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/CollectionAccessor.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/CollectionAccessor.java new file mode 100644 index 000000000..c3569ed42 --- /dev/null +++ b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/CollectionAccessor.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * 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; + + +/** + * 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 CollectionAccessor implements IRangeAccessor> { + + private Method getter; + private Method adder; + private Method remover; + + public CollectionAccessor(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); + } + }; + + @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); + } + for (T e : value) { + if (!current.contains(e)) + adding.add(e); + } + + try { + for (T e : removing) { + remover.invoke(element, e); + } + + for (T e : adding) { + adder.invoke(element, e); + } + } catch (IllegalArgumentException e) { + throw new MappingException(e); + } catch (IllegalAccessException e) { + throw new MappingException(e); + } catch (InvocationTargetException e) { + throw new MappingException(e); + } + return removing.size() > 0 || adding.size() > 0; + + } +}