Skip to main content
summaryrefslogtreecommitdiffstats
blob: fb3784e4c4cfb3d20f0aeb291ebc4b78a187c0b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/********************************************************************************
 * Copyright (c) 2015-2018 Contributors to the Eclipse Foundation
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 ********************************************************************************/


package org.eclipse.mdm.api.base.model;

import java.lang.reflect.Array;
import java.time.LocalDateTime;

/**
 * Scalar type enumeration is a {@link MeasuredValues} factory. Another context
 * where this enumeration is used is the storage of values, represented by the
 * constants of this enumeration, in their {@code String} representation (see:
 * {@link BaseParameter}).
 *
 * @since 1.0.0
 * @author Viktor Stoehr, Gigatronik Ingolstadt GmbH
 * @author Sebastian Dirsch, Gigatronik Ingolstadt GmbH
 */

public final class ScalarType extends EnumerationValue {

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code String} values.
	 */
	public static final ScalarType STRING = new ScalarType(0, "STRING", ValueType.STRING_SEQUENCE, String.class);

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code LocalDateTime} values.
	 */
	public static final ScalarType DATE = new ScalarType(1, "DATE", ValueType.DATE_SEQUENCE, LocalDateTime.class);

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code boolean} values.
	 */
	public static final ScalarType BOOLEAN = new ScalarType(2, "BOOLEAN", ValueType.BOOLEAN_SEQUENCE, boolean.class);

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code byte} values.
	 */
	public static final ScalarType BYTE = new ScalarType(3, "BYTE", ValueType.BYTE_SEQUENCE, byte.class);

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code short} values.
	 */
	public static final ScalarType SHORT = new ScalarType(4, "SHORT", ValueType.SHORT_SEQUENCE, short.class);

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code int} values.
	 */
	public static final ScalarType INTEGER = new ScalarType(5, "INTEGER", ValueType.INTEGER_SEQUENCE, int.class);

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code long} values.
	 */
	public static final ScalarType LONG = new ScalarType(6, "LONG", ValueType.LONG_SEQUENCE, long.class);

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code float} values.
	 */
	public static final ScalarType FLOAT = new ScalarType(7, "FLOAT", ValueType.FLOAT_SEQUENCE, float.class);

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code double} values.
	 */
	public static final ScalarType DOUBLE = new ScalarType(8, "DOUBLE", ValueType.DOUBLE_SEQUENCE, double.class);

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code byte[]} values.
	 */
	public static final ScalarType BYTE_STREAM = new ScalarType(9, "BYTE_STREAM", ValueType.BYTE_STREAM_SEQUENCE, byte[].class);

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code FloatComplex} values.
	 */
	public static final ScalarType FLOAT_COMPLEX = new ScalarType(10, "FLOAT_COMPLEX", ValueType.FLOAT_COMPLEX_SEQUENCE,
			FloatComplex.class);

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code DoubleComplex} values.
	 */
	public static final ScalarType DOUBLE_COMPLEX = new ScalarType(11, "DOUBLE_COMPLEX", ValueType.DOUBLE_COMPLEX_SEQUENCE,
			DoubleComplex.class);

	/**
	 * {@link MeasuredValues} are not allowed to be of this type. This constant
	 * may be used in other contexts.
	 */
	public static final ScalarType ENUMERATION = new ScalarType(12, "ENUMERATION", ValueType.ENUMERATION_SEQUENCE);

	/**
	 * A {@link MeasuredValues} with this type contains an array sequence of
	 * {@code FileLink} values.
	 */
	public static final ScalarType FILE_LINK = new ScalarType(13, "FILE_LINK", ValueType.FILE_LINK_SEQUENCE, FileLink.class);

	/**
	 * TODO ...
	 */
	public static final ScalarType BLOB = new ScalarType(14, "BLOB", ValueType.BLOB, Object.class);

	/**
	 * {@link MeasuredValues} are not allowed to be of this type. This constant
	 * may be used in other contexts.
	 */
	public static final ScalarType UNKNOWN = new ScalarType(15, "UNKNOWN", ValueType.UNKNOWN);

	private final ValueType valueType;
	private final Class<?> arrayType;

	/**
	 * Constructor.
	 * 
	 * @param ord
	 *            ordinal number of the enum value
	 * @param name
	 *            name of the enum value
	 * @param valueType
	 *            The associated {@link ValueType}.
	 */
	private ScalarType(int ord, String name, ValueType valueType) {
		super(name, ord);
		this.valueType = valueType;
		arrayType = null;
	}

	/**
	 * Constructor.
	 *
	 * @param ord
	 *            ordinal number of the enum value
	 * @param name
	 *            name of the enum value
	 * @param valueType
	 *            The associated {@link ValueType}.
	 * @param componentType
	 *            The component type of the array held by instances of
	 *            {@link MeasuredValues}.
	 */
	private ScalarType(int ord, String name, ValueType valueType, Class<?> componentType) {
		super(name, ord);
		this.valueType = valueType;
		arrayType = Array.newInstance(componentType, 0).getClass();
	}

	/**
	 * Creates a new {@link MeasuredValues} initialized with given name, unit
	 * and values including the related validity flags.
	 *
	 * @param name
	 *            This is the name of the corresponding {@link Channel}.
	 * @param unit
	 *            Name of the unit the contained values are of.
	 * @param input
	 *            The measured values.
	 * @param flags
	 *            The validity flags of the measured values.
	 * @return The created {@code MeasuredValues} is returned.
	 * @throws IllegalStateException
	 *             Thrown if {@link #isEnumeration()} or {@link #isUnknown()}
	 *             returns {@code true}.
	 * @throws IllegalArgumentException
	 *             Thrown if given values are not assignment compatible.
	 */
	public MeasuredValues createMeasuredValues(String name, String unit, Object input, boolean[] flags) {
		if (isEnumeration() || isUnknown()) {
			throw new IllegalStateException("It is not allowed to create measured values of type '" + this + "'.");
		} else if (!arrayType.isInstance(input)) {
			throw new IllegalArgumentException("Given values of type '" + input.getClass() + "'.");
		}

		return new MeasuredValues(this, name, unit, input, flags);
	}

	/**
	 * Returns true if this scalar type is {@link #STRING}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isString() {
		return STRING == this;
	}

	/**
	 * Returns true if this scalar type is {@link #DATE}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isDate() {
		return DATE == this;
	}

	/**
	 * Returns true if this scalar type is {@link #BOOLEAN}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isBoolean() {
		return BOOLEAN == this;
	}

	/**
	 * Returns true if this scalar type on of the following:
	 *
	 * <ul>
	 * <li>{@link #BYTE}</li>
	 * <li>{@link #SHORT}</li>
	 * <li>{@link #INTEGER}</li>
	 * <li>{@link #LONG}</li>
	 * <li>{@link #FLOAT}</li>
	 * <li>{@link #DOUBLE}</li>
	 * </ul>
	 *
	 * @return Returns {@code true} in the cases listed above.
	 */
	public boolean isNumericalType() {
		return isIntegerType() || isFloatType();
	}

	/**
	 * Returns true if this scalar type on of the following:
	 *
	 * <ul>
	 * <li>{@link #BYTE}</li>
	 * <li>{@link #SHORT}</li>
	 * <li>{@link #INTEGER}</li>
	 * <li>{@link #LONG}</li>
	 * </ul>
	 *
	 * @return Returns {@code true} in the cases listed above.
	 */
	public boolean isIntegerType() {
		return isByte() || isShort() || isInteger() || isLong();
	}

	/**
	 * Returns true if this scalar type is {@link #BYTE}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isByte() {
		return BYTE == this;
	}

	/**
	 * Returns true if this scalar type is {@link #SHORT}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isShort() {
		return SHORT == this;
	}

	/**
	 * Returns true if this scalar type is {@link #INTEGER}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isInteger() {
		return INTEGER == this;
	}

	/**
	 * Returns true if this scalar type is {@link #LONG}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isLong() {
		return LONG == this;
	}

	/**
	 * Returns true if this scalar type on of the following:
	 *
	 * <ul>
	 * <li>{@link #FLOAT}</li>
	 * <li>{@link #DOUBLE}</li>
	 * </ul>
	 *
	 * @return Returns {@code true} in the cases listed above.
	 */
	public boolean isFloatType() {
		return isFloat() || isDouble();
	}

	/**
	 * Returns true if this scalar type is {@link #FLOAT}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isFloat() {
		return FLOAT == this;
	}

	/**
	 * Returns true if this scalar type is {@link #DOUBLE}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isDouble() {
		return DOUBLE == this;
	}

	/**
	 * Returns true if this scalar type is {@link #BYTE_STREAM}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isByteStream() {
		return BYTE_STREAM == this;
	}

	/**
	 * Returns true if this scalar type on of the following:
	 *
	 * <ul>
	 * <li>{@link #FLOAT_COMPLEX}</li>
	 * <li>{@link #DOUBLE_COMPLEX}</li>
	 * </ul>
	 *
	 * @return Returns {@code true} in the cases listed above.
	 */
	public boolean isComplexType() {
		return isFloatComplex() || isDoubleComplex();
	}

	/**
	 * Returns true if this scalar type is {@link #FLOAT_COMPLEX}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isFloatComplex() {
		return FLOAT_COMPLEX == this;
	}

	/**
	 * Returns true if this scalar type is {@link #DOUBLE_COMPLEX}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isDoubleComplex() {
		return DOUBLE_COMPLEX == this;
	}

	/**
	 * Returns true if this scalar type is {@link #ENUMERATION}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isEnumeration() {
		return ENUMERATION == this;
	}

	/**
	 * Returns true if this scalar type is {@link #FILE_LINK}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isFileLink() {
		return FILE_LINK == this;
	}

	/**
	 * Returns true if this scalar type is {@link #BLOB}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isBlob() {
		return BLOB == this;
	}

	/**
	 * Returns true if this scalar type is {@link #UNKNOWN}.
	 *
	 * @return Returns {@code true} if this constant is the constant described
	 *         above.
	 */
	public boolean isUnknown() {
		return UNKNOWN == this;
	}

	/**
	 * Returns the associated {@link ValueType}.
	 *
	 * @return The associated {@code ValueType} is returned.
	 */
	public ValueType toValueType() {
		return valueType;
	}

	/**
	 * Returns the non sequence {@link ValueType} of the associated {@code
	 * ValueType}.
	 *
	 * @return The non sequence {@code ValueType} is returned.
	 */
	public ValueType toSingleValueType() {
		return valueType.toSingleType();
	}
}

Back to the top