Skip to content

Commit

Permalink
add EventID.toShortString() (#215)
Browse files Browse the repository at this point in the history
This prints just the dotted hex notation.

* add toShortString

* better implementation
  • Loading branch information
bobjacobsen authored Feb 27, 2023
1 parent 52dbd04 commit 60a20e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
35 changes: 17 additions & 18 deletions src/org/openlcb/EventID.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.openlcb;

// For annotations
import net.jcip.annotations.*;
import edu.umd.cs.findbugs.annotations.*;
import net.jcip.annotations.*;
import edu.umd.cs.findbugs.annotations.*;

/**
* Common EventID implementation.
Expand All @@ -17,16 +17,16 @@
public class EventID {

static final int BYTECOUNT = 8;

@CheckReturnValue
public EventID(@NonNull NodeID node, int b7, int b8) {
this.contents = new byte[BYTECOUNT];
System.arraycopy(node.contents, 0, this.contents, 0, BYTECOUNT-2);

this.contents[6] = (byte)b7;
this.contents[7] = (byte)b8;
}

@CheckReturnValue
public EventID(@NonNull byte[] contents) {
if (contents == null)
Expand All @@ -36,7 +36,7 @@ public EventID(@NonNull byte[] contents) {
this.contents = new byte[BYTECOUNT];
System.arraycopy(contents, 0, this.contents, 0, BYTECOUNT);
}

@CheckReturnValue
public EventID(@NonNull String value) {
if (value == null)
Expand All @@ -47,9 +47,9 @@ public EventID(@NonNull String value) {
this.contents = new byte[BYTECOUNT];
System.arraycopy(data, 0, this.contents, 0, BYTECOUNT);
}

byte[] contents;

@CheckReturnValue
@NonNull
public byte[] getContents() {
Expand All @@ -71,7 +71,7 @@ public boolean equals(Object o){
} catch (Exception e) {
return false;
}
}
}

/// Checks whether a given Event ID comes from a given Node ID's space.
public boolean startsWith(NodeID id) {
Expand All @@ -92,21 +92,20 @@ public int hashCode() {
+(contents[5]<<6)
+(contents[6]<<3)
+(contents[7]);
}
}

@CheckReturnValue
@NonNull
@Override
public String toString() {
return "EventID:"
+Utilities.toHexPair(contents[0])+"."
+Utilities.toHexPair(contents[1])+"."
+Utilities.toHexPair(contents[2])+"."
+Utilities.toHexPair(contents[3])+"."
+Utilities.toHexPair(contents[4])+"."
+Utilities.toHexPair(contents[5])+"."
+Utilities.toHexPair(contents[6])+"."
+Utilities.toHexPair(contents[7]);
+Utilities.toHexDotsString(contents);
}

@CheckReturnValue
@NonNull
public String toShortString() {
return Utilities.toHexDotsString(contents);
}

public long toLong() {
Expand Down
24 changes: 15 additions & 9 deletions test/org/openlcb/EventIDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ public void testTooShortArg() {
}
Assert.fail("Should have thrown exception");
}

@Test
public void testOKLengthArg() {
EventID e = new EventID(new byte[]{1,2,3,4,5,6,7,8});
Assert.assertNotNull(e);
}

@Test
public void testEqualsSame() {
EventID e1 = new EventID(new byte[]{1,2,3,4,5,6,7,8});
EventID e2 = new EventID(new byte[]{1,2,3,4,5,6,7,8});
Assert.assertTrue(e1.equals(e2));
Assert.assertEquals("hashcodes equal when equal",e1.hashCode(),e2.hashCode());
}

@Test
public void testStringArgDotted() {
EventID e1 = new EventID("1.2.3.4.5.6.7.8");
Expand All @@ -72,43 +72,43 @@ public void testStringArgSpaces() {
@Test
public void testAltCtor() {
EventID e1 = new EventID(new byte[]{1,2,3,4,5,6,7,8});

NodeID n = new NodeID(new byte[]{1,2,3,4,5,6});
EventID e2 = new EventID(n, 7, 8);

Assert.assertTrue(e1.equals(e2));
}

@Test
public void testEqualsCastSame() {
Object e1 = new EventID(new byte[]{1,2,3,4,5,6,7,8});
EventID e2 = new EventID(new byte[]{1,2,3,4,5,6,7,8});
Assert.assertTrue(e1.equals(e2));
Assert.assertEquals("hashcodes equal when equal",e1.hashCode(),e2.hashCode());
}

@Test
public void testEqualsSelf() {
EventID e1 = new EventID(new byte[]{1,2,3,4,5,6,7,8});
Assert.assertTrue(e1.equals(e1));
Assert.assertEquals("hashcodes equal when equal",e1.hashCode(),e1.hashCode());
}

@Test
public void testEqualsCastSelf() {
EventID e1 = new EventID(new byte[]{1,2,3,4,5,6,7,8});
Object e2 = e1;
Assert.assertTrue(e1.equals(e2));
Assert.assertEquals("hashcodes equal when equal",e1.hashCode(),e1.hashCode());
}

@Test
public void testNotEquals() {
EventID e1 = new EventID(new byte[]{1,2,3,4,5,6,7,8});
EventID e2 = new EventID(new byte[]{1,3,3,4,5,6,7,8});
Assert.assertTrue(!e1.equals(e2));
}

@Test
public void testNotEqualsOtherType() {
EventID e1 = new EventID(new byte[]{1,2,3,4,5,6,7,8});
Expand All @@ -131,6 +131,12 @@ public void testOutputFormat() {
Assert.assertEquals(e1.toString(), "EventID:00.00.01.10.13.0D.D0.AB");
}

@Test
public void testShortOutputFormat() {
EventID e1 = new EventID(new byte[]{0,0,1,0x10,0x13,0x0D,(byte)0xD0,(byte)0xAB});
Assert.assertEquals(e1.toShortString(), "00.00.01.10.13.0D.D0.AB");
}

@Test
public void testToLong() {
EventID e1 = new EventID(new byte[]{0,0,0,0,0,0,0,0});
Expand Down

0 comments on commit 60a20e4

Please sign in to comment.