1
|
package eu.dnetlib.data.mapreduce.hbase.broker.model;
|
2
|
|
3
|
import java.util.List;
|
4
|
import java.util.stream.Collectors;
|
5
|
|
6
|
import eu.dnetlib.data.mapreduce.hbase.broker.mapping.DateParser;
|
7
|
import org.apache.commons.lang3.BooleanUtils;
|
8
|
import org.apache.commons.lang3.StringUtils;
|
9
|
import org.apache.commons.lang3.math.NumberUtils;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
|
13
|
public class MapValue {
|
14
|
|
15
|
private MapValueType type = MapValueType.STRING;
|
16
|
private Object value = "";
|
17
|
|
18
|
private static final Log log = LogFactory.getLog(MapValue.class);
|
19
|
|
20
|
public MapValue() {}
|
21
|
|
22
|
public MapValue(final MapValueType type, final Object value) {
|
23
|
this.type = type;
|
24
|
this.value = value;
|
25
|
}
|
26
|
|
27
|
public MapValueType getType() {
|
28
|
return this.type;
|
29
|
}
|
30
|
|
31
|
public void setType(final MapValueType type) {
|
32
|
this.type = type;
|
33
|
}
|
34
|
|
35
|
public Object getValue() {
|
36
|
return this.value;
|
37
|
}
|
38
|
|
39
|
public void setValue(final Object value) {
|
40
|
this.value = value;
|
41
|
}
|
42
|
|
43
|
public Object asObject() {
|
44
|
if (this.value == null) {
|
45
|
log.warn("Object is NULL");
|
46
|
return null;
|
47
|
}
|
48
|
try {
|
49
|
switch (this.type) {
|
50
|
case STRING:
|
51
|
case INTEGER:
|
52
|
case FLOAT:
|
53
|
case BOOLEAN:
|
54
|
case DATE:
|
55
|
return asSimpleObject(getType(), getValue().toString());
|
56
|
case LIST_STRING:
|
57
|
return ((List<?>) getValue()).stream().map(o -> asSimpleObject(MapValueType.STRING, o.toString())).collect(Collectors.toList());
|
58
|
case LIST_INTEGER:
|
59
|
return ((List<?>) getValue()).stream().map(o -> asSimpleObject(MapValueType.INTEGER, o.toString())).collect(Collectors.toList());
|
60
|
case LIST_FLOAT:
|
61
|
return ((List<?>) getValue()).stream().map(o -> asSimpleObject(MapValueType.FLOAT, o.toString())).collect(Collectors.toList());
|
62
|
case LIST_BOOLEAN:
|
63
|
return ((List<?>) getValue()).stream().map(o -> asSimpleObject(MapValueType.BOOLEAN, o.toString())).collect(Collectors.toList());
|
64
|
case LIST_DATE:
|
65
|
return ((List<?>) getValue()).stream().map(o -> asSimpleObject(MapValueType.DATE, o.toString())).collect(Collectors.toList());
|
66
|
default:
|
67
|
log.warn("Invalid type: " + this.type);
|
68
|
return null;
|
69
|
}
|
70
|
} catch (final Exception e) {
|
71
|
log.warn("Error parsing value: " + this);
|
72
|
return null;
|
73
|
}
|
74
|
}
|
75
|
|
76
|
private Object asSimpleObject(final MapValueType type, final String s) {
|
77
|
try {
|
78
|
switch (type) {
|
79
|
case STRING:
|
80
|
return s.toString();
|
81
|
case INTEGER:
|
82
|
return s.contains(".") ? NumberUtils.toLong(StringUtils.substringBefore(s, "."), 0) : NumberUtils.toLong(s, 0);
|
83
|
case FLOAT:
|
84
|
return NumberUtils.toDouble(s, 0);
|
85
|
case BOOLEAN:
|
86
|
return BooleanUtils.toBoolean(s);
|
87
|
case DATE:
|
88
|
return DateParser.parse(s);
|
89
|
default:
|
90
|
log.warn("Unmamaged type: " + type);
|
91
|
return null;
|
92
|
}
|
93
|
} catch (final Exception e) {
|
94
|
log.warn("Error parsing value: " + s + " - type: " + type);
|
95
|
return null;
|
96
|
}
|
97
|
}
|
98
|
|
99
|
@Override
|
100
|
public String toString() {
|
101
|
return String.format("[ type; %s, value: %s ]", getType(), getValue());
|
102
|
}
|
103
|
}
|