55import io .sentry .JsonSerializable ;
66import io .sentry .ObjectReader ;
77import io .sentry .ObjectWriter ;
8+ import io .sentry .util .LazyEvaluator ;
89import io .sentry .util .StringUtils ;
910import java .io .IOException ;
1011import java .util .UUID ;
1112import org .jetbrains .annotations .NotNull ;
1213import org .jetbrains .annotations .Nullable ;
1314
1415public final class SentryId implements JsonSerializable {
15- private final @ NotNull UUID uuid ;
1616
1717 public static final SentryId EMPTY_ID = new SentryId (new UUID (0 , 0 ));
1818
19+ private final @ NotNull LazyEvaluator <UUID > lazyValue ;
20+
1921 public SentryId () {
2022 this ((UUID ) null );
2123 }
2224
2325 public SentryId (@ Nullable UUID uuid ) {
24- if (uuid == null ) {
25- uuid = UUID .randomUUID ();
26+ if (uuid != null ) {
27+ this .lazyValue = new LazyEvaluator <>(() -> uuid );
28+ } else {
29+ this .lazyValue = new LazyEvaluator <>(UUID ::randomUUID );
2630 }
27- this .uuid = uuid ;
2831 }
2932
3033 public SentryId (final @ NotNull String sentryIdString ) {
31- this .uuid = fromStringSentryId (StringUtils .normalizeUUID (sentryIdString ));
34+ if (sentryIdString .length () != 32 && sentryIdString .length () != 36 ) {
35+ throw new IllegalArgumentException (
36+ "String representation of SentryId has either 32 (UUID no dashes) "
37+ + "or 36 characters long (completed UUID). Received: "
38+ + sentryIdString );
39+ }
40+ this .lazyValue =
41+ new LazyEvaluator <>(() -> fromStringSentryId (StringUtils .normalizeUUID (sentryIdString )));
3242 }
3343
3444 @ Override
3545 public String toString () {
36- return StringUtils .normalizeUUID (uuid .toString ()).replace ("-" , "" );
46+ return StringUtils .normalizeUUID (lazyValue . getValue () .toString ()).replace ("-" , "" );
3747 }
3848
3949 @ Override
4050 public boolean equals (final @ Nullable Object o ) {
4151 if (this == o ) return true ;
4252 if (o == null || getClass () != o .getClass ()) return false ;
4353 SentryId sentryId = (SentryId ) o ;
44- return uuid . compareTo (sentryId .uuid ) == 0 ;
54+ return lazyValue . getValue (). compareTo (sentryId .lazyValue . getValue () ) == 0 ;
4555 }
4656
4757 @ Override
4858 public int hashCode () {
49- return uuid .hashCode ();
59+ return lazyValue . getValue () .hashCode ();
5060 }
5161
5262 private @ NotNull UUID fromStringSentryId (@ NotNull String sentryIdString ) {
@@ -60,12 +70,6 @@ public int hashCode() {
6070 .insert (23 , "-" )
6171 .toString ();
6272 }
63- if (sentryIdString .length () != 36 ) {
64- throw new IllegalArgumentException (
65- "String representation of SentryId has either 32 (UUID no dashes) "
66- + "or 36 characters long (completed UUID). Received: "
67- + sentryIdString );
68- }
6973
7074 return UUID .fromString (sentryIdString );
7175 }
0 commit comments