View Javadoc

1   /*
2    *  Copyright 2001-2007 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.beans;
17  
18  /***
19   * Implementation of a {@link Property} that uses reflection to access
20   * get and set methods.
21   * 
22   * @author Stephen Colebourne
23   */
24  public class SimpleProperty<B, T> implements Property<B, T> {
25  
26      /*** The bean that the property is bound to. */
27      private final B bean;
28      /*** The property. */
29      private final MetaProperty<B, T> metaProperty;
30  
31      /***
32       * Constructor.
33       * 
34       * @param bean  the bean that the property is bound to, not null
35       * @param metaProperty  the meta property, not null
36       */
37      public SimpleProperty(B bean, MetaProperty<B, T> metaProperty) {
38          if (bean == null) {
39              throw new IllegalArgumentException("The bean must not be null");
40          }
41          if (metaProperty == null) {
42              throw new IllegalArgumentException("The meta property must not be null");
43          }
44          this.bean = bean;
45          this.metaProperty = metaProperty;
46      }
47  
48      //-----------------------------------------------------------------------
49      /***
50       * Gets the bean which owns this bound property.
51       * 
52       * @return the bean
53       */
54      public B bean() {
55          return bean;
56      }
57  
58      /***
59       * Gets the property itself.
60       * 
61       * @return the name of the property
62       */
63      public MetaProperty<B, T> metaProperty() {
64          return metaProperty;
65      }
66  
67      //-----------------------------------------------------------------------
68      /***
69       * Gets the value of the bound property.
70       * <p>
71       * This is the equivalent to calling <code>getFoo()</code> on the bean itself.
72       * 
73       * @return the value of the property on the bound bean
74       * @throws UnsupportedOperationException if the property is write-only
75       */
76      public T get() {
77          return metaProperty().get(bean());
78      }
79  
80      /***
81       * Sets the value of the bound property.
82       * <p>
83       * This is the equivalent to calling <code>setFoo()</code> on the bean itself.
84       * 
85       * @param value  the value to set into the property on the bound bean
86       * @throws UnsupportedOperationException if the property is read-only
87       */
88      public void set(T value) {
89          metaProperty().set(bean(), value);
90      }
91  
92      //-----------------------------------------------------------------------
93      /***
94       * Returns a debugging string.
95       * 
96       * @return a debugging string
97       */
98      @Override
99      public String toString() {
100         return metaProperty().getName() + "=" + get();
101     }
102 
103 }