]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/CompoundGetSetValueAccessor.java
3D framework (Simca 2012)
[simantics/3d.git] / org.simantics.objmap2 / src / org / simantics / objmap / graph / rules / range / CompoundGetSetValueAccessor.java
1 package org.simantics.objmap.graph.rules.range;\r
2 \r
3 import java.lang.reflect.InvocationTargetException;\r
4 import java.lang.reflect.Method;\r
5 \r
6 import org.simantics.objmap.exceptions.MappingException;\r
7 \r
8 \r
9 /**\r
10  * Accessor for mapped value. Uses two methods:\r
11  * - Getter: returns the current value.\r
12  * - Setter: sets the current value. The value may be null. (if setter parameter is primitive, null value is not mapped).\r
13  * \r
14  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
15  *\r
16  * @param <T>\r
17  */\r
18 public class CompoundGetSetValueAccessor<Range,T>  implements IRangeAccessor<Range,T> {\r
19         \r
20         private Method getter;\r
21         private Method setter;\r
22         private boolean primitive;\r
23         \r
24         public CompoundGetSetValueAccessor(Method getter, Method setter) {\r
25                 this.getter = getter;\r
26                 this.setter = setter;\r
27                 this.primitive = setter.getParameterTypes()[0].isPrimitive();\r
28         }\r
29         \r
30         @SuppressWarnings("unchecked")\r
31         public T get(Range element) throws MappingException {\r
32                 try {\r
33                         return (T) getter.invoke(element);\r
34                 } catch (IllegalArgumentException e) {\r
35                         throw new MappingException(e);\r
36                 } catch (IllegalAccessException e) {\r
37                         throw new MappingException(e);\r
38                 } catch (InvocationTargetException e) {\r
39                         throw new MappingException(e);\r
40                 }\r
41         };\r
42 \r
43         @Override\r
44         public boolean set(Range element, T value)\r
45                         throws MappingException {\r
46                 if (value == null && primitive)\r
47                         return false;\r
48                 if (equal(get(element),value))\r
49                         return false;\r
50                 try {\r
51                         setter.invoke(element, value);\r
52                 } catch (IllegalArgumentException e) {\r
53                         throw new MappingException(e);\r
54                 } catch (IllegalAccessException e) {\r
55                         throw new MappingException(e);\r
56                 } catch (InvocationTargetException e) {\r
57                         throw new MappingException(e);\r
58                 }\r
59                 return true;\r
60                 \r
61         }\r
62         \r
63         private boolean equal(Object v1, Object v2) {\r
64                 if (v1 == null) {\r
65                         if (v2 == null)\r
66                                 return true;\r
67                         return false;\r
68                 } else if (v2 == null) {\r
69                         return false;\r
70                 }\r
71                 return v1.equals(v2);\r
72         }\r
73 }\r