/
DataType.java
108 lines (90 loc) · 3.46 KB
/
DataType.java
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
package org.infinispan.scripting.impl;
import org.infinispan.commons.CacheException;
import org.infinispan.commons.marshall.Marshaller;
import org.infinispan.commons.util.Immutables;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
public enum DataType {
UTF8(new Utf8Transformer()),
DEFAULT(new DefaultTransformer());
public final Transformer transformer;
DataType(Transformer transformer) {
this.transformer = transformer;
}
public static DataType fromMime(String mime) {
switch (mime) {
case "text/plain; charset=utf-8":
return UTF8;
default:
return DEFAULT;
}
}
interface Transformer {
Map<String, ?> toDataType(Map<String, ?> objs, Optional<Marshaller> marshaller);
Object fromDataType(Object obj, Optional<Marshaller> marshaller);
}
static final class Utf8Transformer implements Transformer {
public static final Charset CHARSET_UTF8 = Charset.forName("UTF-8");
@Override
public Map<String, ?> toDataType(Map<String, ?> objs, Optional<Marshaller> marshaller) {
return objs.entrySet().stream().map(e -> {
Object v = e.getValue();
Object entryValue = v instanceof byte[] ? asString(v) : v;
return Immutables.immutableEntry(e.getKey(), entryValue);
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
@Override
@SuppressWarnings("unchecked")
public Object fromDataType(Object obj, Optional<Marshaller> marshaller) {
if (obj instanceof List)
return ((List) obj).stream()
.map(Object::toString)
.collect(Collectors.joining("\", \"", "[\"", "\"]"))
.toString().getBytes(CHARSET_UTF8);
return Objects.isNull(obj) ? null : obj.toString().getBytes(CHARSET_UTF8);
}
private static String asString(Object v) {
return new String((byte[]) v, CHARSET_UTF8);
}
}
static final class DefaultTransformer implements Transformer {
@Override
public Map<String, ?> toDataType(Map<String, ?> objs, Optional<Marshaller> marshaller) {
if (marshaller.isPresent()) {
Marshaller m = marshaller.get();
return objs.entrySet().stream().map(e -> {
Object v = e.getValue();
Object entryValue = v instanceof byte[] ? fromBytes(v, m) : v;
return Immutables.immutableEntry(e.getKey(), entryValue);
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
return objs;
}
private static Object fromBytes(Object obj, Marshaller marshaller) {
try {
return marshaller.objectFromByteBuffer((byte[]) obj);
} catch (Exception e) {
throw new CacheException(e);
}
}
@Override
public Object fromDataType(Object obj, Optional<Marshaller> marshaller) {
try {
return marshaller.map(m -> toBytes(obj, m)).orElse(obj);
} catch (Exception e) {
throw new CacheException(e);
}
}
private static Object toBytes(Object obj, Marshaller marshaller) {
try {
return obj instanceof byte[] ? obj : marshaller.objectToByteBuffer(obj);
} catch (Exception e) {
throw new CacheException(e);
}
}
}
}