Project

General

Profile

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

    
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.Date;
6
import java.util.Iterator;
7
import java.util.List;
8
import java.util.ListIterator;
9

    
10
import eu.dnetlib.functionality.index.model.Any;
11
import eu.dnetlib.functionality.index.model.AnyMap;
12
import eu.dnetlib.functionality.index.model.AnySeq;
13
import eu.dnetlib.functionality.index.model.InvalidValueTypeException;
14
import eu.dnetlib.functionality.index.model.Value;
15

    
16
/**
17
 * Sequence of Any objects.
18
 */
19
public final class AnySeqImpl extends AbstractAny implements AnySeq {
20

    
21
	/** version. */
22
	private static final long serialVersionUID = 1L;
23

    
24
	/** holds the Any objects of this AnySeq. */
25
	private final List<Any> _anyList;
26

    
27
	/**
28
	 * constructs a new instance of AnySeqImpl.
29
	 */
30
	AnySeqImpl() {
31
		super(ValueType.SEQ);
32
		_anyList = new ArrayList<Any>();
33
	}
34

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

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

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

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

    
73
	/** {@inheritDoc} */
74
	@Override
75
	public boolean addAll(final Collection<? extends Any> c) {
76
		return _anyList.addAll(c);
77
	}
78

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

    
85
	/** {@inheritDoc} */
86
	@Override
87
	public void clear() {
88
		_anyList.clear();
89
	}
90

    
91
	/** {@inheritDoc} */
92
	@Override
93
	public boolean contains(final Object o) {
94
		return _anyList.contains(o);
95
	}
96

    
97
	/** {@inheritDoc} */
98
	@Override
99
	public boolean containsAll(final Collection<?> c) {
100
		return _anyList.containsAll(c);
101
	}
102

    
103
	/** {@inheritDoc} */
104
	@Override
105
	public Any get(final int index) {
106
		return _anyList.get(index);
107
	}
108

    
109
	/** {@inheritDoc} */
110
	@Override
111
	public Any set(final int index, final Any element) {
112
		if (element == null) {
113
			throw new IllegalArgumentException("The value of any Any must not be null.");
114
		}
115
		return _anyList.set(index, element);
116
	}
117

    
118
	/** {@inheritDoc} */
119
	@Override
120
	public int indexOf(final Object o) {
121
		return _anyList.indexOf(o);
122
	}
123

    
124
	/** {@inheritDoc} */
125
	@Override
126
	public boolean isEmpty() {
127
		return _anyList.isEmpty();
128
	}
129

    
130
	/** {@inheritDoc} */
131
	@Override
132
	public Iterator<Any> iterator() {
133
		return _anyList.iterator();
134
	}
135

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

    
142
	/** {@inheritDoc} */
143
	@Override
144
	public ListIterator<Any> listIterator() {
145
		return _anyList.listIterator();
146
	}
147

    
148
	/** {@inheritDoc} */
149
	@Override
150
	public ListIterator<Any> listIterator(final int index) {
151
		return _anyList.listIterator(index);
152
	}
153

    
154
	/** {@inheritDoc} */
155
	@Override
156
	public Any remove(final int index) {
157
		return _anyList.remove(index);
158
	}
159

    
160
	/** {@inheritDoc} */
161
	@Override
162
	public boolean remove(final Object o) {
163
		return _anyList.remove(o);
164
	}
165

    
166
	/** {@inheritDoc} */
167
	@Override
168
	public boolean removeAll(final Collection<?> c) {
169
		return _anyList.removeAll(c);
170
	}
171

    
172
	/** {@inheritDoc} */
173
	@Override
174
	public boolean retainAll(final Collection<?> c) {
175
		return _anyList.retainAll(c);
176
	}
177

    
178
	/** {@inheritDoc} */
179
	@Override
180
	public int size() {
181
		return _anyList.size();
182
	}
183

    
184
	/** {@inheritDoc} */
185
	@Override
186
	public List<Any> subList(final int fromIndex, final int toIndex) {
187
		return _anyList.subList(fromIndex, toIndex);
188
	}
189

    
190
	/** {@inheritDoc} */
191
	@Override
192
	public Object[] toArray() {
193
		return _anyList.toArray();
194
	}
195

    
196
	/** {@inheritDoc} */
197
	@Override
198
	public <T> T[] toArray(final T[] a) {
199
		return _anyList.toArray(a);
200
	}
201

    
202
	/** {@inheritDoc} */
203
	@Override
204
	public AnyMap getMap(final int index) {
205
		final Any anyValue = get(index);
206
		if (anyValue != null) {
207
			if (anyValue.isMap()) {
208
				return (AnyMap) anyValue;
209
			} else {
210
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to AnyMap.");
211
			}
212
		}
213
		return null;
214
	}
215

    
216
	/** {@inheritDoc} */
217
	@Override
218
	public AnySeq getSeq(final int index) {
219
		final Any anyValue = get(index);
220
		if (anyValue != null) {
221
			if (anyValue.isSeq()) {
222
				return (AnySeq) anyValue;
223
			} else {
224
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to AnySeq.");
225
			}
226
		}
227
		return null;
228
	}
229

    
230
	/** {@inheritDoc} */
231
	@Override
232
	public Value getValue(final int index) {
233
		final Any anyValue = get(index);
234
		if (anyValue != null) {
235
			if (anyValue instanceof Value) {
236
				return (Value) anyValue;
237
			} else {
238
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to Value.");
239
			}
240
		}
241
		return null;
242
	}
243

    
244
	/** {@inheritDoc} */
245
	@Override
246
	public String getStringValue(final int index) {
247
		final Any anyValue = get(index);
248
		if (anyValue != null) {
249
			if (anyValue instanceof Value) {
250
				return ((Value) anyValue).asString();
251
			} else {
252
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to String.");
253
			}
254
		}
255
		return null;
256
	}
257

    
258
	/** {@inheritDoc} */
259
	@Override
260
	public Double getDoubleValue(final int index) {
261
		final Any anyValue = get(index);
262
		if (anyValue != null) {
263
			if (anyValue instanceof Value) {
264
				return ((Value) anyValue).asDouble();
265
			} else {
266
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to double.");
267
			}
268
		}
269
		return null;
270
	}
271

    
272
	/** {@inheritDoc} */
273
	@Override
274
	public Long getLongValue(final int index) {
275
		final Any anyValue = get(index);
276
		if (anyValue != null) {
277
			if (anyValue instanceof Value) {
278
				return ((Value) anyValue).asLong();
279
			} else {
280
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to long.");
281
			}
282
		}
283
		return null;
284
	}
285

    
286
	/** {@inheritDoc} */
287
	@Override
288
	public Boolean getBooleanValue(final int index) {
289
		final Any anyValue = get(index);
290
		if (anyValue != null) {
291
			if (anyValue instanceof Value) {
292
				return ((Value) anyValue).asBoolean();
293
			} else {
294
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to boolean.");
295
			}
296
		}
297
		return null;
298
	}
299

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

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

    
328
	/** {@inheritDoc} */
329
	@Override
330
	public int hashCode() {
331
		final int prime = 31;
332
		int result = 1;
333
		result = prime * result + (_anyList == null ? 0 : _anyList.hashCode());
334
		return result;
335
	}
336

    
337
	/** {@inheritDoc} */
338
	@Override
339
	public boolean equals(final Object obj) {
340
		if (this == obj) {
341
			return true;
342
		}
343
		if (obj == null) {
344
			return false;
345
		}
346
		if (!(obj instanceof AnySeqImpl)) {
347
			return false;
348
		}
349
		final AnySeqImpl other = (AnySeqImpl) obj;
350
		if (_anyList == null) {
351
			if (other._anyList != null) {
352
				return false;
353
			}
354
		} else if (!_anyList.equals(other._anyList)) {
355
			return false;
356
		}
357
		return true;
358
	}
359

    
360
	/**
361
	 * {@inheritDoc}
362
	 */
363
	@Override
364
	public String toString() {
365
		return _anyList.toString();
366
	}
367

    
368
	@Override
369
	public AnySeq asSeq() {
370
		return this;
371
	}
372

    
373
	@Override
374
	public List<String> asStrings() {
375
		final List<String> values = new ArrayList<String>(_anyList.size());
376
		for (final Any any : _anyList) {
377
			values.add(any.asValue().asString());
378
		}
379
		return values;
380
	}
381

    
382
	@Override
383
	public List<Long> asLongs() {
384
		final List<Long> values = new ArrayList<Long>(_anyList.size());
385
		for (final Any any : _anyList) {
386
			values.add(any.asValue().asLong());
387
		}
388
		return values;
389
	}
390

    
391
}
(3-3/7)