Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use LazyEvaluator for SpanId creation #3685

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions sentry/src/main/java/io/sentry/SpanId.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,54 @@
package io.sentry;

import io.sentry.util.Objects;
import io.sentry.util.LazyEvaluator;
import io.sentry.util.StringUtils;
import java.io.IOException;
import java.util.UUID;
import org.jetbrains.annotations.NotNull;

public final class SpanId implements JsonSerializable {
public static final SpanId EMPTY_ID = new SpanId(new UUID(0, 0));
public static final SpanId EMPTY_ID = new SpanId("00000000-0000-0000-0000-000000000000");

private final @NotNull String value;
private final @NotNull LazyEvaluator<String> lazyValue;

public SpanId(final @NotNull String value) {
this.value = Objects.requireNonNull(value, "value is required");
Objects.requireNonNull(value, "value is required")
this.lazyValue = new LazyEvaluator<>(() -> value);
lbloder marked this conversation as resolved.
Show resolved Hide resolved
}

public SpanId() {
this(UUID.randomUUID());
}

private SpanId(final @NotNull UUID uuid) {
this(StringUtils.normalizeUUID(uuid.toString()).replace("-", "").substring(0, 16));
this.lazyValue =
new LazyEvaluator<>(
() ->
StringUtils.normalizeUUID(UUID.randomUUID().toString())
.replace("-", "")
.substring(0, 16));
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SpanId spanId = (SpanId) o;
return value.equals(spanId.value);
return lazyValue.getValue().equals(spanId.lazyValue.getValue());
}

@Override
public int hashCode() {
return value.hashCode();
return lazyValue.getValue().hashCode();
}

@Override
public String toString() {
return this.value;
return lazyValue.getValue();
}

// JsonElementSerializer

@Override
public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger logger)
throws IOException {
writer.value(value);
writer.value(lazyValue.getValue());
}

// JsonElementDeserializer
Expand Down
32 changes: 18 additions & 14 deletions sentry/src/main/java/io/sentry/protocol/SentryId.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,58 @@
import io.sentry.JsonSerializable;
import io.sentry.ObjectReader;
import io.sentry.ObjectWriter;
import io.sentry.util.LazyEvaluator;
import io.sentry.util.StringUtils;
import java.io.IOException;
import java.util.UUID;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class SentryId implements JsonSerializable {
private final @NotNull UUID uuid;

public static final SentryId EMPTY_ID = new SentryId(new UUID(0, 0));

private final @NotNull LazyEvaluator<UUID> lazyValue;

public SentryId() {
this((UUID) null);
}

public SentryId(@Nullable UUID uuid) {
if (uuid == null) {
uuid = UUID.randomUUID();
if (uuid != null) {
this.lazyValue = new LazyEvaluator<>(() -> uuid);
} else {
this.lazyValue = new LazyEvaluator<>(UUID::randomUUID);
}
this.uuid = uuid;
}

public SentryId(final @NotNull String sentryIdString) {
this.uuid = fromStringSentryId(StringUtils.normalizeUUID(sentryIdString));
if (sentryIdString.length() != 32 && sentryIdString.length() != 36) {
throw new IllegalArgumentException(
"String representation of SentryId has either 32 (UUID no dashes) "
+ "or 36 characters long (completed UUID). Received: "
+ sentryIdString);
}
this.lazyValue =
new LazyEvaluator<>(() -> fromStringSentryId(StringUtils.normalizeUUID(sentryIdString)));
}

@Override
public String toString() {
return StringUtils.normalizeUUID(uuid.toString()).replace("-", "");
return StringUtils.normalizeUUID(lazyValue.getValue().toString()).replace("-", "");
}

@Override
public boolean equals(final @Nullable Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SentryId sentryId = (SentryId) o;
return uuid.compareTo(sentryId.uuid) == 0;
return lazyValue.getValue().compareTo(sentryId.lazyValue.getValue()) == 0;
}

@Override
public int hashCode() {
return uuid.hashCode();
return lazyValue.getValue().hashCode();
}

private @NotNull UUID fromStringSentryId(@NotNull String sentryIdString) {
Expand All @@ -60,12 +70,6 @@ public int hashCode() {
.insert(23, "-")
.toString();
}
if (sentryIdString.length() != 36) {
throw new IllegalArgumentException(
"String representation of SentryId has either 32 (UUID no dashes) "
+ "or 36 characters long (completed UUID). Received: "
+ sentryIdString);
}

return UUID.fromString(sentryIdString);
}
Expand Down
Loading