Skip to main content
summaryrefslogtreecommitdiffstats
blob: be7cee5bf7e0d5a8c5a8d1f5a8d8b25a67f52245 (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
/********************************************************************************
 * 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.adapter;

import org.eclipse.mdm.api.base.model.Enumeration;
import org.eclipse.mdm.api.base.model.Value;
import org.eclipse.mdm.api.base.model.ValueType;

import javax.xml.ws.Service;
import java.lang.reflect.Field;

/**
 * Represents a modeled attribute.
 *
 * @since 1.0.0
 * @author Viktor Stoehr, Gigatronik Ingolstadt GmbH
 * @author Sebastian Dirsch, Gigatronik Ingolstadt GmbH
 * @see EntityType
 * @see ValueType
 * @see Value
 */
public interface Attribute {

	// ======================================================================
	// Public methods
	// ======================================================================

	/**
	 * Returns the {@link EntityType} this attribute belongs to.
	 *
	 * @return The owning {@code EntityType} is returned.
	 */
	EntityType getEntityType();

	/**
	 * Returns the name of this attribute.
	 *
	 * @return The name is returned.
	 */
	String getName();

	/**
	 * Returns the unit name of this attribute.
	 *
	 * @return The unit name is returned.
	 */
	String getUnit();

	/**
	 * Returns the {@link ValueType} of this attribute.
	 *
	 * @return The {@code ValueType} is returned.
	 */
	ValueType getValueType();

	/**
	 * Returns the enumeration {@code Class} associated with this
	 * {@code Attribute}.
	 *
	 * @return The enumeration {@code Class} associated with this
	 *         {@code Attribute} is returned.
	 * @throws IllegalStateException
	 *             Thrown if the {@link ValueType} of this {@code Attribute} is
	 *             neither {@link ValueType#ENUMERATION} nor
	 *             {@link ValueType#ENUMERATION_SEQUENCE}.
	 */
	Enumeration<?> getEnumObj();

	/**
	 * Creates a new and empty {@link Value}.
	 *
	 * @return Created {@code Value} is returned.
	 */
	default Value createValue() {
		ValueType valueType = getValueType();
		if (valueType.isEnumerationType()) {
			return valueType.create(getEnumObj(), getName());
		} else {
			return valueType.create(getName());
		}
	}

	/**
	 * Creates a new {@link Value} with given initial value.
	 *
	 * @param input
	 *            The initial value.
	 * @return Created {@code Value} is returned.
	 */
	default Value createValue(Object input) {
		return createValue("", input);
	}

	/**
	 * Creates a new {@link Value} with given unit name and initial value.
	 *
	 * @param unit
	 *            The name of unit.
	 * @param input
	 *            The initial value.
	 * @return Created {@code Value} is returned.
	 */
	default Value createValue(String unit, Object input) {
		return createValue(unit, true, input);
	}

	/**
	 * Creates a new sequence {@link Value} with given unit name and initial
	 * value.
	 *
	 * @param unit
	 *            The name of unit.
	 * @param input
	 *            The initial value.
	 * @return Created {@code Value} is returned.
	 */
	default Value createValueSeq(String unit, Object input) {
		if(getValueType().isEnumerationType()) {
			return createEnumerationSequence(unit, input);
		}
		return createValueSequence(unit, input);
	}

	default Value createValueSequence(String unit, Object input) {
		ValueType valueType = getValueType();
		try {
			Field field = valueType.getClass().getField(valueType.name() + "_SEQUENCE");
			ValueType<?> sequenceValueType = (ValueType<?>) field.get(valueType);
			return sequenceValueType.create(getName(), unit, true, input);

		} catch (NoSuchFieldException | ClassCastException | IllegalAccessException e) {
			throw new RuntimeException("Can't figure out sequence type for " + valueType.name());
		}
	}

	default Value createEnumerationSequence(String unit, Object input) {
		ValueType valueType = getValueType().toSequenceType();
		return valueType.create(getName(), unit, true, input, getEnumObj().getName());
	}

	/**
	 * Creates a new {@link Value} with given unit name, initial valid flag and
	 * value.
	 *
	 * @param unit
	 *            The name of unit.
	 * @param valid
	 *            The initial valid flag.
	 * @param input
	 *            The initial value.
	 * @return Created {@code Value} is returned.
	 */
	default Value createValue(String unit, boolean valid, Object input) {
		ValueType<?> valueType = getValueType();
		if (valueType.isEnumerationType()) {
			return valueType.create(getName(), unit, valid, input, getEnumObj().getName());
		} else {
			return valueType.create(getName(), unit, valid, input);
		}
	}

	/**
	 * Returns the name of this attribute.
	 *
	 * @return The name of this attribute is returned.
	 */
	@Override
	String toString();

}

Back to the top