MergedValueMap.java
6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.ObjectUtils
* org.apache.jackrabbit.util.ISO8601
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ValueMap
*/
package com.day.cq.wcm.foundation.forms;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.ObjectUtils;
import org.apache.jackrabbit.util.ISO8601;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
public class MergedValueMap
implements ValueMap {
private List<Resource> resources;
private Map<String, Object> cache;
public MergedValueMap(List<Resource> resources) {
this.resources = resources;
}
protected void readFully() {
if (this.cache == null) {
this.cache = this.fetchAndMergeAllValues();
}
}
protected Map<String, Object> fetchAndMergeAllValues() {
HashMap<String, Object> mergedMap = new HashMap<String, Object>();
boolean firstResource = true;
for (Resource resource : this.resources) {
ValueMap map = (ValueMap)resource.adaptTo(ValueMap.class);
if (map != null) {
for (Map.Entry entry : map.entrySet()) {
String key = (String)entry.getKey();
if (mergedMap.containsKey(key)) {
Object newValue;
Object value = mergedMap.get(key);
if (value == null || (newValue = entry.getValue()) != null && value.equals(newValue)) continue;
mergedMap.put(key, null);
continue;
}
if (firstResource) {
mergedMap.put(key, entry.getValue());
continue;
}
mergedMap.put(key, null);
}
}
firstResource = false;
}
return mergedMap;
}
public Object get(Object key) {
this.readFully();
Object value = this.cache.get(key);
if (value == null) {
value = this.read((String)key);
}
return value;
}
private Object read(String key) {
Object value = null;
boolean firstResource = true;
boolean isArray = false;
for (Resource resource : this.resources) {
ValueMap map = (ValueMap)resource.adaptTo(ValueMap.class);
if (map != null) {
if (firstResource) {
value = map.get((Object)key);
if (value == null) break;
isArray = value.getClass().isArray();
} else {
Object newValue = map.get((Object)key);
if (newValue == null) {
value = null;
break;
}
boolean newIsArray = newValue.getClass().isArray();
if (isArray != newIsArray || isArray && !Arrays.equals((Object[])value, (Object[])newValue) || !isArray && !ObjectUtils.equals((Object)value, (Object)newValue)) {
value = null;
break;
}
}
}
firstResource = false;
}
this.cache.put(key, value);
return value;
}
public <T> T get(String name, Class<T> type) {
this.readFully();
if (type == null) {
return (T)this.get(name);
}
return this.convert(this.get(name), type);
}
public <T> T get(String name, T defaultValue) {
this.readFully();
Class value = this.get(name, (T)defaultValue.getClass());
return value == null ? defaultValue : value;
}
public int size() {
this.readFully();
return this.cache.size();
}
public boolean isEmpty() {
return this.size() == 0;
}
public boolean containsKey(Object key) {
this.readFully();
return this.cache.containsKey(key);
}
public boolean containsValue(Object value) {
this.readFully();
return this.cache.containsValue(value);
}
public Set<Map.Entry<String, Object>> entrySet() {
this.readFully();
return this.cache.entrySet();
}
public Set<String> keySet() {
this.readFully();
return this.cache.keySet();
}
public Collection<Object> values() {
this.readFully();
return this.cache.values();
}
private <T> T convert(Object obj, Class<T> type) {
try {
if (obj == null) {
return null;
}
if (type.isAssignableFrom(obj.getClass())) {
return (T)obj;
}
if (type.isArray()) {
return (T)this.convertToArray(obj, type.getComponentType());
}
if (obj instanceof Calendar && type == String.class) {
return (T)ISO8601.format((Calendar)((Calendar)obj));
}
if (type == String.class) {
return (T)String.valueOf(obj);
}
if (type == Integer.class) {
return Integer.parseInt(obj.toString());
}
if (type == Long.class) {
return Long.parseLong(obj.toString());
}
if (type == Double.class) {
return Double.parseDouble(obj.toString());
}
if (type == Boolean.class) {
return Boolean.parseBoolean(obj.toString());
}
return null;
}
catch (NumberFormatException e) {
return null;
}
}
private <T> T[] convertToArray(Object obj, Class<T> type) {
LinkedList<T> values = new LinkedList<T>();
if (obj.getClass().isArray()) {
for (Object o : (Object[])obj) {
values.add(this.convert(o, type));
}
} else {
values.add(this.convert(obj, type));
}
Object[] result = (Object[])Array.newInstance(type, values.size());
return values.toArray(result);
}
public void clear() {
throw new UnsupportedOperationException();
}
public Object put(String key, Object value) {
throw new UnsupportedOperationException();
}
public void putAll(Map<? extends String, ? extends Object> t) {
throw new UnsupportedOperationException();
}
public Object remove(Object key) {
throw new UnsupportedOperationException();
}
}