]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/CompoundGetSetValueAccessor.java
Sync git svn branch with SVN repository r33144.
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / rules / range / CompoundGetSetValueAccessor.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 \r
17 import org.simantics.objmap.exceptions.MappingException;\r
18 \r
19 \r
20 /**\r
21  * Accessor for mapped value. Uses two methods:\r
22  * - Getter: returns the current value.\r
23  * - Setter: sets the current value. The value may be null. (if setter parameter is primitive, null value is not mapped).\r
24  * \r
25  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
26  *\r
27  * @param <T>\r
28  */\r
29 public class CompoundGetSetValueAccessor<Range,T>  implements IRangeAccessor<Range,T> {\r
30         \r
31         private Method getter;\r
32         private Method setter;\r
33         private boolean primitive;\r
34         \r
35         public CompoundGetSetValueAccessor(Method getter, Method setter) {\r
36                 this.getter = getter;\r
37                 this.setter = setter;\r
38                 this.primitive = setter.getParameterTypes()[0].isPrimitive();\r
39         }\r
40         \r
41         @SuppressWarnings("unchecked")\r
42         public T get(Range element) throws MappingException {\r
43                 try {\r
44                         return (T) getter.invoke(element);\r
45                 } catch (IllegalArgumentException e) {\r
46                         throw new MappingException(e);\r
47                 } catch (IllegalAccessException e) {\r
48                         throw new MappingException(e);\r
49                 } catch (InvocationTargetException e) {\r
50                         throw new MappingException(e.getCause());\r
51                 }\r
52         };\r
53 \r
54         @Override\r
55         public boolean set(Range element, T value)\r
56                         throws MappingException {\r
57                 if (value == null && primitive)\r
58                         return false;\r
59                 if (equal(get(element),value))\r
60                         return false;\r
61                 try {\r
62                         setter.invoke(element, value);\r
63                 } catch (IllegalArgumentException e) {\r
64                         throw new MappingException(e);\r
65                 } catch (IllegalAccessException e) {\r
66                         throw new MappingException(e);\r
67                 } catch (InvocationTargetException e) {\r
68                         throw new MappingException(e.getCause());\r
69                 }\r
70                 return true;\r
71                 \r
72         }\r
73         \r
74         private boolean equal(Object v1, Object v2) {\r
75                 if (v1 == null) {\r
76                         if (v2 == null)\r
77                                 return true;\r
78                         return false;\r
79                 } else if (v2 == null) {\r
80                         return false;\r
81                 }\r
82                 return v1.equals(v2);\r
83         }\r
84 }\r