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