SimpleMetaDataMap.java
2.22 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.cq.workflow.metadata;
import com.day.cq.workflow.metadata.MetaDataMap;
import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.LinkedList;
public class SimpleMetaDataMap
extends HashMap<String, Object>
implements MetaDataMap {
private static final long serialVersionUID = 1360764073724170383L;
@Override
public <T> T get(String name, Class<T> type) {
return this.convert(this.get(name), type);
}
@Override
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;
}
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) {
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);
}
}