]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/FieldAccessor.java
6615e94fb41edf507f55e646cbb9b539bca679c0
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / rules / range / FieldAccessor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management
3  * in 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.Field;
15
16 import org.apache.log4j.Logger;
17 import org.simantics.objmap.exceptions.MappingException;
18
19
20 /**
21  * Accesses the given field of the element.
22  * @author Hannu Niemistö
23  */
24 public class FieldAccessor<Range,T> implements IRangeAccessor<Range,T> {
25     
26     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
27     
28         Field field;
29
30         public FieldAccessor(Field field) {
31                 this.field = field;
32         }
33
34         @Override
35         public T get(Range element) throws MappingException {
36                 try {
37                     @SuppressWarnings("unchecked")
38                         T result = (T)field.get(element);
39                     
40                 if(LOGGER.isInfoEnabled())
41                     LOGGER.info("        FieldAccessor.get " +
42                             field.getName() + " -> " + result
43                     );
44                 
45                         return result;
46                 } catch (IllegalArgumentException e) {
47                         throw new MappingException(e);
48                 } catch (IllegalAccessException e) {
49                         throw new MappingException(e);
50                 }
51         }
52
53         @Override
54         public boolean set(Range element, T value) throws MappingException {
55                 try {
56                         Object currentValue = field.get(element);
57                         
58             if(LOGGER.isInfoEnabled())
59                 LOGGER.info("        FieldAccessor.set " +
60                         field.getName() + " " + currentValue +  
61                         " -> " + value
62                 );
63                         
64                         if(value == null 
65                                 ? (currentValue == null || field.getType().isPrimitive()) 
66                                 : value.equals(currentValue))
67                                 return false;                   
68                         field.set(element, value);
69                         return true;
70                 } catch (IllegalArgumentException e) {
71                         throw new MappingException(e);
72                 } catch (IllegalAccessException e) {
73                         throw new MappingException(e);
74                 }
75         }       
76 }