|
5 | 5 | import io.sentry.JsonSerializable; |
6 | 6 | import io.sentry.ObjectReader; |
7 | 7 | import io.sentry.ObjectWriter; |
| 8 | +import io.sentry.util.LazyEvaluator; |
8 | 9 | import io.sentry.util.StringUtils; |
9 | 10 | import java.io.IOException; |
10 | 11 | import java.util.UUID; |
11 | 12 | import org.jetbrains.annotations.NotNull; |
12 | 13 | import org.jetbrains.annotations.Nullable; |
13 | 14 |
|
14 | 15 | public final class SentryId implements JsonSerializable { |
15 | | - private final @NotNull UUID uuid; |
16 | 16 |
|
17 | 17 | public static final SentryId EMPTY_ID = new SentryId(new UUID(0, 0)); |
18 | 18 |
|
| 19 | + private final @NotNull LazyEvaluator<String> lazyStringValue; |
| 20 | + |
19 | 21 | public SentryId() { |
20 | 22 | this((UUID) null); |
21 | 23 | } |
22 | 24 |
|
23 | 25 | public SentryId(@Nullable UUID uuid) { |
24 | | - if (uuid == null) { |
25 | | - uuid = UUID.randomUUID(); |
| 26 | + if (uuid != null) { |
| 27 | + this.lazyStringValue = new LazyEvaluator<>(() -> uuidToSentryIdString(uuid)); |
| 28 | + } else { |
| 29 | + this.lazyStringValue = new LazyEvaluator<>(() -> uuidToSentryIdString(UUID.randomUUID())); |
26 | 30 | } |
27 | | - this.uuid = uuid; |
28 | 31 | } |
29 | 32 |
|
30 | 33 | public SentryId(final @NotNull String sentryIdString) { |
31 | | - this.uuid = fromStringSentryId(StringUtils.normalizeUUID(sentryIdString)); |
| 34 | + String normalized = StringUtils.normalizeUUID(sentryIdString); |
| 35 | + if (normalized.length() != 32 && normalized.length() != 36) { |
| 36 | + throw new IllegalArgumentException( |
| 37 | + "String representation of SentryId has either 32 (UUID no dashes) " |
| 38 | + + "or 36 characters long (completed UUID). Received: " |
| 39 | + + sentryIdString); |
| 40 | + } |
| 41 | + this.lazyStringValue = new LazyEvaluator<>(() -> uuidStringToSentryIdString(normalized)); |
32 | 42 | } |
33 | 43 |
|
34 | 44 | @Override |
35 | 45 | public String toString() { |
36 | | - return StringUtils.normalizeUUID(uuid.toString()).replace("-", ""); |
| 46 | + return lazyStringValue.getValue(); |
37 | 47 | } |
38 | 48 |
|
39 | 49 | @Override |
40 | 50 | public boolean equals(final @Nullable Object o) { |
41 | 51 | if (this == o) return true; |
42 | 52 | if (o == null || getClass() != o.getClass()) return false; |
43 | 53 | SentryId sentryId = (SentryId) o; |
44 | | - return uuid.compareTo(sentryId.uuid) == 0; |
| 54 | + return lazyStringValue.getValue().compareTo(sentryId.lazyStringValue.getValue()) == 0; |
45 | 55 | } |
46 | 56 |
|
47 | 57 | @Override |
48 | 58 | public int hashCode() { |
49 | | - return uuid.hashCode(); |
| 59 | + return lazyStringValue.getValue().hashCode(); |
50 | 60 | } |
51 | 61 |
|
52 | | - private @NotNull UUID fromStringSentryId(@NotNull String sentryIdString) { |
53 | | - if (sentryIdString.length() == 32) { |
54 | | - // expected format, SentryId is a UUID without dashes |
55 | | - sentryIdString = |
56 | | - new StringBuilder(sentryIdString) |
57 | | - .insert(8, "-") |
58 | | - .insert(13, "-") |
59 | | - .insert(18, "-") |
60 | | - .insert(23, "-") |
61 | | - .toString(); |
62 | | - } |
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 | | - } |
| 62 | + private @NotNull String uuidToSentryIdString(@NotNull UUID uuid) { |
| 63 | + return uuidStringToSentryIdString(uuid.toString()); |
| 64 | + } |
69 | 65 |
|
70 | | - return UUID.fromString(sentryIdString); |
| 66 | + private @NotNull String uuidStringToSentryIdString(@NotNull String uuidString) { |
| 67 | + return StringUtils.normalizeUUID(uuidString).replace("-", ""); |
71 | 68 | } |
72 | 69 |
|
73 | 70 | // JsonSerializable |
|
0 commit comments