X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fgraph%2Frules%2Frange%2FListAccessor.java;h=18479880417c3659978a1c3637fb1b46dccee2c1;hp=a384235be4021a49be6e3ccec5e66c3155308791;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/ListAccessor.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/ListAccessor.java index a384235be..184798804 100644 --- a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/ListAccessor.java +++ b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/ListAccessor.java @@ -1,94 +1,94 @@ -/******************************************************************************* - * 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; - - } -} +/******************************************************************************* + * 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; + + } +}