@@ -47,18 +47,18 @@ public final class NitriteId implements Comparable<NitriteId>, Serializable {
4747 private static final long serialVersionUID = 1477462375L ;
4848 private static final SnowflakeIdGenerator generator = new SnowflakeIdGenerator ();
4949
50- /**
51- * Gets the underlying value of the NitriteId.
52- * <p>
53- * The value is a string representation of a 64bit integer number.
54- */
55- private String idValue ;
50+ /** The underlying value of the NitriteId. */
51+ private long idValue ;
5652
5753 private NitriteId () {
58- this .idValue = Long . toString ( generator .getId () );
54+ this .idValue = generator .getId ();
5955 }
6056
6157 private NitriteId (String value ) {
58+ this .idValue = Long .parseLong (value );
59+ }
60+
61+ private NitriteId (long value ) {
6262 this .idValue = value ;
6363 }
6464
@@ -84,6 +84,17 @@ public static NitriteId createId(String value) {
8484 return new NitriteId (value );
8585 }
8686
87+ /**
88+ * Creates a {@link NitriteId} from a {@code long} value.
89+ *
90+ * @param value the value
91+ * @return the {@link NitriteId}
92+ */
93+ public static NitriteId createId (long value ) {
94+ validId (value );
95+ return new NitriteId (value );
96+ }
97+
8798 /**
8899 * Validates a value to be used as {@link NitriteId}.
89100 * <p>
@@ -106,26 +117,19 @@ public static boolean validId(Object value) {
106117
107118 @ Override
108119 public int compareTo (NitriteId other ) {
109- if (other .idValue == null ) {
110- throw new InvalidIdException ("Cannot compare with null id" );
111- }
112-
113- return Long .compare (Long .parseLong (idValue ), Long .parseLong (other .idValue ));
120+ return Long .compare (idValue , other .idValue );
114121 }
115122
116123 @ Override
117124 public String toString () {
118- if (idValue != null ) {
119- return ID_PREFIX + idValue + ID_SUFFIX ;
120- }
121- return "" ;
125+ return ID_PREFIX + idValue + ID_SUFFIX ;
122126 }
123127
124128 private void writeObject (ObjectOutputStream stream ) throws IOException {
125- stream .writeUTF (idValue );
129+ stream .writeUTF (Long . toString ( idValue ) );
126130 }
127131
128132 private void readObject (ObjectInputStream stream ) throws IOException {
129- idValue = stream .readUTF ();
133+ idValue = Long . parseLong ( stream .readUTF () );
130134 }
131135}
0 commit comments