ValueMapDecorator.java
3.71 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.jackrabbit.util.ISO8601
* org.apache.sling.api.resource.ValueMap
*/
package com.adobe.cq.commerce.common;
import java.lang.reflect.Array;
import java.util.Calendar;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.apache.jackrabbit.util.ISO8601;
import org.apache.sling.api.resource.ValueMap;
public class ValueMapDecorator
implements ValueMap {
private final Map<String, Object> base;
public ValueMapDecorator(Map<String, Object> base) {
this.base = base;
}
public <T> T get(String name, Class<T> type) {
return this.convert(this.get(name), type);
}
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 (type == String.class) {
if (obj instanceof Calendar) {
return (T)ISO8601.format((Calendar)((Calendar)obj));
}
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) {
if (obj.getClass().isArray()) {
Object[] array = (Object[])obj;
Object[] result = (Object[])Array.newInstance(type, array.length);
for (int i = 0; i < array.length; ++i) {
result[i] = this.convert(array[i], type);
}
return result;
}
Object[] result = (Object[])Array.newInstance(type, 1);
result[0] = this.convert(obj, type);
return result;
}
public <T> T get(String name, T defaultValue) {
if (defaultValue == null) {
return (T)this.get(name);
}
Class value = this.get(name, (T)defaultValue.getClass());
return value == null ? defaultValue : value;
}
public int size() {
return this.base.size();
}
public boolean isEmpty() {
return this.base.isEmpty();
}
public boolean containsKey(Object key) {
return this.base.containsKey(key);
}
public boolean containsValue(Object value) {
return this.base.containsValue(value);
}
public Object get(Object key) {
return this.base.get(key);
}
public Object put(String key, Object value) {
return this.base.put(key, value);
}
public Object remove(Object key) {
return this.base.remove(key);
}
public void putAll(Map<? extends String, ?> t) {
this.base.putAll(t);
}
public void clear() {
this.base.clear();
}
public Set<String> keySet() {
return this.base.keySet();
}
public Collection<Object> values() {
return this.base.values();
}
public Set<Map.Entry<String, Object>> entrySet() {
return this.base.entrySet();
}
public String toString() {
return super.toString() + " : " + this.base.toString();
}
}