Skip to content

Commit 8ea55fe

Browse files
committed
vtype: adding equals/hashCode/toString for VEnum
1 parent e7a99c4 commit 8ea55fe

3 files changed

Lines changed: 174 additions & 55 deletions

File tree

Lines changed: 97 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,97 @@
1-
/**
2-
* Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
3-
* All rights reserved. Use is subject to license terms. See LICENSE.TXT
4-
*/
5-
package org.epics.vtype;
6-
7-
import java.util.List;
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-
}
1+
/**
2+
* Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
3+
* All rights reserved. Use is subject to license terms. See LICENSE.TXT
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+
}

epics-vtype/vtype/src/main/java/org/epics/vtype/VString.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public final boolean equals(Object obj) {
4141
if (obj instanceof VString) {
4242
VString other = (VString) obj;
4343

44-
return getClass().equals(other.getClass()) &&
45-
getValue().equals(other.getValue()) &&
44+
return getValue().equals(other.getValue()) &&
4645
getAlarm().equals(other.getAlarm()) &&
4746
getTime().equals(other.getTime());
4847
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
3+
* All rights reserved. Use is subject to license terms. See LICENSE.TXT
4+
*/
5+
package org.epics.vtype;
6+
7+
import java.time.Instant;
8+
import org.junit.Test;
9+
import static org.junit.Assert.*;
10+
import static org.hamcrest.Matchers.*;
11+
12+
/**
13+
*
14+
* @author carcassi
15+
*/
16+
public class VEnumTest {
17+
18+
@Test
19+
public void of1() {
20+
EnumDisplay display = EnumDisplay.of("A", "B", "C");
21+
Alarm alarm = Alarm.of(AlarmSeverity.MINOR, AlarmStatus.DB, "LOW");
22+
Time time = Time.of(Instant.ofEpochSecond(1354719441, 521786982));
23+
VEnum value = VEnum.of(0, display, alarm, time);
24+
assertThat(value.getValue(), equalTo("A"));
25+
assertThat(value.getIndex(), equalTo(0));
26+
assertThat(value.getAlarm(), equalTo(alarm));
27+
assertThat(value.getTime(), equalTo(time));
28+
assertThat(value.getDisplay(), equalTo(display));
29+
assertThat(value.toString(), equalTo("VEnum[\"A\", MINOR(DB) - LOW, 2012-12-05T14:57:21.521786982Z]"));
30+
}
31+
32+
@Test(expected = NullPointerException.class)
33+
public void of2() {
34+
VEnum value = VEnum.of(0, null, Alarm.none(), Time.now());
35+
}
36+
37+
@Test(expected = NullPointerException.class)
38+
public void of3() {
39+
VEnum value = VEnum.of(0, EnumDisplay.of(3), null, Time.now());
40+
}
41+
42+
@Test(expected = NullPointerException.class)
43+
public void of4() {
44+
VEnum value = VEnum.of(0, EnumDisplay.of(3), Alarm.none(), null);
45+
}
46+
47+
@Test
48+
public void equals1() {
49+
EnumDisplay display = EnumDisplay.of("A", "B", "C");
50+
Alarm alarm = Alarm.of(AlarmSeverity.MINOR, AlarmStatus.DB, "LOW");
51+
Time time = Time.of(Instant.ofEpochSecond(1354719441, 521786982));
52+
Time now = Time.now();
53+
assertThat(VEnum.of(0, display, alarm, time), equalTo(VEnum.of(0, display, alarm, time)));
54+
assertThat(VEnum.of(1, display, Alarm.none(), now), equalTo(VEnum.of(1, display, Alarm.none(), now)));
55+
assertThat(VEnum.of(0, display, alarm, time), not(equalTo(null)));
56+
assertThat(VEnum.of(0, display, alarm, time), not(equalTo(VEnum.of(1, display, alarm, time))));
57+
assertThat(VEnum.of(0, display, alarm, time), not(equalTo(VEnum.of(0, EnumDisplay.of(3), alarm, time))));
58+
assertThat(VEnum.of(0, display, alarm, time), not(equalTo(VEnum.of(0, display, Alarm.none(), time))));
59+
assertThat(VEnum.of(0, display, alarm, time), not(equalTo(VEnum.of(0, display, alarm, now))));
60+
}
61+
62+
@Test
63+
public void hashCode1() {
64+
EnumDisplay display = EnumDisplay.of("A", "B", "C");
65+
Alarm alarm = Alarm.of(AlarmSeverity.MINOR, AlarmStatus.DB, "LOW");
66+
Time time = Time.of(Instant.ofEpochSecond(1354719441, 521786982));
67+
Time now = Time.now();
68+
assertThat(VEnum.of(0, display, alarm, time).hashCode(), equalTo(VEnum.of(0, display, alarm, time).hashCode()));
69+
assertThat(VEnum.of(1, display, Alarm.none(), now).hashCode(), equalTo(VEnum.of(1, display, Alarm.none(), now).hashCode()));
70+
assertThat(VEnum.of(0, display, alarm, time).hashCode(), not(equalTo(VEnum.of(1, display, alarm, time).hashCode())));
71+
assertThat(VEnum.of(0, display, alarm, time).hashCode(), not(equalTo(VEnum.of(0, EnumDisplay.of(3), alarm, time).hashCode())));
72+
assertThat(VEnum.of(0, display, alarm, time).hashCode(), not(equalTo(VEnum.of(0, display, Alarm.none(), time).hashCode())));
73+
assertThat(VEnum.of(0, display, alarm, time).hashCode(), not(equalTo(VEnum.of(0, display, alarm, now).hashCode())));
74+
}
75+
76+
}

0 commit comments

Comments
 (0)