Project

General

Profile

1
package eu.dnetlib.clients.index.model.impl;
2

    
3
import java.util.*;
4
import java.util.stream.Collectors;
5

    
6
import eu.dnetlib.clients.index.model.*;
7

    
8
/**
9
 * Sequence of Any objects.
10
 */
11
public final class AnySeqImpl extends AbstractAny implements AnySeq {
12

    
13
	/**
14
	 * version.
15
	 */
16
	private static final long serialVersionUID = 1L;
17

    
18
	/**
19
	 * holds the Any objects of this AnySeq.
20
	 */
21
	private final List<Any> _anyList;
22

    
23
	/**
24
	 * constructs a new instance of AnySeqImpl.
25
	 */
26
	AnySeqImpl() {
27
		super(ValueType.SEQ);
28
		_anyList = new ArrayList<Any>();
29
	}
30

    
31
	/**
32
	 * {@inheritDoc}
33
	 */
34
	@Override
35
	public boolean add(final Any value) {
36
		if (value == null) {
37
			throw new IllegalArgumentException("The value of any Any must not be null.");
38
		}
39
		return _anyList.add(value);
40
	}
41

    
42
	/**
43
	 * {@inheritDoc}
44
	 */
45
	@Override
46
	public boolean add(final String e) {
47
		return add(new ValueImpl(ValueType.STRING, e));
48
	}
49

    
50
	/**
51
	 * {@inheritDoc}
52
	 */
53
	@Override
54
	public boolean add(final Number n) {
55
		if (n instanceof Double) {
56
			return add(new ValueImpl(ValueType.DOUBLE, n));
57
		} else if (n instanceof Long) {
58
			return add(new ValueImpl(ValueType.LONG, n));
59
		} else if (n instanceof Integer || n instanceof Short || n instanceof Byte) {
60
			return add(new ValueImpl(ValueType.LONG, Long.valueOf(n.longValue())));
61
		} else { // default: DOUBLE
62
			return add(new ValueImpl(ValueType.DOUBLE, Double.valueOf(n.doubleValue())));
63
		}
64
	}
65

    
66
	/**
67
	 * {@inheritDoc}
68
	 */
69
	@Override
70
	public void add(final int index, final Any element) {
71
		if (element == null) {
72
			throw new IllegalArgumentException("The value of any Any must not be null.");
73
		}
74
		_anyList.add(index, element);
75
	}
76

    
77
	/**
78
	 * {@inheritDoc}
79
	 */
80
	@Override
81
	public boolean addAll(final Collection<? extends Any> c) {
82
		return _anyList.addAll(c);
83
	}
84

    
85
	/**
86
	 * {@inheritDoc}
87
	 */
88
	@Override
89
	public boolean addAll(final int index, final Collection<? extends Any> c) {
90
		return _anyList.addAll(index, c);
91
	}
92

    
93
	/**
94
	 * {@inheritDoc}
95
	 */
96
	@Override
97
	public void clear() {
98
		_anyList.clear();
99
	}
100

    
101
	/**
102
	 * {@inheritDoc}
103
	 */
104
	@Override
105
	public boolean contains(final Object o) {
106
		return _anyList.contains(o);
107
	}
108

    
109
	/**
110
	 * {@inheritDoc}
111
	 */
112
	@Override
113
	public boolean containsAll(final Collection<?> c) {
114
		return _anyList.containsAll(c);
115
	}
116

    
117
	/**
118
	 * {@inheritDoc}
119
	 */
120
	@Override
121
	public Any get(final int index) {
122
		return _anyList.get(index);
123
	}
124

    
125
	/**
126
	 * {@inheritDoc}
127
	 */
128
	@Override
129
	public Any set(final int index, final Any element) {
130
		if (element == null) {
131
			throw new IllegalArgumentException("The value of any Any must not be null.");
132
		}
133
		return _anyList.set(index, element);
134
	}
135

    
136
	/**
137
	 * {@inheritDoc}
138
	 */
139
	@Override
140
	public int indexOf(final Object o) {
141
		return _anyList.indexOf(o);
142
	}
143

    
144
	/**
145
	 * {@inheritDoc}
146
	 */
147
	@Override
148
	public boolean isEmpty() {
149
		return _anyList.isEmpty();
150
	}
151

    
152
	/**
153
	 * {@inheritDoc}
154
	 */
155
	@Override
156
	public Iterator<Any> iterator() {
157
		return _anyList.iterator();
158
	}
159

    
160
	/**
161
	 * {@inheritDoc}
162
	 */
163
	@Override
164
	public int lastIndexOf(final Object o) {
165
		return _anyList.lastIndexOf(o);
166
	}
167

    
168
	/**
169
	 * {@inheritDoc}
170
	 */
171
	@Override
172
	public ListIterator<Any> listIterator() {
173
		return _anyList.listIterator();
174
	}
175

    
176
	/**
177
	 * {@inheritDoc}
178
	 */
179
	@Override
180
	public ListIterator<Any> listIterator(final int index) {
181
		return _anyList.listIterator(index);
182
	}
183

    
184
	/**
185
	 * {@inheritDoc}
186
	 */
187
	@Override
188
	public Any remove(final int index) {
189
		return _anyList.remove(index);
190
	}
191

    
192
	/**
193
	 * {@inheritDoc}
194
	 */
195
	@Override
196
	public boolean remove(final Object o) {
197
		return _anyList.remove(o);
198
	}
199

    
200
	/**
201
	 * {@inheritDoc}
202
	 */
203
	@Override
204
	public boolean removeAll(final Collection<?> c) {
205
		return _anyList.removeAll(c);
206
	}
207

    
208
	/**
209
	 * {@inheritDoc}
210
	 */
211
	@Override
212
	public boolean retainAll(final Collection<?> c) {
213
		return _anyList.retainAll(c);
214
	}
215

    
216
	/**
217
	 * {@inheritDoc}
218
	 */
219
	@Override
220
	public int size() {
221
		return _anyList.size();
222
	}
223

    
224
	/**
225
	 * {@inheritDoc}
226
	 */
227
	@Override
228
	public List<Any> subList(final int fromIndex, final int toIndex) {
229
		return _anyList.subList(fromIndex, toIndex);
230
	}
231

    
232
	/**
233
	 * {@inheritDoc}
234
	 */
235
	@Override
236
	public Object[] toArray() {
237
		return _anyList.toArray();
238
	}
239

    
240
	/**
241
	 * {@inheritDoc}
242
	 */
243
	@Override
244
	public <T> T[] toArray(final T[] a) {
245
		return _anyList.toArray(a);
246
	}
247

    
248
	/**
249
	 * {@inheritDoc}
250
	 */
251
	@Override
252
	public AnyMap getMap(final int index) {
253
		final Any anyValue = get(index);
254
		if (anyValue != null) {
255
			if (anyValue.isMap()) {
256
				return (AnyMap) anyValue;
257
			} else {
258
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to AnyMap.");
259
			}
260
		}
261
		return null;
262
	}
263

    
264
	/**
265
	 * {@inheritDoc}
266
	 */
267
	@Override
268
	public AnySeq getSeq(final int index) {
269
		final Any anyValue = get(index);
270
		if (anyValue != null) {
271
			if (anyValue.isSeq()) {
272
				return (AnySeq) anyValue;
273
			} else {
274
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to AnySeq.");
275
			}
276
		}
277
		return null;
278
	}
279

    
280
	/**
281
	 * {@inheritDoc}
282
	 */
283
	@Override
284
	public Value getValue(final int index) {
285
		final Any anyValue = get(index);
286
		if (anyValue != null) {
287
			if (anyValue instanceof Value) {
288
				return (Value) anyValue;
289
			} else {
290
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to Value.");
291
			}
292
		}
293
		return null;
294
	}
295

    
296
	/**
297
	 * {@inheritDoc}
298
	 */
299
	@Override
300
	public String getStringValue(final int index) {
301
		final Any anyValue = get(index);
302
		if (anyValue != null) {
303
			if (anyValue instanceof Value) {
304
				return ((Value) anyValue).asString();
305
			} else {
306
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to String.");
307
			}
308
		}
309
		return null;
310
	}
311

    
312
	/**
313
	 * {@inheritDoc}
314
	 */
315
	@Override
316
	public Double getDoubleValue(final int index) {
317
		final Any anyValue = get(index);
318
		if (anyValue != null) {
319
			if (anyValue instanceof Value) {
320
				return ((Value) anyValue).asDouble();
321
			} else {
322
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to double.");
323
			}
324
		}
325
		return null;
326
	}
327

    
328
	/**
329
	 * {@inheritDoc}
330
	 */
331
	@Override
332
	public Long getLongValue(final int index) {
333
		final Any anyValue = get(index);
334
		if (anyValue != null) {
335
			if (anyValue instanceof Value) {
336
				return ((Value) anyValue).asLong();
337
			} else {
338
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to long.");
339
			}
340
		}
341
		return null;
342
	}
343

    
344
	/**
345
	 * {@inheritDoc}
346
	 */
347
	@Override
348
	public Boolean getBooleanValue(final int index) {
349
		final Any anyValue = get(index);
350
		if (anyValue != null) {
351
			if (anyValue instanceof Value) {
352
				return ((Value) anyValue).asBoolean();
353
			} else {
354
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to boolean.");
355
			}
356
		}
357
		return null;
358
	}
359

    
360
	/**
361
	 * {@inheritDoc}
362
	 */
363
	@Override
364
	public Date getDateValue(final int index) {
365
		final Any anyValue = get(index);
366
		if (anyValue != null) {
367
			if (anyValue instanceof Value) {
368
				return ((Value) anyValue).asDate();
369
			} else {
370
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to Date.");
371
			}
372
		}
373
		return null;
374
	}
375

    
376
	/**
377
	 * {@inheritDoc}
378
	 */
379
	@Override
380
	public Date getDateTimeValue(final int index) {
381
		final Any anyValue = get(index);
382
		if (anyValue != null) {
383
			if (anyValue instanceof Value) {
384
				return ((Value) anyValue).asDateTime();
385
			} else {
386
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to DateTime.");
387
			}
388
		}
389
		return null;
390
	}
391

    
392
	/**
393
	 * {@inheritDoc}
394
	 */
395
	@Override
396
	public int hashCode() {
397
		final int prime = 31;
398
		int result = 1;
399
		result = prime * result + (_anyList == null ? 0 : _anyList.hashCode());
400
		return result;
401
	}
402

    
403
	/**
404
	 * {@inheritDoc}
405
	 */
406
	@Override
407
	public boolean equals(final Object obj) {
408
		if (this == obj) {
409
			return true;
410
		}
411
		if (obj == null) {
412
			return false;
413
		}
414
		if (!(obj instanceof AnySeqImpl)) {
415
			return false;
416
		}
417
		final AnySeqImpl other = (AnySeqImpl) obj;
418
		if (_anyList == null) {
419
			if (other._anyList != null) {
420
				return false;
421
			}
422
		} else if (!_anyList.equals(other._anyList)) {
423
			return false;
424
		}
425
		return true;
426
	}
427

    
428
	/**
429
	 * {@inheritDoc}
430
	 */
431
	@Override
432
	public String toString() {
433
		return _anyList.toString();
434
	}
435

    
436
	/**
437
	 * {@inheritDoc}
438
	 */
439
	@Override
440
	public AnySeq asSeq() {
441
		return this;
442
	}
443

    
444
	/**
445
	 * {@inheritDoc}
446
	 */
447
	@Override
448
	public List<String> asStrings() {
449
		final List<String> values = new ArrayList<>(_anyList.size());
450
		values.addAll(_anyList.stream().map(any -> any.asValue().asString()).collect(Collectors.toList()));
451
		return values;
452
	}
453

    
454
	/**
455
	 * {@inheritDoc}
456
	 */
457
	@Override
458
	public List<Long> asLongs() {
459
		return _anyList.stream().map(any -> any.asValue().asLong()).collect(Collectors.toList());
460
	}
461

    
462
}
(3-3/7)