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