org.glowroot.instrumentation.test.harness.ImmutableLocalSpan Maven / Gradle / Ivy
package org.glowroot.instrumentation.test.harness;
import org.glowroot.instrumentation.test.harness.shaded.com.google.errorprone.annotations.CanIgnoreReturnValue;
import org.glowroot.instrumentation.test.harness.shaded.com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link LocalSpan}.
*
* Use the builder to create immutable instances:
* {@code ImmutableLocalSpan.builder()}.
*/
@Generated(from = "LocalSpan", generator = "Immutables")
@SuppressWarnings({"all"})
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
public final class ImmutableLocalSpan implements LocalSpan {
private final long totalNanos;
private final String message;
private final Map detail;
private final @Nullable Long locationStackTraceMillis;
private final @Nullable ThrowableInfo exception;
private final List childSpans;
private ImmutableLocalSpan(
long totalNanos,
String message,
Map detail,
@Nullable Long locationStackTraceMillis,
@Nullable ThrowableInfo exception,
List childSpans) {
this.totalNanos = totalNanos;
this.message = message;
this.detail = detail;
this.locationStackTraceMillis = locationStackTraceMillis;
this.exception = exception;
this.childSpans = childSpans;
}
/**
* @return The value of the {@code totalNanos} attribute
*/
@Override
public long totalNanos() {
return totalNanos;
}
/**
* @return The value of the {@code message} attribute
*/
@Override
public String message() {
return message;
}
/**
* @return The value of the {@code detail} attribute
*/
@Override
public Map detail() {
return detail;
}
/**
* @return The value of the {@code locationStackTraceMillis} attribute
*/
@Override
public @Nullable Long locationStackTraceMillis() {
return locationStackTraceMillis;
}
/**
* @return The value of the {@code exception} attribute
*/
@Override
public @Nullable ThrowableInfo exception() {
return exception;
}
/**
* @return The value of the {@code childSpans} attribute
*/
@Override
public List childSpans() {
return childSpans;
}
/**
* Copy the current immutable object by setting a value for the {@link LocalSpan#totalNanos() totalNanos} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for totalNanos
* @return A modified copy of the {@code this} object
*/
public final ImmutableLocalSpan withTotalNanos(long value) {
if (this.totalNanos == value) return this;
return new ImmutableLocalSpan(
value,
this.message,
this.detail,
this.locationStackTraceMillis,
this.exception,
this.childSpans);
}
/**
* Copy the current immutable object by setting a value for the {@link LocalSpan#message() message} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for message
* @return A modified copy of the {@code this} object
*/
public final ImmutableLocalSpan withMessage(String value) {
String newValue = ImmutableLocalSpan.requireNonNull(value, "message");
if (this.message.equals(newValue)) return this;
return new ImmutableLocalSpan(
this.totalNanos,
newValue,
this.detail,
this.locationStackTraceMillis,
this.exception,
this.childSpans);
}
/**
* Copy the current immutable object by replacing the {@link LocalSpan#detail() detail} map with the specified map.
* Nulls are not permitted as keys or values.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param entries The entries to be added to the detail map
* @return A modified copy of {@code this} object
*/
public final ImmutableLocalSpan withDetail(Map entries) {
if (this.detail == entries) return this;
Map newValue = createUnmodifiableMap(false, false, entries);
return new ImmutableLocalSpan(
this.totalNanos,
this.message,
newValue,
this.locationStackTraceMillis,
this.exception,
this.childSpans);
}
/**
* Copy the current immutable object by setting a value for the {@link LocalSpan#locationStackTraceMillis() locationStackTraceMillis} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for locationStackTraceMillis (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableLocalSpan withLocationStackTraceMillis(@Nullable Long value) {
if (ImmutableLocalSpan.equals(this.locationStackTraceMillis, value)) return this;
return new ImmutableLocalSpan(this.totalNanos, this.message, this.detail, value, this.exception, this.childSpans);
}
/**
* Copy the current immutable object by setting a value for the {@link LocalSpan#exception() exception} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for exception (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableLocalSpan withException(@Nullable ThrowableInfo value) {
if (this.exception == value) return this;
return new ImmutableLocalSpan(
this.totalNanos,
this.message,
this.detail,
this.locationStackTraceMillis,
value,
this.childSpans);
}
/**
* Copy the current immutable object with elements that replace the content of {@link LocalSpan#childSpans() childSpans}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableLocalSpan withChildSpans(Span... elements) {
List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new ImmutableLocalSpan(
this.totalNanos,
this.message,
this.detail,
this.locationStackTraceMillis,
this.exception,
newValue);
}
/**
* Copy the current immutable object with elements that replace the content of {@link LocalSpan#childSpans() childSpans}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of childSpans elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableLocalSpan withChildSpans(Iterable extends Span> elements) {
if (this.childSpans == elements) return this;
List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new ImmutableLocalSpan(
this.totalNanos,
this.message,
this.detail,
this.locationStackTraceMillis,
this.exception,
newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableLocalSpan} that have equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(Object another) {
if (this == another) return true;
return another instanceof ImmutableLocalSpan
&& equalTo((ImmutableLocalSpan) another);
}
private boolean equalTo(ImmutableLocalSpan another) {
return totalNanos == another.totalNanos
&& message.equals(another.message)
&& detail.equals(another.detail)
&& ImmutableLocalSpan.equals(locationStackTraceMillis, another.locationStackTraceMillis)
&& ImmutableLocalSpan.equals(exception, another.exception)
&& childSpans.equals(another.childSpans);
}
/**
* Computes a hash code from attributes: {@code totalNanos}, {@code message}, {@code detail}, {@code locationStackTraceMillis}, {@code exception}, {@code childSpans}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + (int) (totalNanos ^ (totalNanos >>> 32));
h += (h << 5) + message.hashCode();
h += (h << 5) + detail.hashCode();
h += (h << 5) + ImmutableLocalSpan.hashCode(locationStackTraceMillis);
h += (h << 5) + ImmutableLocalSpan.hashCode(exception);
h += (h << 5) + childSpans.hashCode();
return h;
}
/**
* Prints the immutable value {@code LocalSpan} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "LocalSpan{"
+ "totalNanos=" + totalNanos
+ ", message=" + message
+ ", detail=" + detail
+ ", locationStackTraceMillis=" + locationStackTraceMillis
+ ", exception=" + exception
+ ", childSpans=" + childSpans
+ "}";
}
/**
* Creates an immutable copy of a {@link LocalSpan} value.
* Uses accessors to get values to initialize the new immutable instance.
* If an instance is already immutable, it is returned as is.
* @param instance The instance to copy
* @return A copied immutable LocalSpan instance
*/
public static ImmutableLocalSpan copyOf(LocalSpan instance) {
if (instance instanceof ImmutableLocalSpan) {
return (ImmutableLocalSpan) instance;
}
return ImmutableLocalSpan.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableLocalSpan ImmutableLocalSpan}.
*
* ImmutableLocalSpan.builder()
* .totalNanos(long) // required {@link LocalSpan#totalNanos() totalNanos}
* .message(String) // required {@link LocalSpan#message() message}
* .putDetail|putAllDetail(String => Object) // {@link LocalSpan#detail() detail} mappings
* .locationStackTraceMillis(Long | null) // nullable {@link LocalSpan#locationStackTraceMillis() locationStackTraceMillis}
* .exception(org.glowroot.instrumentation.test.harness.ThrowableInfo | null) // nullable {@link LocalSpan#exception() exception}
* .addChildSpans|addAllChildSpans(org.glowroot.instrumentation.test.harness.Span) // {@link LocalSpan#childSpans() childSpans} elements
* .build();
*
* @return A new ImmutableLocalSpan builder
*/
public static ImmutableLocalSpan.Builder builder() {
return new ImmutableLocalSpan.Builder();
}
/**
* Builds instances of type {@link ImmutableLocalSpan ImmutableLocalSpan}.
* Initialize attributes and then invoke the {@link #build()} method to create an
* immutable instance.
* {@code Builder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.
*/
@Generated(from = "LocalSpan", generator = "Immutables")
public static final class Builder {
private static final long INIT_BIT_TOTAL_NANOS = 0x1L;
private static final long INIT_BIT_MESSAGE = 0x2L;
private long initBits = 0x3L;
private long totalNanos;
private String message;
private Map detail = new LinkedHashMap();
private Long locationStackTraceMillis;
private ThrowableInfo exception;
private List childSpans = new ArrayList();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code org.glowroot.instrumentation.test.harness.Span} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(Span instance) {
ImmutableLocalSpan.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code org.glowroot.instrumentation.test.harness.LocalSpan} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(LocalSpan instance) {
ImmutableLocalSpan.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
if (object instanceof Span) {
Span instance = (Span) object;
putAllDetail(instance.detail());
message(instance.message());
@Nullable Long locationStackTraceMillisValue = instance.locationStackTraceMillis();
if (locationStackTraceMillisValue != null) {
locationStackTraceMillis(locationStackTraceMillisValue);
}
totalNanos(instance.totalNanos());
}
if (object instanceof LocalSpan) {
LocalSpan instance = (LocalSpan) object;
@Nullable ThrowableInfo exceptionValue = instance.exception();
if (exceptionValue != null) {
exception(exceptionValue);
}
addAllChildSpans(instance.childSpans());
}
}
/**
* Initializes the value for the {@link LocalSpan#totalNanos() totalNanos} attribute.
* @param totalNanos The value for totalNanos
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder totalNanos(long totalNanos) {
this.totalNanos = totalNanos;
initBits &= ~INIT_BIT_TOTAL_NANOS;
return this;
}
/**
* Initializes the value for the {@link LocalSpan#message() message} attribute.
* @param message The value for message
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder message(String message) {
this.message = ImmutableLocalSpan.requireNonNull(message, "message");
initBits &= ~INIT_BIT_MESSAGE;
return this;
}
/**
* Put one entry to the {@link LocalSpan#detail() detail} map.
* @param key The key in the detail map
* @param value The associated value in the detail map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putDetail(String key, Object value) {
this.detail.put(key, value);
return this;
}
/**
* Put one entry to the {@link LocalSpan#detail() detail} map. Nulls are not permitted
* @param entry The key and value entry
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putDetail(Map.Entry entry) {
String k = entry.getKey();
Object v = entry.getValue();
this.detail.put(k, v);
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link LocalSpan#detail() detail} map. Nulls are not permitted
* @param entries The entries that will be added to the detail map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder detail(Map entries) {
this.detail.clear();
return putAllDetail(entries);
}
/**
* Put all mappings from the specified map as entries to {@link LocalSpan#detail() detail} map. Nulls are not permitted
* @param entries The entries that will be added to the detail map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putAllDetail(Map entries) {
for (Map.Entry e : entries.entrySet()) {
String k = e.getKey();
Object v = e.getValue();
this.detail.put(k, v);
}
return this;
}
/**
* Initializes the value for the {@link LocalSpan#locationStackTraceMillis() locationStackTraceMillis} attribute.
* @param locationStackTraceMillis The value for locationStackTraceMillis (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder locationStackTraceMillis(@Nullable Long locationStackTraceMillis) {
this.locationStackTraceMillis = locationStackTraceMillis;
return this;
}
/**
* Initializes the value for the {@link LocalSpan#exception() exception} attribute.
* @param exception The value for exception (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder exception(@Nullable ThrowableInfo exception) {
this.exception = exception;
return this;
}
/**
* Adds one element to {@link LocalSpan#childSpans() childSpans} list.
* @param element A childSpans element
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addChildSpans(Span element) {
this.childSpans.add(ImmutableLocalSpan.requireNonNull(element, "childSpans element"));
return this;
}
/**
* Adds elements to {@link LocalSpan#childSpans() childSpans} list.
* @param elements An array of childSpans elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addChildSpans(Span... elements) {
for (Span element : elements) {
this.childSpans.add(ImmutableLocalSpan.requireNonNull(element, "childSpans element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link LocalSpan#childSpans() childSpans} list.
* @param elements An iterable of childSpans elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder childSpans(Iterable extends Span> elements) {
this.childSpans.clear();
return addAllChildSpans(elements);
}
/**
* Adds elements to {@link LocalSpan#childSpans() childSpans} list.
* @param elements An iterable of childSpans elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addAllChildSpans(Iterable extends Span> elements) {
for (Span element : elements) {
this.childSpans.add(ImmutableLocalSpan.requireNonNull(element, "childSpans element"));
}
return this;
}
/**
* Builds a new {@link ImmutableLocalSpan ImmutableLocalSpan}.
* @return An immutable instance of LocalSpan
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableLocalSpan build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableLocalSpan(
totalNanos,
message,
createUnmodifiableMap(false, false, detail),
locationStackTraceMillis,
exception,
createUnmodifiableList(true, childSpans));
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList();
if ((initBits & INIT_BIT_TOTAL_NANOS) != 0) attributes.add("totalNanos");
if ((initBits & INIT_BIT_MESSAGE) != 0) attributes.add("message");
return "Cannot build LocalSpan, some of required attributes are not set " + attributes;
}
}
private static T requireNonNull(T object, String message) {
if (object == null) throw new NullPointerException(message);
return object;
}
private static int hashCode(Object object) {
return object != null ? object.hashCode() : 0;
}
private static boolean equals(Object left, Object right) {
return left == right || (left != null && left.equals(right));
}
private static List createSafeList(Iterable extends T> iterable, boolean checkNulls, boolean skipNulls) {
ArrayList list;
if (iterable instanceof Collection>) {
int size = ((Collection>) iterable).size();
if (size == 0) return Collections.emptyList();
list = new ArrayList();
} else {
list = new ArrayList();
}
for (T element : iterable) {
if (skipNulls && element == null) continue;
if (checkNulls) ImmutableLocalSpan.requireNonNull(element, "element");
list.add(element);
}
return list;
}
private static List createUnmodifiableList(boolean clone, List list) {
switch(list.size()) {
case 0: return Collections.emptyList();
case 1: return Collections.singletonList(list.get(0));
default:
if (clone) {
return Collections.unmodifiableList(new ArrayList(list));
} else {
if (list instanceof ArrayList>) {
((ArrayList>) list).trimToSize();
}
return Collections.unmodifiableList(list);
}
}
}
private static Map createUnmodifiableMap(boolean checkNulls, boolean skipNulls, Map extends K, ? extends V> map) {
switch (map.size()) {
case 0: return Collections.emptyMap();
case 1: {
Map.Entry extends K, ? extends V> e = map.entrySet().iterator().next();
K k = e.getKey();
V v = e.getValue();
if (checkNulls) {
ImmutableLocalSpan.requireNonNull(k, "key");
ImmutableLocalSpan.requireNonNull(v, "value");
}
if (skipNulls && (k == null || v == null)) {
return Collections.emptyMap();
}
return Collections.singletonMap(k, v);
}
default: {
Map linkedMap = new LinkedHashMap(map.size());
if (skipNulls || checkNulls) {
for (Map.Entry extends K, ? extends V> e : map.entrySet()) {
K k = e.getKey();
V v = e.getValue();
if (skipNulls) {
if (k == null || v == null) continue;
} else if (checkNulls) {
ImmutableLocalSpan.requireNonNull(k, "key");
ImmutableLocalSpan.requireNonNull(v, "value");
}
linkedMap.put(k, v);
}
} else {
linkedMap.putAll(map);
}
return Collections.unmodifiableMap(linkedMap);
}
}
}
}