1
|
package eu.dnetlib.clients.index.model.impl;
|
2
|
|
3
|
import java.util.Date;
|
4
|
import java.util.Iterator;
|
5
|
import java.util.Map.Entry;
|
6
|
|
7
|
import eu.dnetlib.clients.index.model.*;
|
8
|
import eu.dnetlib.clients.index.model.Any.ValueType;
|
9
|
import org.apache.commons.lang3.NotImplementedException;
|
10
|
|
11
|
import static java.lang.String.format;
|
12
|
|
13
|
/**
|
14
|
* Implementation of DataFactory.
|
15
|
*/
|
16
|
public class DefaultDataFactoryImpl implements DataFactory {
|
17
|
|
18
|
/**
|
19
|
* instance for sharing.
|
20
|
*/
|
21
|
public static final DefaultDataFactoryImpl INSTANCE = new DefaultDataFactoryImpl();
|
22
|
|
23
|
/**
|
24
|
* immutable empyty map.
|
25
|
*/
|
26
|
public static final AnyMap IMMUTABLE_EMPTY_MAP = new ImmutableAnyMapImpl(INSTANCE.createAnyMap());
|
27
|
|
28
|
/**
|
29
|
* {@inheritDoc}
|
30
|
*/
|
31
|
@Override
|
32
|
public AnyMap createAnyMap() {
|
33
|
return new AnyMapImpl();
|
34
|
}
|
35
|
|
36
|
/**
|
37
|
* {@inheritDoc}
|
38
|
*/
|
39
|
@Override
|
40
|
public AnySeq createAnySeq() {
|
41
|
return new AnySeqImpl();
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* {@inheritDoc}
|
46
|
*/
|
47
|
@Override
|
48
|
public Value createStringValue(final String value) {
|
49
|
return new ValueImpl(ValueType.STRING, value);
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* {@inheritDoc}
|
54
|
*/
|
55
|
@Override
|
56
|
public Value createBooleanValue(final Boolean value) {
|
57
|
return new ValueImpl(ValueType.BOOLEAN, value);
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* {@inheritDoc}
|
62
|
*/
|
63
|
@Override
|
64
|
public Value createLongValue(final Long value) {
|
65
|
return new ValueImpl(ValueType.LONG, value);
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* {@inheritDoc}
|
70
|
*/
|
71
|
@Override
|
72
|
public Value createLongValue(final int value) {
|
73
|
return new ValueImpl(ValueType.LONG, (long) value);
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* {@inheritDoc}
|
78
|
*/
|
79
|
@Override
|
80
|
public Value createDoubleValue(final Double value) {
|
81
|
return new ValueImpl(ValueType.DOUBLE, value);
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* {@inheritDoc}
|
86
|
*/
|
87
|
@Override
|
88
|
public Value createDoubleValue(final float value) {
|
89
|
return new ValueImpl(ValueType.DOUBLE, Double.valueOf(value));
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* {@inheritDoc}
|
94
|
*/
|
95
|
@Override
|
96
|
public Value createDateValue(final Date value) {
|
97
|
return new ValueImpl(ValueType.DATE, new Date(value.getTime()));
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* create DATE value with original string.
|
102
|
*/
|
103
|
private Value createDateValue(final Date value, final String originalString) {
|
104
|
return new ValueImpl(ValueType.DATE, new DateValue(new Date(value.getTime()), originalString));
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* {@inheritDoc}
|
109
|
*/
|
110
|
@Override
|
111
|
public Value createDateTimeValue(final Date value) {
|
112
|
return new ValueImpl(ValueType.DATETIME, new Date(value.getTime()));
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* create DATETIME value with original string.
|
117
|
*/
|
118
|
private Value createDateTimeValue(final Date value, final String originalString) {
|
119
|
return new ValueImpl(ValueType.DATETIME, new DateValue(new Date(value.getTime()), originalString));
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* {@inheritDoc}
|
124
|
*/
|
125
|
@Override
|
126
|
public Value parseFromString(final String value, final String type) {
|
127
|
ValueType valueType = null;
|
128
|
if (type == null) {
|
129
|
valueType = ValueType.STRING;
|
130
|
} else {
|
131
|
try {
|
132
|
valueType = ValueType.valueOf(type.toUpperCase());
|
133
|
} catch (final IllegalArgumentException e) {
|
134
|
throw new InvalidValueTypeException("invalid type: " + type);
|
135
|
}
|
136
|
}
|
137
|
return parseFromString(value, valueType);
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* {@inheritDoc}
|
142
|
*/
|
143
|
@Override
|
144
|
public Value parseFromString(final String value, final ValueType valueType) {
|
145
|
try {
|
146
|
switch (valueType) {
|
147
|
case BOOLEAN:
|
148
|
return createBooleanValue(Boolean.parseBoolean(value));
|
149
|
case DATE:
|
150
|
return createDateValue(ValueFormatHelper.INSTANCE.parseDate(value), value);
|
151
|
case DATETIME:
|
152
|
return createDateTimeValue(ValueFormatHelper.INSTANCE.parseDateTime(value), value);
|
153
|
case DOUBLE:
|
154
|
return createDoubleValue(Double.parseDouble(value));
|
155
|
case LONG:
|
156
|
return createLongValue(Long.parseLong(value));
|
157
|
case STRING:
|
158
|
return createStringValue(value);
|
159
|
default:
|
160
|
throw new NotImplementedException("conversion for type: " + valueType);
|
161
|
}
|
162
|
} catch (final Exception e) {
|
163
|
throw new InvalidValueTypeException(format("cannot convert value %s into %s", value, valueType), e);
|
164
|
}
|
165
|
}
|
166
|
|
167
|
/**
|
168
|
* {@inheritDoc}
|
169
|
*/
|
170
|
@Override
|
171
|
public Value tryDateTimestampParsingFromString(final String value) {
|
172
|
final Date timestamp = ValueFormatHelper.INSTANCE.tryParseDateTime(value);
|
173
|
if (timestamp != null) {
|
174
|
return createDateTimeValue(timestamp, value);
|
175
|
}
|
176
|
final Date date = ValueFormatHelper.INSTANCE.tryParseDate(value);
|
177
|
if (date != null) {
|
178
|
return createDateValue(date, value);
|
179
|
}
|
180
|
return createStringValue(value);
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* {@inheritDoc}
|
185
|
*
|
186
|
* @deprecated Use {@link #autoConvertValue(Object)} instead
|
187
|
*/
|
188
|
@Deprecated
|
189
|
@Override
|
190
|
public Value parseFromObject(final Object object) {
|
191
|
return autoConvertValue(object);
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* {@inheritDoc}
|
196
|
*/
|
197
|
@Override
|
198
|
public Value autoConvertValue(final Object object) {
|
199
|
if (object instanceof Value) {
|
200
|
return (Value) object;
|
201
|
} else if (object instanceof String) {
|
202
|
return createStringValue((String) object);
|
203
|
} else if (object instanceof Double) {
|
204
|
return createDoubleValue((Double) object);
|
205
|
} else if (object instanceof Long) {
|
206
|
return createLongValue((Long) object);
|
207
|
} else if (object instanceof Integer) {
|
208
|
return createLongValue(((Integer) object).longValue());
|
209
|
} else if (object instanceof Short) {
|
210
|
return createLongValue(((Short) object).longValue());
|
211
|
} else if (object instanceof Byte) {
|
212
|
return createLongValue(((Byte) object).longValue());
|
213
|
} else if (object instanceof Number) {
|
214
|
return createDoubleValue(((Number) object).doubleValue());
|
215
|
} else if (object instanceof Boolean) {
|
216
|
return createBooleanValue((Boolean) object);
|
217
|
} else if (object instanceof Date) {
|
218
|
return createDateTimeValue((Date) object);
|
219
|
} else {
|
220
|
throw new InvalidValueTypeException(object.getClass());
|
221
|
}
|
222
|
}
|
223
|
|
224
|
/**
|
225
|
* {@inheritDoc}
|
226
|
*/
|
227
|
@Override
|
228
|
public Any cloneAny(final Any source) {
|
229
|
if (source == null) {
|
230
|
return null;
|
231
|
}
|
232
|
switch (source.getValueType()) {
|
233
|
case MAP:
|
234
|
return cloneAnyMap((AnyMap) source);
|
235
|
case SEQ:
|
236
|
return cloneAnySeq((AnySeq) source);
|
237
|
default:
|
238
|
return new ValueImpl((Value) source);
|
239
|
}
|
240
|
}
|
241
|
|
242
|
/**
|
243
|
* {@inheritDoc}
|
244
|
*/
|
245
|
@Override
|
246
|
public AnyMap cloneAnyMap(final AnyMap source) {
|
247
|
if (source == null) {
|
248
|
return null;
|
249
|
}
|
250
|
final AnyMap destination = createAnyMap();
|
251
|
for (final Entry<String, Any> entry : source.entrySet()) {
|
252
|
destination.put(entry.getKey(), cloneAny(entry.getValue()));
|
253
|
}
|
254
|
return destination;
|
255
|
}
|
256
|
|
257
|
/**
|
258
|
* {@inheritDoc}
|
259
|
*/
|
260
|
@Override
|
261
|
public AnySeq cloneAnySeq(final AnySeq source) {
|
262
|
if (source == null) {
|
263
|
return null;
|
264
|
}
|
265
|
final AnySeq destination = createAnySeq();
|
266
|
final Iterator<Any> iterator = source.iterator();
|
267
|
while (iterator.hasNext()) {
|
268
|
destination.add(cloneAny(iterator.next()));
|
269
|
}
|
270
|
return destination;
|
271
|
}
|
272
|
|
273
|
}
|