]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/CompoundGetSetValueAccessor.java
Fixed SCL Console Clear Console active-state when imports are erroneous
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / rules / range / CompoundGetSetValueAccessor.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.objmap.graph.rules.range;
13
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
16
17 import org.simantics.objmap.exceptions.MappingException;
18
19
20 /**
21  * Accessor for mapped value. Uses two methods:
22  * - Getter: returns the current value.
23  * - Setter: sets the current value. The value may be null. (if setter parameter is primitive, null value is not mapped).
24  * 
25  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
26  *
27  * @param <T>
28  */
29 public class CompoundGetSetValueAccessor<Range,T>  implements IRangeAccessor<Range,T> {
30         
31         private Method getter;
32         private Method setter;
33         private boolean primitive;
34         
35         public CompoundGetSetValueAccessor(Method getter, Method setter) {
36                 this.getter = getter;
37                 this.setter = setter;
38                 this.primitive = setter.getParameterTypes()[0].isPrimitive();
39         }
40         
41         @SuppressWarnings("unchecked")
42         public T get(Range element) throws MappingException {
43                 try {
44                         return (T) getter.invoke(element);
45                 } catch (IllegalArgumentException e) {
46                         throw new MappingException(e);
47                 } catch (IllegalAccessException e) {
48                         throw new MappingException(e);
49                 } catch (InvocationTargetException e) {
50                         throw new MappingException(e.getCause());
51                 }
52         };
53
54         @Override
55         public boolean set(Range element, T value)
56                         throws MappingException {
57                 if (value == null && primitive)
58                         return false;
59                 if (equal(get(element),value))
60                         return false;
61                 try {
62                         setter.invoke(element, value);
63                 } catch (IllegalArgumentException e) {
64                         throw new MappingException(e);
65                 } catch (IllegalAccessException e) {
66                         throw new MappingException(e);
67                 } catch (InvocationTargetException e) {
68                         throw new MappingException(e.getCause());
69                 }
70                 return true;
71                 
72         }
73         
74         private boolean equal(Object v1, Object v2) {
75                 if (v1 == null) {
76                         if (v2 == null)
77                                 return true;
78                         return false;
79                 } else if (v2 == null) {
80                         return false;
81                 }
82                 return v1.equals(v2);
83         }
84 }