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.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  import org.joda.beans.Bean;
23  import org.joda.beans.BeanBuilder;
24  import org.joda.beans.MetaProperty;
25  import org.joda.beans.PropertyMap;
26  import org.joda.beans.impl.BasicBeanBuilder;
27  import org.joda.beans.impl.BasicMetaBean;
28  
29  /**
30   * Implementation of a meta-bean for {@code MapBean}.
31   * 
32   * @author Stephen Colebourne
33   */
34  class MapMetaBean extends BasicMetaBean {
35  
36      /**
37       * The bean itself.
38       */
39      private final MapBean bean;
40  
41      /**
42       * Creates the meta-bean.
43       * 
44       * @param bean  the underlying bean, not null
45       */
46      MapMetaBean(MapBean bean) {
47          this.bean = bean;
48      }
49  
50      //-----------------------------------------------------------------------
51      @Override
52      public BeanBuilder<MapBean> builder() {
53          return new BasicBeanBuilder<MapBean>(new MapBean());
54      }
55  
56      @Override
57      public PropertyMap createPropertyMap(Bean bean) {
58          return MapBeanPropertyMap.of(beanType().cast(bean));
59      }
60  
61      @Override
62      public Class<MapBean> beanType() {
63          return MapBean.class;
64      }
65  
66      @Override
67      public String beanName() {
68          return MapBean.class.getName();
69      }
70  
71      @Override
72      public int metaPropertyCount() {
73          return bean.size();
74      }
75  
76      @Override
77      public boolean metaPropertyExists(String name) {
78          return bean.containsKey(name);
79      }
80  
81      @SuppressWarnings("unchecked")
82      @Override
83      public <R> MetaProperty<R> metaProperty(String name) {
84          return (MetaProperty<R>) bean.metaProperty(name);
85      }
86  
87      @Override
88      public Iterable<MetaProperty<?>> metaPropertyIterable() {
89          return new Iterable<MetaProperty<?>>() {
90              private final Iterator<String> it = bean.keySet().iterator();
91              @Override
92              public Iterator<MetaProperty<?>> iterator() {
93                  return new Iterator<MetaProperty<?>>() {
94                      @Override
95                      public boolean hasNext() {
96                          return it.hasNext();
97                      }
98                      @Override
99                      public MetaProperty<?> next() {
100                         return MapBeanMetaProperty.of(bean, it.next());
101                     }
102                     @Override
103                     public void remove() {
104                         throw new UnsupportedOperationException("Unmodifiable");
105                     }
106                     
107                 };
108             }
109         };
110     }
111 
112     @Override
113     public Map<String, MetaProperty<?>> metaPropertyMap() {
114         Map<String, MetaProperty<?>> map = new HashMap<String, MetaProperty<?>>();
115         for (String name : bean.keySet()) {
116             map.put(name, MapBeanMetaProperty.of(bean, name));
117         }
118         return map;
119     }
120 
121 }