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.AbstractMap;
19  import java.util.AbstractSet;
20  import java.util.Collections;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.joda.beans.MetaBean;
26  import org.joda.beans.Property;
27  import org.joda.beans.PropertyMap;
28  import org.joda.beans.impl.BasicProperty;
29  
30  /**
31   * A map of properties for a {@code FlexiBean}.
32   * 
33   * @author Stephen Colebourne
34   */
35  final class FlexiPropertyMap
36          extends AbstractMap<String, Property<?>> implements PropertyMap {
37  
38      /** The bean. */
39      private final FlexiBean bean;
40  
41      /**
42       * Factory to create a property map avoiding duplicate generics.
43       * 
44       * @param bean  the bean
45       */
46      static FlexiPropertyMap of(FlexiBean bean) {
47          return new FlexiPropertyMap(bean);
48      }
49  
50      /**
51       * Creates a property map.
52       * 
53       * @param bean  the bean that the property is bound to, not null
54       */
55      private FlexiPropertyMap(FlexiBean bean) {
56          if (bean == null) {
57              throw new NullPointerException("Bean must not be null");
58          }
59          this.bean = bean;
60      }
61  
62      //-----------------------------------------------------------------------
63      @Override
64      public int size() {
65          return bean.size();
66      }
67  
68      @Override
69      public boolean containsKey(Object obj) {
70          return bean.data.containsKey(obj);
71      }
72  
73      @Override
74      public Property<?> get(Object obj) {
75          return containsKey(obj) ? bean.property(obj.toString()) : null;
76      }
77  
78      @Override
79      public Set<String> keySet() {
80          return bean.data.keySet();
81      }
82  
83      @Override
84      public Set<Entry<String, Property<?>>> entrySet() {
85          if (size() == 0) {
86              return Collections.emptySet();
87          }
88          final MetaBean metaBean = bean.metaBean();
89          return new AbstractSet<Entry<String, Property<?>>>() {
90              // TODO: possibly override contains()
91              @Override
92              public int size() {
93                  return bean.size();
94              }
95              @Override
96              public Iterator<Entry<String, Property<?>>> iterator() {
97                  final Iterator<String> it = bean.data.keySet().iterator();
98                  return new Iterator<Entry<String, Property<?>>>() {
99                      @Override
100                     public boolean hasNext() {
101                         return it.hasNext();
102                     }
103                     @Override
104                     public Entry<String, Property<?>> next() {
105                         String name = it.next();
106                         Property<?> prop = BasicProperty.of(bean, FlexiMetaProperty.of(metaBean, name));
107                         return new SimpleImmutableEntry<String, Property<?>>(name, prop);
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, Object> flatten() {
121         return bean.toMap();
122     }
123 
124 }