org.glowroot.instrumentation.test.harness.ImmutableOutgoingSpan 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.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 OutgoingSpan}.
*
* Use the builder to create immutable instances:
* {@code ImmutableOutgoingSpan.builder()}.
*/
@Generated(from = "OutgoingSpan", generator = "Immutables")
@SuppressWarnings({"all"})
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
public final class ImmutableOutgoingSpan implements OutgoingSpan {
private final long totalNanos;
private final String message;
private final Map detail;
private final @Nullable Long locationStackTraceMillis;
private final String type;
private final String dest;
private final @Nullable ThrowableInfo exception;
private ImmutableOutgoingSpan(
long totalNanos,
String message,
Map detail,
@Nullable Long locationStackTraceMillis,
String type,
String dest,
@Nullable ThrowableInfo exception) {
this.totalNanos = totalNanos;
this.message = message;
this.detail = detail;
this.locationStackTraceMillis = locationStackTraceMillis;
this.type = type;
this.dest = dest;
this.exception = exception;
}
/**
* @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 type} attribute
*/
@Override
public String type() {
return type;
}
/**
* @return The value of the {@code dest} attribute
*/
@Override
public String dest() {
return dest;
}
/**
* @return The value of the {@code exception} attribute
*/
@Override
public @Nullable ThrowableInfo exception() {
return exception;
}
/**
* Copy the current immutable object by setting a value for the {@link OutgoingSpan#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 ImmutableOutgoingSpan withTotalNanos(long value) {
if (this.totalNanos == value) return this;
return new ImmutableOutgoingSpan(
value,
this.message,
this.detail,
this.locationStackTraceMillis,
this.type,
this.dest,
this.exception);
}
/**
* Copy the current immutable object by setting a value for the {@link OutgoingSpan#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 ImmutableOutgoingSpan withMessage(String value) {
String newValue = ImmutableOutgoingSpan.requireNonNull(value, "message");
if (this.message.equals(newValue)) return this;
return new ImmutableOutgoingSpan(
this.totalNanos,
newValue,
this.detail,
this.locationStackTraceMillis,
this.type,
this.dest,
this.exception);
}
/**
* Copy the current immutable object by replacing the {@link OutgoingSpan#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 ImmutableOutgoingSpan withDetail(Map entries) {
if (this.detail == entries) return this;
Map newValue = createUnmodifiableMap(false, false, entries);
return new ImmutableOutgoingSpan(
this.totalNanos,
this.message,
newValue,
this.locationStackTraceMillis,
this.type,
this.dest,
this.exception);
}
/**
* Copy the current immutable object by setting a value for the {@link OutgoingSpan#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 ImmutableOutgoingSpan withLocationStackTraceMillis(@Nullable Long value) {
if (ImmutableOutgoingSpan.equals(this.locationStackTraceMillis, value)) return this;
return new ImmutableOutgoingSpan(this.totalNanos, this.message, this.detail, value, this.type, this.dest, this.exception);
}
/**
* Copy the current immutable object by setting a value for the {@link OutgoingSpan#type() type} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for type
* @return A modified copy of the {@code this} object
*/
public final ImmutableOutgoingSpan withType(String value) {
String newValue = ImmutableOutgoingSpan.requireNonNull(value, "type");
if (this.type.equals(newValue)) return this;
return new ImmutableOutgoingSpan(
this.totalNanos,
this.message,
this.detail,
this.locationStackTraceMillis,
newValue,
this.dest,
this.exception);
}
/**
* Copy the current immutable object by setting a value for the {@link OutgoingSpan#dest() dest} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for dest
* @return A modified copy of the {@code this} object
*/
public final ImmutableOutgoingSpan withDest(String value) {
String newValue = ImmutableOutgoingSpan.requireNonNull(value, "dest");
if (this.dest.equals(newValue)) return this;
return new ImmutableOutgoingSpan(
this.totalNanos,
this.message,
this.detail,
this.locationStackTraceMillis,
this.type,
newValue,
this.exception);
}
/**
* Copy the current immutable object by setting a value for the {@link OutgoingSpan#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 ImmutableOutgoingSpan withException(@Nullable ThrowableInfo value) {
if (this.exception == value) return this;
return new ImmutableOutgoingSpan(
this.totalNanos,
this.message,
this.detail,
this.locationStackTraceMillis,
this.type,
this.dest,
value);
}
/**
* This instance is equal to all instances of {@code ImmutableOutgoingSpan} 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 ImmutableOutgoingSpan
&& equalTo((ImmutableOutgoingSpan) another);
}
private boolean equalTo(ImmutableOutgoingSpan another) {
return totalNanos == another.totalNanos
&& message.equals(another.message)
&& detail.equals(another.detail)
&& ImmutableOutgoingSpan.equals(locationStackTraceMillis, another.locationStackTraceMillis)
&& type.equals(another.type)
&& dest.equals(another.dest)
&& ImmutableOutgoingSpan.equals(exception, another.exception);
}
/**
* Computes a hash code from attributes: {@code totalNanos}, {@code message}, {@code detail}, {@code locationStackTraceMillis}, {@code type}, {@code dest}, {@code exception}.
* @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) + ImmutableOutgoingSpan.hashCode(locationStackTraceMillis);
h += (h << 5) + type.hashCode();
h += (h << 5) + dest.hashCode();
h += (h << 5) + ImmutableOutgoingSpan.hashCode(exception);
return h;
}
/**
* Prints the immutable value {@code OutgoingSpan} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "OutgoingSpan{"
+ "totalNanos=" + totalNanos
+ ", message=" + message
+ ", detail=" + detail
+ ", locationStackTraceMillis=" + locationStackTraceMillis
+ ", type=" + type
+ ", dest=" + dest
+ ", exception=" + exception
+ "}";
}
/**
* Creates an immutable copy of a {@link OutgoingSpan} 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 OutgoingSpan instance
*/
public static ImmutableOutgoingSpan copyOf(OutgoingSpan instance) {
if (instance instanceof ImmutableOutgoingSpan) {
return (ImmutableOutgoingSpan) instance;
}
return ImmutableOutgoingSpan.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableOutgoingSpan ImmutableOutgoingSpan}.
*
* ImmutableOutgoingSpan.builder()
* .totalNanos(long) // required {@link OutgoingSpan#totalNanos() totalNanos}
* .message(String) // required {@link OutgoingSpan#message() message}
* .putDetail|putAllDetail(String => Object) // {@link OutgoingSpan#detail() detail} mappings
* .locationStackTraceMillis(Long | null) // nullable {@link OutgoingSpan#locationStackTraceMillis() locationStackTraceMillis}
* .type(String) // required {@link OutgoingSpan#type() type}
* .dest(String) // required {@link OutgoingSpan#dest() dest}
* .exception(org.glowroot.instrumentation.test.harness.ThrowableInfo | null) // nullable {@link OutgoingSpan#exception() exception}
* .build();
*
* @return A new ImmutableOutgoingSpan builder
*/
public static ImmutableOutgoingSpan.Builder builder() {
return new ImmutableOutgoingSpan.Builder();
}
/**
* Builds instances of type {@link ImmutableOutgoingSpan ImmutableOutgoingSpan}.
* 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 = "OutgoingSpan", 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 static final long INIT_BIT_TYPE = 0x4L;
private static final long INIT_BIT_DEST = 0x8L;
private long initBits = 0xfL;
private long totalNanos;
private String message;
private Map detail = new LinkedHashMap();
private Long locationStackTraceMillis;
private String type;
private String dest;
private ThrowableInfo exception;
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) {
ImmutableOutgoingSpan.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code org.glowroot.instrumentation.test.harness.OutgoingSpan} 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(OutgoingSpan instance) {
ImmutableOutgoingSpan.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 OutgoingSpan) {
OutgoingSpan instance = (OutgoingSpan) object;
@Nullable ThrowableInfo exceptionValue = instance.exception();
if (exceptionValue != null) {
exception(exceptionValue);
}
type(instance.type());
dest(instance.dest());
}
}
/**
* Initializes the value for the {@link OutgoingSpan#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 OutgoingSpan#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 = ImmutableOutgoingSpan.requireNonNull(message, "message");
initBits &= ~INIT_BIT_MESSAGE;
return this;
}
/**
* Put one entry to the {@link OutgoingSpan#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 OutgoingSpan#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 OutgoingSpan#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 OutgoingSpan#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 OutgoingSpan#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 OutgoingSpan#type() type} attribute.
* @param type The value for type
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder type(String type) {
this.type = ImmutableOutgoingSpan.requireNonNull(type, "type");
initBits &= ~INIT_BIT_TYPE;
return this;
}
/**
* Initializes the value for the {@link OutgoingSpan#dest() dest} attribute.
* @param dest The value for dest
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder dest(String dest) {
this.dest = ImmutableOutgoingSpan.requireNonNull(dest, "dest");
initBits &= ~INIT_BIT_DEST;
return this;
}
/**
* Initializes the value for the {@link OutgoingSpan#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;
}
/**
* Builds a new {@link ImmutableOutgoingSpan ImmutableOutgoingSpan}.
* @return An immutable instance of OutgoingSpan
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableOutgoingSpan build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableOutgoingSpan(
totalNanos,
message,
createUnmodifiableMap(false, false, detail),
locationStackTraceMillis,
type,
dest,
exception);
}
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");
if ((initBits & INIT_BIT_TYPE) != 0) attributes.add("type");
if ((initBits & INIT_BIT_DEST) != 0) attributes.add("dest");
return "Cannot build OutgoingSpan, 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 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) {
ImmutableOutgoingSpan.requireNonNull(k, "key");
ImmutableOutgoingSpan.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) {
ImmutableOutgoingSpan.requireNonNull(k, "key");
ImmutableOutgoingSpan.requireNonNull(v, "value");
}
linkedMap.put(k, v);
}
} else {
linkedMap.putAll(map);
}
return Collections.unmodifiableMap(linkedMap);
}
}
}
}