1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.data.provider.impl;
17
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import org.kuali.rice.krad.data.metadata.DataObjectMetadata;
25 import org.kuali.rice.krad.data.provider.MetadataProvider;
26
27
28
29
30
31
32
33
34 public abstract class MetadataProviderBase implements MetadataProvider {
35
36
37
38
39 protected ConcurrentHashMap<Class<?>, DataObjectMetadata> masterMetadataMap =
40 new ConcurrentHashMap<Class<?>, DataObjectMetadata>();
41
42
43
44
45
46
47
48
49
50 protected abstract void initializeMetadata(Collection<Class<?>> types);
51
52
53
54
55 @Override
56 public boolean handles(Class<?> type) {
57 if (type == null) {
58 return false;
59 }
60 if (masterMetadataMap.isEmpty()) {
61 initializeMetadata(null);
62 }
63 return masterMetadataMap.containsKey(type);
64 }
65
66
67
68
69 @Override
70 public Collection<Class<?>> getSupportedTypes() {
71 if (masterMetadataMap.isEmpty()) {
72 initializeMetadata(null);
73 }
74 return masterMetadataMap.keySet();
75 }
76
77
78
79
80 @Override
81 public Map<Class<?>, DataObjectMetadata> provideMetadata() {
82 return provideMetadataForTypes(null);
83 }
84
85
86
87
88 @Override
89 public Map<Class<?>, DataObjectMetadata> provideMetadataForTypes(Collection<Class<?>> types) {
90 if (masterMetadataMap.isEmpty()) {
91 initializeMetadata(types);
92 return Collections.unmodifiableMap(masterMetadataMap);
93 } else if (types == null || types.isEmpty()) {
94 return Collections.unmodifiableMap(masterMetadataMap);
95 } else {
96 HashMap<Class<?>, DataObjectMetadata> subMap = new HashMap<Class<?>, DataObjectMetadata>();
97
98 for (Class<?> key : masterMetadataMap.keySet()) {
99 if (types.contains(key)) {
100 subMap.put(key, masterMetadataMap.get(key));
101 }
102 }
103 return subMap;
104 }
105 }
106
107
108
109
110 @Override
111 public DataObjectMetadata getMetadataForType(Class<?> dataObjectType) throws IllegalArgumentException {
112 if (dataObjectType == null) {
113 throw new IllegalArgumentException("getMetadataForType: NULL passed for the dataObjectType");
114 }
115 if (masterMetadataMap.isEmpty()) {
116 initializeMetadata(null);
117 }
118 return masterMetadataMap.get(dataObjectType);
119 }
120
121
122
123
124
125
126
127 protected boolean isClassPersistable(Class<?> clazz) {
128 if (masterMetadataMap.isEmpty()) {
129 initializeMetadata(null);
130 }
131 return masterMetadataMap.containsKey(clazz);
132 }
133
134
135
136
137
138
139 @Override
140 public boolean requiresListOfExistingTypes() {
141 return false;
142 }
143 }