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.map;
17  
18  import java.lang.annotation.Annotation;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.joda.beans.Bean;
23  import org.joda.beans.MetaBean;
24  import org.joda.beans.Property;
25  import org.joda.beans.PropertyReadWrite;
26  import org.joda.beans.impl.BasicMetaProperty;
27  import org.joda.beans.impl.BasicProperty;
28  
29  /**
30   * A meta-property using a {@code MapBean} for storage.
31   * 
32   * @author Stephen Colebourne
33   */
34  final class MapBeanMetaProperty extends BasicMetaProperty<Object> {
35  
36      /** The bean. */
37      private MapBean bean;
38  
39      /**
40       * Factory to create a meta-property.
41       * 
42       * @param mapBean  the {@code MapBean}, not null
43       * @param propertyName  the property name, not empty
44       */
45      public static MapBeanMetaProperty of(MapBean mapBean, String propertyName) {
46          return new MapBeanMetaProperty(mapBean, propertyName);
47      }
48  
49      /**
50       * Constructor.
51       * 
52       * @param mapBean  the {@code MapBean}, not null
53       * @param propertyName  the property name, not empty
54       */
55      private MapBeanMetaProperty(MapBean mapBean, String propertyName) {
56          super(propertyName);
57          this.bean = mapBean;
58      }
59  
60      //-----------------------------------------------------------------------
61      @Override
62      public Property<Object> createProperty(Bean bean) {
63          return BasicProperty.of(bean, this);
64      }
65  
66      @Override
67      public MetaBean metaBean() {
68          return bean.metaBean();
69      }
70  
71      @Override
72      public Class<?> declaringType() {
73          return MapBean.class;
74      }
75  
76      @Override
77      public Class<Object> propertyType() {
78          return Object.class;
79      }
80  
81      @Override
82      public Class<Object> propertyGenericType() {
83          return Object.class;
84      }
85  
86      @Override
87      public PropertyReadWrite readWrite() {
88          return PropertyReadWrite.READ_WRITE;
89      }
90  
91      @Override
92      public List<Annotation> annotations() {
93          return Collections.emptyList();
94      }
95  
96      //-----------------------------------------------------------------------
97      @Override
98      public Object get(Bean bean) {
99          if (bean != this.bean) {
100             throw new ClassCastException("Bean is a MapBean, but not the correct MapBean");
101         }
102         return this.bean.get(name());
103     }
104 
105     @Override
106     public void set(Bean bean, Object value) {
107         if (bean != this.bean) {
108             throw new ClassCastException("Bean is a MapBean, but not the correct MapBean");
109         }
110         this.bean.put(name(), value);
111     }
112 
113 }