MergedInheritanceValueMap.java
5.87 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.inherit.InheritanceValueMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.wrappers.ValueMapDecorator
*/
package com.day.cq.wcm.webservicesupport.impl;
import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.wcm.webservicesupport.impl.ReadOnlyEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
class MergedInheritanceValueMap
implements InheritanceValueMap {
private static final ValueMap EMPTY = new ValueMapDecorator(Collections.emptyMap());
private ResourceResolver resolver;
private final List<CacheEntry> cache;
public MergedInheritanceValueMap(ResourceResolver resolver, String basePath, String[] segments, String cutoffPath) {
this.resolver = resolver;
if (cutoffPath == null) {
cutoffPath = "/";
}
this.cache = new ArrayList<CacheEntry>();
String path = basePath;
while (!cutoffPath.equals(path) && path != null) {
for (String segment : segments) {
this.cache.add(new CacheEntry(path + "/" + segment));
}
path = ResourceUtil.getParent((String)path);
}
}
public <T> T get(String name, Class<T> type) {
Object value = this.override().get(name, type);
if (value == null) {
value = this.base().get(name, type);
}
return (T)value;
}
private ValueMap base() {
return this.cache.get(1).getData(this.resolver);
}
private ValueMap override() {
return this.cache.get(0).getData(this.resolver);
}
public <T> T get(String name, T defaultValue) {
Object value = defaultValue != null ? this.override().get(name, defaultValue.getClass()) : this.override().get(name, null);
if (value == null) {
value = this.base().get(name, defaultValue);
}
return (T)value;
}
public int size() {
int size = this.base().size();
for (String key : this.override().keySet()) {
if (this.base().containsKey((Object)key)) continue;
++size;
}
return size;
}
public boolean isEmpty() {
return this.base().isEmpty() && this.override().isEmpty();
}
public boolean containsKey(Object key) {
return this.base().containsKey(key) || this.override().containsKey(key);
}
public boolean containsValue(Object value) {
return this.base().containsValue(value) || this.override().containsValue(value);
}
public Object get(Object key) {
Object value = this.override().get(key);
if (value == null) {
value = this.base().get(key);
}
return value;
}
public Object put(String key, Object value) {
throw new UnsupportedOperationException();
}
public Object remove(Object key) {
throw new UnsupportedOperationException();
}
public void putAll(Map<? extends String, ? extends Object> m) {
throw new UnsupportedOperationException();
}
public void clear() {
throw new UnsupportedOperationException();
}
public Set<String> keySet() {
HashSet<String> keys = new HashSet<String>(this.size());
keys.addAll(this.base().keySet());
keys.addAll(this.override().keySet());
return keys;
}
public Collection<Object> values() {
ArrayList<Object> values = new ArrayList<Object>(this.size());
for (String key : this.keySet()) {
values.add(this.get(key));
}
return values;
}
public Set<Map.Entry<String, Object>> entrySet() {
HashSet<Map.Entry<String, Object>> entries = new HashSet<Map.Entry<String, Object>>(this.size());
for (String key : this.keySet()) {
entries.add(new ReadOnlyEntry(key, this.get(key)));
}
return entries;
}
public <T> T getInherited(String key, Class<T> type) {
for (CacheEntry entry : this.cache) {
ValueMap valueMap = entry.getData(this.resolver);
Object res = valueMap.get(key, type);
if (res == null) continue;
return (T)res;
}
return null;
}
public <T> T getInherited(String key, T defaultValue) {
for (CacheEntry entry : this.cache) {
ValueMap valueMap = entry.getData(this.resolver);
Object res = defaultValue != null ? valueMap.get(key, defaultValue.getClass()) : valueMap.get(key, defaultValue);
if (res == null) continue;
return (T)res;
}
return defaultValue;
}
public String toString() {
return this.getClass().getSimpleName() + "[ cache: " + this.cache + "]";
}
private static class CacheEntry {
private ValueMap data;
private String path;
public CacheEntry(String path) {
this.path = path;
}
public ValueMap getData(ResourceResolver resolver) {
if (this.data == null) {
Resource res = resolver.getResource(this.path);
this.data = res == null ? EMPTY : (ValueMap)res.adaptTo(ValueMap.class);
}
return this.data;
}
public String toString() {
return this.getClass().getSimpleName() + "[ path: " + this.path + ", hasData: " + (this.data != null) + "]";
}
}
}