]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/ListAccessor.java
5f79c58568836c3aff430c0f085576d72290baaa
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / rules / range / ListAccessor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.objmap.graph.rules.range;\r
13 \r
14 import java.lang.reflect.InvocationTargetException;\r
15 import java.lang.reflect.Method;\r
16 import java.util.ArrayList;\r
17 import java.util.Collection;\r
18 \r
19 import org.simantics.objmap.exceptions.MappingException;\r
20 import org.simantics.utils.datastructures.Pair;\r
21 \r
22 \r
23 /**\r
24  * Accessor for mapped collections. \r
25  * Uses three methods:\r
26  * - Getter: returns the collection.\r
27  * - Adder: adds one item into the collection.\r
28  * - Remover: removes one item from the collection. \r
29  * \r
30  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
31  *\r
32  * @param <T>\r
33  */\r
34 public class ListAccessor<Range,T>  implements IRangeAccessor<Range,Collection<T>> {\r
35         \r
36         private Method getter;\r
37         private Method adder;\r
38         private Method remover;\r
39         \r
40         public ListAccessor(Method getter, Method adder, Method remover) {\r
41                 this.getter = getter;\r
42                 this.adder = adder;\r
43                 this.remover = remover;\r
44         }\r
45         \r
46         @SuppressWarnings("unchecked")\r
47         public java.util.Collection<T> get(Object element) throws MappingException {\r
48                 try {\r
49                         return (Collection<T>) getter.invoke(element);\r
50                 } catch (IllegalArgumentException e) {\r
51                         throw new MappingException(e);\r
52                 } catch (IllegalAccessException e) {\r
53                         throw new MappingException(e);\r
54                 } catch (InvocationTargetException e) {\r
55                         throw new MappingException(e);\r
56                 }\r
57         };\r
58 \r
59         @Override\r
60         public boolean set(Range element, Collection<T> value)\r
61                         throws MappingException {\r
62                 java.util.Collection<T> current = get(element);\r
63                 Collection<Pair<Integer, T>> adding = new ArrayList<Pair<Integer, T>>();\r
64                 Collection<T> removing = new ArrayList<T>();\r
65                 for (T e : current) {\r
66                         if (!value.contains(e))\r
67                                 removing.add(e);\r
68                 }\r
69                 int i = 0;\r
70                 for (T e : value) {\r
71                         if (!current.contains(e))\r
72                                 adding.add(new Pair<Integer, T>(i, e));\r
73                         i++;\r
74                 }\r
75                 \r
76                 try {\r
77                         for (T e : removing) {\r
78                                 remover.invoke(element, e);\r
79                         }\r
80                         \r
81                         for (Pair<Integer,T> e : adding) {\r
82                                 adder.invoke(element, e.first,e.second);\r
83                         }\r
84                 } catch (IllegalArgumentException e) {\r
85                         throw new MappingException(e);\r
86                 } catch (IllegalAccessException e) {\r
87                         throw new MappingException(e);\r
88                 } catch (InvocationTargetException e) {\r
89                         throw new MappingException(e);\r
90                 }\r
91                 return removing.size() > 0 || adding.size() > 0;\r
92                 \r
93         }\r
94 }\r