Skip to content

Commit e8d5a3f

Browse files
committed
#41 allow the creation of VEnum with index out of display range
1 parent a7488be commit e8d5a3f

2 files changed

Lines changed: 154 additions & 154 deletions

File tree

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,59 @@
1-
/**
2-
* Copyright information and license terms for this software can be
3-
* found in the file LICENSE.TXT included with the distribution.
4-
*/
5-
package org.epics.vtype;
6-
7-
/**
8-
* Immutable {@code VEnum} implementation.
9-
*
10-
* @author carcassi
11-
*/
12-
final class IVEnum extends VEnum {
13-
14-
private final Alarm alarm;
15-
private final Time time;
16-
private final int index;
17-
private final EnumDisplay enumDisplay;
18-
19-
IVEnum(int index, EnumDisplay enumDisplay, Alarm alarm, Time time) {
20-
VType.argumentNotNull("enumDisplay", enumDisplay);
21-
VType.argumentNotNull("alarm", alarm);
22-
VType.argumentNotNull("time", time);
23-
if (index < 0 || index >= enumDisplay.getChoices().size()) {
24-
throw new IndexOutOfBoundsException("VEnum index must be within the label range");
25-
}
26-
this.index = index;
27-
this.enumDisplay = enumDisplay;
28-
this.alarm = alarm;
29-
this.time = time;
30-
}
31-
32-
@Override
33-
public String getValue() {
34-
return enumDisplay.getChoices().get(index);
35-
}
36-
37-
@Override
38-
public int getIndex() {
39-
return index;
40-
}
41-
42-
@Override
43-
public EnumDisplay getDisplay() {
44-
return enumDisplay;
45-
}
46-
47-
@Override
48-
public Alarm getAlarm() {
49-
return alarm;
50-
}
51-
52-
@Override
53-
public Time getTime() {
54-
return time;
55-
}
56-
57-
}
1+
/**
2+
* Copyright information and license terms for this software can be
3+
* found in the file LICENSE.TXT included with the distribution.
4+
*/
5+
package org.epics.vtype;
6+
7+
/**
8+
* Immutable {@code VEnum} implementation.
9+
*
10+
* @author carcassi
11+
*/
12+
final class IVEnum extends VEnum {
13+
14+
private final Alarm alarm;
15+
private final Time time;
16+
private final int index;
17+
private final EnumDisplay enumDisplay;
18+
19+
IVEnum(int index, EnumDisplay enumDisplay, Alarm alarm, Time time) {
20+
VType.argumentNotNull("enumDisplay", enumDisplay);
21+
VType.argumentNotNull("alarm", alarm);
22+
VType.argumentNotNull("time", time);
23+
VType.argumentNotNull("index", index);
24+
this.index = index;
25+
this.enumDisplay = enumDisplay;
26+
this.alarm = alarm;
27+
this.time = time;
28+
}
29+
30+
@Override
31+
public String getValue() {
32+
try {
33+
return enumDisplay.getChoices().get(index);
34+
} catch (IndexOutOfBoundsException ex) {
35+
return "Invalid index : " + index + " must be within the label range " + enumDisplay.getChoices().toString();
36+
}
37+
}
38+
39+
@Override
40+
public int getIndex() {
41+
return index;
42+
}
43+
44+
@Override
45+
public EnumDisplay getDisplay() {
46+
return enumDisplay;
47+
}
48+
49+
@Override
50+
public Alarm getAlarm() {
51+
return alarm;
52+
}
53+
54+
@Override
55+
public Time getTime() {
56+
return time;
57+
}
58+
59+
}
Lines changed: 95 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,95 @@
1-
/**
2-
* Copyright information and license terms for this software can be
3-
* found in the file LICENSE.TXT included with the distribution.
4-
*/
5-
package org.epics.vtype;
6-
7-
import java.util.List;
8-
import java.util.Objects;
9-
import static org.epics.vtype.VType.typeOf;
10-
11-
/**
12-
* Scalar enum with alarm and timestamp.
13-
* Given that enumerated values are of very limited use without
14-
* the labels, and that the current label is the data most likely used, the
15-
* enum is scalar of type {@link String}. The index is provided as an extra field, and
16-
* the list of all possible values is always provided.
17-
*
18-
* @author carcassi
19-
*/
20-
public abstract class VEnum extends Scalar {
21-
22-
/**
23-
* {@inheritDoc }
24-
*/
25-
@Override
26-
public abstract String getValue();
27-
28-
/**
29-
* Return the index of the value in the list of labels.
30-
*
31-
* @return the current index
32-
*/
33-
public abstract int getIndex();
34-
35-
/**
36-
* Returns the display information, including all possible choice names.
37-
*
38-
* @return the enum display
39-
*/
40-
public abstract EnumDisplay getDisplay();
41-
42-
/**
43-
* Create a new VEnum.
44-
*
45-
* @param index the index in the label array
46-
* @param metaData the metadata
47-
* @param alarm the alarm
48-
* @param time the time
49-
* @return the new value
50-
*/
51-
public static VEnum of(int index, EnumDisplay metaData, Alarm alarm, Time time) {
52-
return new IVEnum(index, metaData, alarm, time);
53-
}
54-
55-
@Override
56-
public final boolean equals(Object obj) {
57-
if (this == obj) {
58-
return true;
59-
}
60-
61-
if (obj instanceof VEnum) {
62-
VEnum other = (VEnum) obj;
63-
64-
return getIndex() == other.getIndex() &&
65-
getDisplay().equals(other.getDisplay()) &&
66-
getAlarm().equals(other.getAlarm()) &&
67-
getTime().equals(other.getTime());
68-
}
69-
70-
return false;
71-
}
72-
73-
@Override
74-
public final int hashCode() {
75-
int hash = 7;
76-
hash = 23 * hash + Objects.hashCode(getValue());
77-
hash = 23 * hash + Objects.hashCode(getAlarm());
78-
hash = 23 * hash + Objects.hashCode(getTime());
79-
return hash;
80-
}
81-
82-
@Override
83-
public final String toString() {
84-
StringBuilder builder = new StringBuilder();
85-
Class type = typeOf(this);
86-
builder.append(type.getSimpleName())
87-
.append("[\"")
88-
.append(getValue())
89-
.append("\", ")
90-
.append(getAlarm())
91-
.append(", ")
92-
.append(getTime())
93-
.append(']');
94-
return builder.toString();
95-
}
96-
97-
}
1+
/**
2+
* Copyright information and license terms for this software can be
3+
* found in the file LICENSE.TXT included with the distribution.
4+
*/
5+
package org.epics.vtype;
6+
7+
import java.util.Objects;
8+
9+
/**
10+
* Scalar enum with alarm and timestamp.
11+
* Given that enumerated values are of very limited use without
12+
* the labels, and that the current label is the data most likely used, the
13+
* enum is scalar of type {@link String}. The index is provided as an extra field, and
14+
* the list of all possible values is always provided.
15+
*
16+
* @author carcassi
17+
*/
18+
public abstract class VEnum extends Scalar {
19+
20+
/**
21+
* {@inheritDoc }
22+
*/
23+
@Override
24+
public abstract String getValue();
25+
26+
/**
27+
* Return the index of the value in the list of labels.
28+
*
29+
* @return the current index
30+
*/
31+
public abstract int getIndex();
32+
33+
/**
34+
* Returns the display information, including all possible choice names.
35+
*
36+
* @return the enum display
37+
*/
38+
public abstract EnumDisplay getDisplay();
39+
40+
/**
41+
* Create a new VEnum.
42+
*
43+
* @param index the index in the label array
44+
* @param metaData the metadata
45+
* @param alarm the alarm
46+
* @param time the time
47+
* @return the new value
48+
*/
49+
public static VEnum of(int index, EnumDisplay metaData, Alarm alarm, Time time) {
50+
return new IVEnum(index, metaData, alarm, time);
51+
}
52+
53+
@Override
54+
public final boolean equals(Object obj) {
55+
if (this == obj) {
56+
return true;
57+
}
58+
59+
if (obj instanceof VEnum) {
60+
VEnum other = (VEnum) obj;
61+
62+
return getIndex() == other.getIndex() &&
63+
getDisplay().equals(other.getDisplay()) &&
64+
getAlarm().equals(other.getAlarm()) &&
65+
getTime().equals(other.getTime());
66+
}
67+
68+
return false;
69+
}
70+
71+
@Override
72+
public final int hashCode() {
73+
int hash = 7;
74+
hash = 23 * hash + Objects.hashCode(getValue());
75+
hash = 23 * hash + Objects.hashCode(getAlarm());
76+
hash = 23 * hash + Objects.hashCode(getTime());
77+
return hash;
78+
}
79+
80+
@Override
81+
public final String toString() {
82+
StringBuilder builder = new StringBuilder();
83+
Class type = typeOf(this);
84+
builder.append(type.getSimpleName())
85+
.append("[\"")
86+
.append(getValue())
87+
.append("\", ")
88+
.append(getAlarm())
89+
.append(", ")
90+
.append(getTime())
91+
.append(']');
92+
return builder.toString();
93+
}
94+
95+
}

0 commit comments

Comments
 (0)