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.util.AbstractMap;
19  import java.util.AbstractSet;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.joda.beans.Bean;
27  import org.joda.beans.MetaProperty;
28  import org.joda.beans.Property;
29  import org.joda.beans.PropertyMap;
30  
31  /**
32   * A standard map of properties.
33   * <p>
34   * This is the standard implementation of a map of properties derived from a meta-bean.
35   * 
36   * @author Stephen Colebourne
37   */
38  public final class BasicPropertyMap
39          extends AbstractMap<String, Property<?>> implements PropertyMap {
40  
41      /** The bean. */
42      private final Bean bean;
43  
44      /**
45       * Factory to create a property map avoiding duplicate generics.
46       * 
47       * @param bean  the bean
48       * @return the property map, not null
49       */
50      public static BasicPropertyMap of(Bean bean) {
51          return new BasicPropertyMap(bean);
52      }
53  
54      /**
55       * Creates a property map.
56       * 
57       * @param bean  the bean that the property is bound to, not null
58       */
59      private BasicPropertyMap(Bean bean) {
60          if (bean == null) {
61              throw new NullPointerException("Bean must not be null");
62          }
63          this.bean = bean;
64      }
65  
66      //-----------------------------------------------------------------------
67      @Override
68      public int size() {
69          return bean.metaBean().metaPropertyCount();
70      }
71  
72      @Override
73      public boolean containsKey(Object obj) {
74          return obj instanceof String ? bean.metaBean().metaPropertyExists(obj.toString()) : false;
75      }
76  
77      @Override
78      public Property<?> get(Object obj) {
79          return containsKey(obj) ? bean.metaBean().metaProperty(obj.toString()).createProperty(bean) : null;
80      }
81  
82      @Override
83      public Set<String> keySet() {
84          return bean.metaBean().metaPropertyMap().keySet();
85      }
86  
87      @Override
88      public Set<Entry<String, Property<?>>> entrySet() {
89          return new AbstractSet<Entry<String, Property<?>>>() {
90              // TODO: possibly override contains()
91              @Override
92              public int size() {
93                  return bean.metaBean().metaPropertyCount();
94              }
95              @Override
96              public Iterator<Entry<String, Property<?>>> iterator() {
97                  final Iterator<MetaProperty<?>> it = bean.metaBean().metaPropertyMap().values().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                         MetaProperty<?> meta = it.next();
106                         return new SimpleImmutableEntry<String, Property<?>>(meta.name(), BasicProperty.of(bean, meta));
107                     }
108                     @Override
109                     public void remove() {
110                         throw new UnsupportedOperationException("Unmodifiable");
111                     }
112                 };
113             }
114         };
115     }
116 
117     //-----------------------------------------------------------------------
118     @Override
119     public Map<String, Object> flatten() {
120         // TODO: dedicated map implementation
121         Map<String, MetaProperty<?>> propertyMap = bean.metaBean().metaPropertyMap();
122         Map<String, Object> map = new HashMap<String, Object>(propertyMap.size());
123         for (Entry<String, MetaProperty<?>> entry : propertyMap.entrySet()) {
124             map.put(entry.getKey(), entry.getValue().get(bean));
125         }
126         return Collections.unmodifiableMap(map);
127     }
128 
129 }