Add SeqNum#valueOf(String)
Change-Id: I073e3de37f25067296ef8db83ee59c2439c38865
diff --git a/src/main/java/net/onrc/onos/core/util/distributed/sharedlog/SeqNum.java b/src/main/java/net/onrc/onos/core/util/distributed/sharedlog/SeqNum.java
index 480138b..083464f 100644
--- a/src/main/java/net/onrc/onos/core/util/distributed/sharedlog/SeqNum.java
+++ b/src/main/java/net/onrc/onos/core/util/distributed/sharedlog/SeqNum.java
@@ -65,6 +65,26 @@
}
/**
+ * Gets an instance for specified string.
+ *
+ * @param str parsed result must not be {@link #ZERO}
+ * @return SeqNum
+ */
+ public static SeqNum valueOf(final String str) {
+ return valueOf(UnsignedLongs.decode(str));
+ }
+
+ /**
+ * Gets an instance for specified string.
+ *
+ * @param str string representation of unsigned long
+ * @return SeqNum
+ */
+ public static SeqNum anyValueOf(final String str) {
+ return anyValueOf(UnsignedLongs.decode(str));
+ }
+
+ /**
* Gets the next sequence number.
* <p>
* WARN: This is not a atomic sequencer,
diff --git a/src/test/java/net/onrc/onos/core/util/distributed/sharedlog/SeqNumTest.java b/src/test/java/net/onrc/onos/core/util/distributed/sharedlog/SeqNumTest.java
index 9ae7897..a37ee8d 100644
--- a/src/test/java/net/onrc/onos/core/util/distributed/sharedlog/SeqNumTest.java
+++ b/src/test/java/net/onrc/onos/core/util/distributed/sharedlog/SeqNumTest.java
@@ -154,4 +154,24 @@
assertThat(negOne.compareTo(negOne.step(Long.MAX_VALUE).next().next()), greaterThan(0));
assertThat(negOne.step(Long.MAX_VALUE).next().next().compareTo(negOne), lessThan(0));
}
+
+ /**
+ * Tests parsing decimal unsigned long.
+ */
+ @Test
+ public void testDecStr() {
+ final SeqNum ref = SeqNum.valueOf(Long.MAX_VALUE + 1);
+ assertEquals(ref, SeqNum.valueOf("9223372036854775808"));
+ assertEquals(ref, SeqNum.anyValueOf("9223372036854775808"));
+ }
+
+ /**
+ * Tests parsing hexadecimal unsigned long.
+ */
+ @Test
+ public void testHexStr() {
+ final SeqNum ref = SeqNum.valueOf(Long.MAX_VALUE + 1);
+ assertEquals(ref, SeqNum.valueOf("0x8000000000000000"));
+ assertEquals(ref, SeqNum.anyValueOf("0x8000000000000000"));
+ }
}