1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33
34
35
36
37
38 public final class BasicPropertyMap
39 extends AbstractMap<String, Property<?>> implements PropertyMap {
40
41
42 private final Bean bean;
43
44
45
46
47
48
49
50 public static BasicPropertyMap of(Bean bean) {
51 return new BasicPropertyMap(bean);
52 }
53
54
55
56
57
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
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
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 }