2323import org .apache .hugegraph .backend .id .Id .IdType ;
2424import org .apache .hugegraph .backend .serializer .BytesBuffer ;
2525import org .apache .hugegraph .structure .HugeVertex ;
26+ import org .apache .hugegraph .util .Bytes ;
2627import org .apache .hugegraph .util .E ;
2728import org .apache .hugegraph .util .LongEncoding ;
2829import org .apache .hugegraph .util .NumericUtil ;
@@ -128,14 +129,20 @@ public static int compareType(Id id1, Id id2) {
128129 public static class StringId implements Id {
129130
130131 protected String id ;
132+ protected byte [] bytes ;
131133
132134 public StringId (String id ) {
133- E .checkArgument (!id .isEmpty (), "The id can't be empty" );
135+ E .checkArgument (id != null && !id .isEmpty (),
136+ "The id can't be null or empty" );
134137 this .id = id ;
138+ this .bytes = null ;
135139 }
136140
137141 public StringId (byte [] bytes ) {
138- this .id = StringEncoding .decode (bytes );
142+ E .checkArgument (bytes != null && bytes .length > 0 ,
143+ "The id bytes can't be null or empty" );
144+ this .bytes = bytes ;
145+ this .id = null ;
139146 }
140147
141148 @ Override
@@ -145,27 +152,35 @@ public IdType type() {
145152
146153 @ Override
147154 public Object asObject () {
148- return this .id ;
155+ return this .asString () ;
149156 }
150157
151158 @ Override
152159 public String asString () {
160+ if (this .id == null ) {
161+ assert this .bytes != null ;
162+ this .id = StringEncoding .decode (this .bytes );
163+ }
153164 return this .id ;
154165 }
155166
156167 @ Override
157168 public long asLong () {
158- return Long .parseLong (this .id );
169+ return Long .parseLong (this .asString () );
159170 }
160171
161172 @ Override
162173 public byte [] asBytes () {
163- return StringEncoding .encode (this .id );
174+ if (this .bytes == null ) {
175+ assert this .id != null ;
176+ this .bytes = StringEncoding .encode (this .id );
177+ }
178+ return this .bytes ;
164179 }
165180
166181 @ Override
167182 public int length () {
168- return this .id .length ();
183+ return this .asString () .length ();
169184 }
170185
171186 @ Override
@@ -174,25 +189,37 @@ public int compareTo(Id other) {
174189 if (cmp != 0 ) {
175190 return cmp ;
176191 }
177- return this .id .compareTo (other .asString ());
192+ if (this .id != null ) {
193+ return this .id .compareTo (other .asString ());
194+ } else {
195+ return Bytes .compare (this .bytes , other .asBytes ());
196+ }
178197 }
179198
180199 @ Override
181200 public int hashCode () {
182- return this .id .hashCode ();
201+ return this .asString () .hashCode ();
183202 }
184203
185204 @ Override
186- public boolean equals (Object other ) {
187- if (!(other instanceof StringId )) {
205+ public boolean equals (Object obj ) {
206+ if (!(obj instanceof StringId )) {
188207 return false ;
189208 }
190- return this .id .equals (((StringId ) other ).id );
209+ StringId other = (StringId ) obj ;
210+ if (this .id != null ) {
211+ return this .id .equals (other .asString ());
212+ } else if (other .bytes == null ) {
213+ return this .asString ().equals (other .asString ());
214+ } else {
215+ assert this .bytes != null ;
216+ return Bytes .equals (this .bytes , other .asBytes ());
217+ }
191218 }
192219
193220 @ Override
194221 public String toString () {
195- return this .id ;
222+ return this .asString () ;
196223 }
197224 }
198225
0 commit comments