]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/CollectionAccessor.java
Sync git svn branch with SVN repository r33144.
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / rules / range / CollectionAccessor.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 \r
21 \r
22 /**\r
23  * Accessor for mapped collections. \r
24  * Uses three methods:\r
25  * - Getter: returns the collection.\r
26  * - Adder: adds one item into the collection.\r
27  * - Remover: removes one item from the collection. \r
28  * \r
29  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
30  *\r
31  * @param <T>\r
32  */\r
33 public class CollectionAccessor<Range,T>  implements IRangeAccessor<Range,Collection<T>> {\r
34         \r
35         private Method getter;\r
36         private Method adder;\r
37         private Method remover;\r
38         \r
39         public CollectionAccessor(Method getter, Method adder, Method remover) {\r
40                 this.getter = getter;\r
41                 this.adder = adder;\r
42                 this.remover = remover;\r
43         }\r
44         \r
45         @SuppressWarnings("unchecked")\r
46         public java.util.Collection<T> get(Object element) throws MappingException {\r
47                 try {\r
48                         return (Collection<T>) getter.invoke(element);\r
49                 } catch (IllegalArgumentException e) {\r
50                         throw new MappingException(e);\r
51                 } catch (IllegalAccessException e) {\r
52                         throw new MappingException(e);\r
53                 } catch (InvocationTargetException e) {\r
54                         throw new MappingException(e.getCause());\r
55                 }\r
56         };\r
57 \r
58         @Override\r
59         public boolean set(Range element, Collection<T> value)\r
60                         throws MappingException {\r
61                 java.util.Collection<T> current = get(element);\r
62                 Collection<T> adding = new ArrayList<T>();\r
63                 Collection<T> removing = new ArrayList<T>();\r
64                 for (T e : current) {\r
65                         if (!value.contains(e))\r
66                                 removing.add(e);\r
67                 }\r
68                 for (T e : value) {\r
69                         if (!current.contains(e))\r
70                                 adding.add(e);\r
71                 }\r
72                 \r
73                 try {\r
74                         for (T e : removing) {\r
75                                 remover.invoke(element, e);\r
76                         }\r
77                         \r
78                         for (T e : adding) {\r
79                                 adder.invoke(element, e);\r
80                         }\r
81                 } catch (IllegalArgumentException e) {\r
82                         throw new MappingException(e);\r
83                 } catch (IllegalAccessException e) {\r
84                         throw new MappingException(e);\r
85                 } catch (InvocationTargetException e) {\r
86                         throw new MappingException(e.getCause());\r
87                 }\r
88                 return removing.size() > 0 || adding.size() > 0;\r
89                 \r
90         }\r
91 }\r