1 package org.simantics.objmap.graph.rules.range;
\r
3 import java.lang.reflect.InvocationTargetException;
\r
4 import java.lang.reflect.Method;
\r
6 import org.simantics.objmap.exceptions.MappingException;
\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
14 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
18 public class CompoundGetSetValueAccessor<Range,T> implements IRangeAccessor<Range,T> {
\r
20 private Method getter;
\r
21 private Method setter;
\r
22 private boolean primitive;
\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
30 @SuppressWarnings("unchecked")
\r
31 public T get(Range element) throws MappingException {
\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
44 public boolean set(Range element, T value)
\r
45 throws MappingException {
\r
46 if (value == null && primitive)
\r
48 if (equal(get(element),value))
\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
63 private boolean equal(Object v1, Object v2) {
\r
68 } else if (v2 == null) {
\r
71 return v1.equals(v2);
\r