Skip to content

Commit e7a99c4

Browse files
committed
vtype: adding equals/hashCode/toString for VString
1 parent b433f2e commit e7a99c4

6 files changed

Lines changed: 201 additions & 75 deletions

File tree

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
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-
/**
8-
* Immutable VString implementation.
9-
*
10-
* @author carcassi
11-
*/
12-
class IVString extends VString {
13-
14-
private final String value;
15-
private final Alarm alarm;
16-
private final Time time;
17-
18-
IVString(String value, Alarm alarm, Time time) {
19-
this.value = value;
20-
this.alarm = alarm;
21-
this.time = time;
22-
}
23-
24-
@Override
25-
public String getValue() {
26-
return value;
27-
}
28-
29-
@Override
30-
public Alarm getAlarm() {
31-
return alarm;
32-
}
33-
34-
@Override
35-
public Time getTime() {
36-
return time;
37-
}
38-
39-
}
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+
/**
8+
* Immutable VString implementation.
9+
*
10+
* @author carcassi
11+
*/
12+
class IVString extends VString {
13+
14+
private final String value;
15+
private final Alarm alarm;
16+
private final Time time;
17+
18+
IVString(String value, Alarm alarm, Time time) {
19+
VType.argumentNotNull("value", value);
20+
VType.argumentNotNull("alarm", alarm);
21+
VType.argumentNotNull("time", time);
22+
this.value = value;
23+
this.alarm = alarm;
24+
this.time = time;
25+
}
26+
27+
@Override
28+
public String getValue() {
29+
return value;
30+
}
31+
32+
@Override
33+
public Alarm getAlarm() {
34+
return alarm;
35+
}
36+
37+
@Override
38+
public Time getTime() {
39+
return time;
40+
}
41+
42+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public abstract class VNumber extends Scalar implements DisplayProvider {
3434
* @return the string representation
3535
*/
3636
@Override
37-
public String toString() {
37+
public final String toString() {
3838
StringBuilder builder = new StringBuilder();
3939
Class type = typeOf(this);
4040
builder.append(type.getSimpleName())
Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,77 @@
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-
/**
8-
* Scalar string with alarm and timestamp.
9-
*
10-
* @author carcassi
11-
*/
12-
public abstract class VString extends Scalar {
13-
14-
/**
15-
* {@inheritDoc }
16-
*/
17-
@Override
18-
public abstract String getValue();
19-
20-
21-
/**
22-
* Creates a new VString.
23-
*
24-
* @param value the string value
25-
* @param alarm the alarm
26-
* @param time the time
27-
* @return the new value
28-
*/
29-
public static VString of(final String value, final Alarm alarm, final Time time) {
30-
return new IVString(value, alarm, time);
31-
}
32-
33-
}
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.Objects;
8+
9+
/**
10+
* Scalar string with alarm and timestamp.
11+
*
12+
* @author carcassi
13+
*/
14+
public abstract class VString extends Scalar {
15+
16+
/**
17+
* {@inheritDoc }
18+
*/
19+
@Override
20+
public abstract String getValue();
21+
22+
23+
/**
24+
* Creates a new VString.
25+
*
26+
* @param value the string value
27+
* @param alarm the alarm
28+
* @param time the time
29+
* @return the new value
30+
*/
31+
public static VString of(final String value, final Alarm alarm, final Time time) {
32+
return new IVString(value, alarm, time);
33+
}
34+
35+
@Override
36+
public final boolean equals(Object obj) {
37+
if (this == obj) {
38+
return true;
39+
}
40+
41+
if (obj instanceof VString) {
42+
VString other = (VString) obj;
43+
44+
return getClass().equals(other.getClass()) &&
45+
getValue().equals(other.getValue()) &&
46+
getAlarm().equals(other.getAlarm()) &&
47+
getTime().equals(other.getTime());
48+
}
49+
50+
return false;
51+
}
52+
53+
@Override
54+
public final int hashCode() {
55+
int hash = 7;
56+
hash = 23 * hash + Objects.hashCode(getValue());
57+
hash = 23 * hash + Objects.hashCode(getAlarm());
58+
hash = 23 * hash + Objects.hashCode(getTime());
59+
return hash;
60+
}
61+
62+
@Override
63+
public final String toString() {
64+
StringBuilder builder = new StringBuilder();
65+
Class type = typeOf(this);
66+
builder.append(type.getSimpleName())
67+
.append("[\"")
68+
.append(getValue())
69+
.append("\", ")
70+
.append(getAlarm())
71+
.append(", ")
72+
.append(getTime())
73+
.append(']');
74+
return builder.toString();
75+
}
76+
77+
}

epics-vtype/vtype/src/test/java/org/epics/vtype/FeatureTestVNumber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public abstract class FeatureTestVNumber<N extends Number, V extends VNumber> {
2828
abstract String getToString();
2929

3030
@Test
31-
public void vNumberArrayof1() {
31+
public void vNumberOf1() {
3232
Alarm alarm = Alarm.of(AlarmSeverity.MINOR, AlarmStatus.DB, "LOW");
3333
Time time = Time.of(Instant.ofEpochSecond(1354719441, 521786982));
3434
VNumber value = VNumber.of(getValue(), alarm, time, Display.none());

epics-vtype/vtype/src/test/java/org/epics/vtype/FeatureTestVNumberArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public abstract class FeatureTestVNumberArray<L extends ListNumber, V extends VN
3333
abstract String getToString();
3434

3535
@Test
36-
public void vNumberArrayof1() {
36+
public void vNumberArrayOf1() {
3737
Alarm alarm = Alarm.of(AlarmSeverity.MINOR, AlarmStatus.DB, "LOW");
3838
Time time = Time.of(Instant.ofEpochSecond(1354719441, 521786982));
3939
ListInteger sizes = ArrayInteger.of(5,2);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.text.DecimalFormat;
8+
import java.time.Instant;
9+
import org.epics.util.stats.Range;
10+
import org.junit.Test;
11+
import static org.junit.Assert.*;
12+
import static org.hamcrest.Matchers.*;
13+
14+
/**
15+
*
16+
* @author carcassi
17+
*/
18+
public class VStringTest {
19+
20+
public String getValue() {
21+
return "A string";
22+
}
23+
24+
public String getAnotherValue() {
25+
return "Another string";
26+
}
27+
28+
@Test
29+
public void of1() {
30+
Alarm alarm = Alarm.of(AlarmSeverity.MINOR, AlarmStatus.DB, "LOW");
31+
Time time = Time.of(Instant.ofEpochSecond(1354719441, 521786982));
32+
VString value = VString.of(getValue(), alarm, time);
33+
assertThat(value.getValue(), equalTo(getValue()));
34+
assertThat(value.getAlarm(), equalTo(alarm));
35+
assertThat(value.getTime(), equalTo(time));
36+
assertThat(value.toString(), equalTo("VString[\"A string\", MINOR(DB) - LOW, 2012-12-05T14:57:21.521786982Z]"));
37+
}
38+
39+
@Test(expected = NullPointerException.class)
40+
public void of2() {
41+
VString.of(null, Alarm.none(), Time.now());
42+
}
43+
44+
@Test(expected = NullPointerException.class)
45+
public void of3() {
46+
VString.of(getValue(), null, Time.now());
47+
}
48+
49+
@Test(expected = NullPointerException.class)
50+
public void of4() {
51+
VString.of(getValue(), Alarm.none(), null);
52+
}
53+
54+
@Test
55+
public void equals1() {
56+
Alarm alarm = Alarm.of(AlarmSeverity.MINOR, AlarmStatus.DB, "LOW");
57+
Time time = Time.of(Instant.ofEpochSecond(1354719441, 521786982));
58+
Time now = Time.now();
59+
assertThat(VString.of(getValue(), alarm, time), equalTo(VString.of(getValue(), alarm, time)));
60+
assertThat(VString.of(getAnotherValue(), Alarm.none(), now), equalTo(VString.of(getAnotherValue(), Alarm.none(), now)));
61+
assertThat(VString.of(getValue(), alarm, time), not(equalTo(null)));
62+
assertThat(VString.of(getValue(), alarm, time), not(equalTo(VString.of(getAnotherValue(), alarm, time))));
63+
assertThat(VString.of(getValue(), alarm, time), not(equalTo(VString.of(getValue(), Alarm.none(), time))));
64+
assertThat(VString.of(getValue(), alarm, time), not(equalTo(VString.of(getValue(), alarm, now))));
65+
}
66+
67+
@Test
68+
public void hashCode1() {
69+
Alarm alarm = Alarm.of(AlarmSeverity.MINOR, AlarmStatus.DB, "LOW");
70+
Time time = Time.of(Instant.ofEpochSecond(1354719441, 521786982));
71+
Time now = Time.now();
72+
assertThat(VString.of(getValue(), alarm, time).hashCode(), equalTo(VString.of(getValue(), alarm, time).hashCode()));
73+
assertThat(VString.of(getAnotherValue(), Alarm.none(), now).hashCode(), equalTo(VString.of(getAnotherValue(), Alarm.none(), now).hashCode()));
74+
assertThat(VString.of(getValue(), alarm, time).hashCode(), not(equalTo(VString.of(getAnotherValue(), alarm, time).hashCode())));
75+
assertThat(VString.of(getValue(), alarm, time).hashCode(), not(equalTo(VString.of(getValue(), Alarm.none(), time).hashCode())));
76+
assertThat(VString.of(getValue(), alarm, time).hashCode(), not(equalTo(VString.of(getValue(), alarm, now).hashCode())));
77+
}
78+
79+
}

0 commit comments

Comments
 (0)