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