View Javadoc

1   /*
2    *  Copyright 2001-2013 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.impl;
17  
18  import java.lang.annotation.Annotation;
19  import java.util.List;
20  import java.util.NoSuchElementException;
21  
22  import org.joda.beans.Bean;
23  import org.joda.beans.JodaBeanUtils;
24  import org.joda.beans.MetaProperty;
25  import org.joda.beans.Property;
26  import org.joda.convert.StringConvert;
27  
28  /**
29   * An abstract base meta-property.
30   * 
31   * @param <P>  the type of the property content
32   * @author Stephen Colebourne
33   */
34  public abstract class BasicMetaProperty<P> implements MetaProperty<P> {
35  
36      /** The name of the property. */
37      private final String name;
38  
39      /**
40       * Constructor.
41       * 
42       * @param propertyName  the property name, not empty
43       */
44      protected BasicMetaProperty(String propertyName) {
45          if (propertyName == null || propertyName.length() == 0) {
46              throw new NullPointerException("Property name must not be null or empty");
47          }
48          this.name = propertyName;
49      }
50  
51      //-----------------------------------------------------------------------
52      @Override
53      public Property<P> createProperty(Bean bean) {
54          return BasicProperty.of(bean, this);
55      }
56  
57      @Override
58      public String name() {
59          return name;
60      }
61  
62      //-----------------------------------------------------------------------
63      @Override
64      public P put(Bean bean, Object value) {
65          P old = get(bean);
66          set(bean, value);
67          return old;
68      }
69  
70      //-----------------------------------------------------------------------
71      @Override
72      public String getString(Bean bean) {
73          return getString(bean, JodaBeanUtils.stringConverter());
74      }
75  
76      @Override
77      public String getString(Bean bean, StringConvert stringConvert) {
78          P value = get(bean);
79          return stringConvert.convertToString(propertyType(), value);
80      }
81  
82      @Override
83      public void setString(Bean bean, String value) {
84          setString(bean, value, JodaBeanUtils.stringConverter());
85      }
86  
87      @Override
88      public void setString(Bean bean, String value, StringConvert stringConvert) {
89          set(bean, stringConvert.convertFromString(propertyType(), value));
90      }
91  
92      //-----------------------------------------------------------------------
93      @SuppressWarnings("unchecked")
94      @Override
95      public <A extends Annotation> A annotation(Class<A> annotationClass) {
96          List<Annotation> annotations = annotations();
97          for (Annotation annotation : annotations) {
98              if (annotationClass.isInstance(annotation)) {
99                  return (A) annotation;
100             }
101         }
102         throw new NoSuchElementException("Unknown annotation: " + annotationClass.getName());
103     }
104 
105     //-----------------------------------------------------------------------
106     @Override
107     public boolean equals(Object obj) {
108         if (obj instanceof MetaProperty<?>) {
109             MetaProperty<?> other = (MetaProperty<?>) obj;
110             return name().equals(other.name()) && declaringType().equals(other.declaringType());
111         }
112         return false;
113     }
114 
115     @Override
116     public int hashCode() {
117         return name().hashCode() ^ declaringType().hashCode();
118     }
119 
120     /**
121      * Returns a string that summarises the meta-property.
122      * 
123      * @return a summary string, not null
124      */
125     @Override
126     public String toString() {
127         return declaringType().getSimpleName() + ":" + name();
128     }
129 
130 }