1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.infra.merge.result.impl.local;
19
20 import com.google.common.base.Preconditions;
21 import org.apache.shardingsphere.infra.util.json.JsonUtils;
22
23 import java.time.LocalDateTime;
24 import java.util.Collection;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.Properties;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32
33
34
35
36 public final class LocalDataQueryResultRow {
37
38 private final List<Object> data;
39
40 public LocalDataQueryResultRow(final Object... data) {
41 this.data = Stream.of(data).map(this::convert).collect(Collectors.toList());
42 }
43
44 private Object convert(final Object data) {
45 if (null == data || "null".equals(data)) {
46 return "";
47 }
48 if (data instanceof Optional) {
49 return ((Optional<?>) data).isPresent() ? convert(((Optional<?>) data).get()) : "";
50 }
51 if (data instanceof String) {
52 return data;
53 }
54 if (data instanceof Boolean || data instanceof Integer || data instanceof Long || data instanceof LocalDateTime) {
55 return data.toString();
56 }
57 if (data instanceof Enum) {
58 return ((Enum<?>) data).name();
59 }
60 if (data instanceof Properties) {
61 return ((Properties) data).isEmpty() ? "" : JsonUtils.toJsonString(convert((Properties) data));
62 }
63 if (data instanceof Map) {
64 return ((Map<?, ?>) data).isEmpty() ? "" : JsonUtils.toJsonString(data);
65 }
66 if (data instanceof Collection) {
67 return ((Collection<?>) data).isEmpty() ? "" : JsonUtils.toJsonString(data);
68 }
69 return JsonUtils.toJsonString(data);
70 }
71
72 private Map<Object, Object> convert(final Properties props) {
73 return props.keySet().stream().map(Object::toString).sorted().collect(Collectors.toMap(each -> each, props::get, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
74 }
75
76
77
78
79
80
81
82 public Object getCell(final int columnIndex) {
83 Preconditions.checkArgument(columnIndex > 0 && columnIndex < data.size() + 1);
84 return data.get(columnIndex - 1);
85 }
86 }