View Javadoc

1   /*
2   * E-nspire Gemini.
3   * A Java and AspectJ based framework that enables transparent 
4   * bidirectional relationships between Plain Old Java Objects.
5   * 
6   * Copyright (C) 2005 Dragan Djuric
7   * 
8   * This program is free software; you can redistribute it and/or
9   * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  * 
22  * Contact the author at dragand@dev.java.net
23  */
24  package com.enspire.gemini.updaters;
25  
26  import com.enspire.gemini.RelationshipUpdater;
27  import com.enspire.reflection.PropertyReflection;
28  
29  /***
30   * Updates one end of a relationship, that have the upper multiplicity 1,
31   * and is represented as a simple Java property, not including primitive Java
32   * types.
33   *
34   * @author Dragan Djuric <dragand@dev.java.net>
35   * @since 1.0
36   **/
37  public class SimplePropertyRelationshipUpdater implements RelationshipUpdater {
38  
39      private PropertyReflection propertyReflection;
40      
41      /***
42       * Constructs an object leaving its dependencies unset.
43       */
44      public SimplePropertyRelationshipUpdater() {
45          super();
46      }
47      
48      /***
49       * Constructs an object and sets the propertyReflection dependency.
50       * @param reflection the object that is used to manipulate JavaBean properties.
51       */
52      public SimplePropertyRelationshipUpdater(PropertyReflection reflection) {
53          super();
54          this.propertyReflection = reflection;
55      }
56  
57      /***
58       * Gets propertyReflection - the object that is used to manipulate 
59       * JavaBean properties.
60       * @return propertyReflection the object that is used to manipulate 
61       * JavaBean properties
62       */
63      public PropertyReflection getPropertyReflection() {
64          return this.propertyReflection;
65      }
66      
67      /***
68       * Sets propertyReflection - the object that is used to manipulate 
69       * JavaBean properties.
70       * @param propertyReflection the propertyReflection to set.
71       */
72      public void setPropertyReflection(PropertyReflection propertyReflection) {
73          this.propertyReflection = propertyReflection;
74      }
75      
76      /***
77       * Sets the the property to new value if it is diferent from the old value.
78       * 
79       * @see com.enspire.gemini.RelationshipUpdater#set(java.lang.Object, java.lang.String, java.lang.Object)
80       */
81      public Object set(Object owner, String propertyName, Object value) {
82          if (owner == null) {
83              return null;
84          }
85          Object oldValue = getPropertyReflection().
86                  getSimpleProperty(owner, propertyName);
87          if (oldValue != value) {
88              getPropertyReflection().setSimpleProperty(owner, propertyName, value);
89          }
90          return oldValue;
91      }
92  
93      /***
94       * Sets the property to <code>null</code>.
95       * 
96       * @see com.enspire.gemini.RelationshipUpdater#unset(java.lang.Object, java.lang.String, java.lang.Object)
97       */
98      public Object unset(Object owner, String propertyName, Object value) {
99          return set(owner, propertyName, null);
100     }
101     
102 }