org.glowroot.instrumentation.test.harness.ImmutableTimer 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.List;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link IncomingSpan.Timer}.
*
* Use the builder to create immutable instances:
* {@code ImmutableTimer.builder()}.
*/
@Generated(from = "IncomingSpan.Timer", generator = "Immutables")
@SuppressWarnings({"all"})
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
public final class ImmutableTimer implements IncomingSpan.Timer {
private final String name;
private final boolean extended;
private final long totalNanos;
private final long count;
private final List childTimers;
private ImmutableTimer(
String name,
boolean extended,
long totalNanos,
long count,
List childTimers) {
this.name = name;
this.extended = extended;
this.totalNanos = totalNanos;
this.count = count;
this.childTimers = childTimers;
}
/**
* @return The value of the {@code name} attribute
*/
@Override
public String name() {
return name;
}
/**
* @return The value of the {@code extended} attribute
*/
@Override
public boolean extended() {
return extended;
}
/**
* @return The value of the {@code totalNanos} attribute
*/
@Override
public long totalNanos() {
return totalNanos;
}
/**
* @return The value of the {@code count} attribute
*/
@Override
public long count() {
return count;
}
/**
* @return The value of the {@code childTimers} attribute
*/
@Override
public List childTimers() {
return childTimers;
}
/**
* Copy the current immutable object by setting a value for the {@link IncomingSpan.Timer#name() name} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for name
* @return A modified copy of the {@code this} object
*/
public final ImmutableTimer withName(String value) {
String newValue = ImmutableTimer.requireNonNull(value, "name");
if (this.name.equals(newValue)) return this;
return new ImmutableTimer(newValue, this.extended, this.totalNanos, this.count, this.childTimers);
}
/**
* Copy the current immutable object by setting a value for the {@link IncomingSpan.Timer#extended() extended} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for extended
* @return A modified copy of the {@code this} object
*/
public final ImmutableTimer withExtended(boolean value) {
if (this.extended == value) return this;
return new ImmutableTimer(this.name, value, this.totalNanos, this.count, this.childTimers);
}
/**
* Copy the current immutable object by setting a value for the {@link IncomingSpan.Timer#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 ImmutableTimer withTotalNanos(long value) {
if (this.totalNanos == value) return this;
return new ImmutableTimer(this.name, this.extended, value, this.count, this.childTimers);
}
/**
* Copy the current immutable object by setting a value for the {@link IncomingSpan.Timer#count() count} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for count
* @return A modified copy of the {@code this} object
*/
public final ImmutableTimer withCount(long value) {
if (this.count == value) return this;
return new ImmutableTimer(this.name, this.extended, this.totalNanos, value, this.childTimers);
}
/**
* Copy the current immutable object with elements that replace the content of {@link IncomingSpan.Timer#childTimers() childTimers}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableTimer withChildTimers(IncomingSpan.Timer... elements) {
List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new ImmutableTimer(this.name, this.extended, this.totalNanos, this.count, newValue);
}
/**
* Copy the current immutable object with elements that replace the content of {@link IncomingSpan.Timer#childTimers() childTimers}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of childTimers elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableTimer withChildTimers(Iterable extends IncomingSpan.Timer> elements) {
if (this.childTimers == elements) return this;
List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new ImmutableTimer(this.name, this.extended, this.totalNanos, this.count, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableTimer} 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 ImmutableTimer
&& equalTo((ImmutableTimer) another);
}
private boolean equalTo(ImmutableTimer another) {
return name.equals(another.name)
&& extended == another.extended
&& totalNanos == another.totalNanos
&& count == another.count
&& childTimers.equals(another.childTimers);
}
/**
* Computes a hash code from attributes: {@code name}, {@code extended}, {@code totalNanos}, {@code count}, {@code childTimers}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + name.hashCode();
h += (h << 5) + (extended ? 1231 : 1237);
h += (h << 5) + (int) (totalNanos ^ (totalNanos >>> 32));
h += (h << 5) + (int) (count ^ (count >>> 32));
h += (h << 5) + childTimers.hashCode();
return h;
}
/**
* Prints the immutable value {@code Timer} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "Timer{"
+ "name=" + name
+ ", extended=" + extended
+ ", totalNanos=" + totalNanos
+ ", count=" + count
+ ", childTimers=" + childTimers
+ "}";
}
/**
* Creates an immutable copy of a {@link IncomingSpan.Timer} 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 Timer instance
*/
public static ImmutableTimer copyOf(IncomingSpan.Timer instance) {
if (instance instanceof ImmutableTimer) {
return (ImmutableTimer) instance;
}
return ImmutableTimer.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableTimer ImmutableTimer}.
*
* ImmutableTimer.builder()
* .name(String) // required {@link IncomingSpan.Timer#name() name}
* .extended(boolean) // required {@link IncomingSpan.Timer#extended() extended}
* .totalNanos(long) // required {@link IncomingSpan.Timer#totalNanos() totalNanos}
* .count(long) // required {@link IncomingSpan.Timer#count() count}
* .addChildTimers|addAllChildTimers(org.glowroot.instrumentation.test.harness.IncomingSpan.Timer) // {@link IncomingSpan.Timer#childTimers() childTimers} elements
* .build();
*
* @return A new ImmutableTimer builder
*/
public static ImmutableTimer.Builder builder() {
return new ImmutableTimer.Builder();
}
/**
* Builds instances of type {@link ImmutableTimer ImmutableTimer}.
* 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 = "IncomingSpan.Timer", generator = "Immutables")
public static final class Builder {
private static final long INIT_BIT_NAME = 0x1L;
private static final long INIT_BIT_EXTENDED = 0x2L;
private static final long INIT_BIT_TOTAL_NANOS = 0x4L;
private static final long INIT_BIT_COUNT = 0x8L;
private long initBits = 0xfL;
private String name;
private boolean extended;
private long totalNanos;
private long count;
private List childTimers = new ArrayList();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Timer} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* Collection elements and entries will be added, not replaced.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(IncomingSpan.Timer instance) {
ImmutableTimer.requireNonNull(instance, "instance");
name(instance.name());
extended(instance.extended());
totalNanos(instance.totalNanos());
count(instance.count());
addAllChildTimers(instance.childTimers());
return this;
}
/**
* Initializes the value for the {@link IncomingSpan.Timer#name() name} attribute.
* @param name The value for name
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder name(String name) {
this.name = ImmutableTimer.requireNonNull(name, "name");
initBits &= ~INIT_BIT_NAME;
return this;
}
/**
* Initializes the value for the {@link IncomingSpan.Timer#extended() extended} attribute.
* @param extended The value for extended
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder extended(boolean extended) {
this.extended = extended;
initBits &= ~INIT_BIT_EXTENDED;
return this;
}
/**
* Initializes the value for the {@link IncomingSpan.Timer#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 IncomingSpan.Timer#count() count} attribute.
* @param count The value for count
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder count(long count) {
this.count = count;
initBits &= ~INIT_BIT_COUNT;
return this;
}
/**
* Adds one element to {@link IncomingSpan.Timer#childTimers() childTimers} list.
* @param element A childTimers element
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addChildTimers(IncomingSpan.Timer element) {
this.childTimers.add(ImmutableTimer.requireNonNull(element, "childTimers element"));
return this;
}
/**
* Adds elements to {@link IncomingSpan.Timer#childTimers() childTimers} list.
* @param elements An array of childTimers elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addChildTimers(IncomingSpan.Timer... elements) {
for (IncomingSpan.Timer element : elements) {
this.childTimers.add(ImmutableTimer.requireNonNull(element, "childTimers element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link IncomingSpan.Timer#childTimers() childTimers} list.
* @param elements An iterable of childTimers elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder childTimers(Iterable extends IncomingSpan.Timer> elements) {
this.childTimers.clear();
return addAllChildTimers(elements);
}
/**
* Adds elements to {@link IncomingSpan.Timer#childTimers() childTimers} list.
* @param elements An iterable of childTimers elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addAllChildTimers(Iterable extends IncomingSpan.Timer> elements) {
for (IncomingSpan.Timer element : elements) {
this.childTimers.add(ImmutableTimer.requireNonNull(element, "childTimers element"));
}
return this;
}
/**
* Builds a new {@link ImmutableTimer ImmutableTimer}.
* @return An immutable instance of Timer
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableTimer build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableTimer(name, extended, totalNanos, count, createUnmodifiableList(true, childTimers));
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList();
if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name");
if ((initBits & INIT_BIT_EXTENDED) != 0) attributes.add("extended");
if ((initBits & INIT_BIT_TOTAL_NANOS) != 0) attributes.add("totalNanos");
if ((initBits & INIT_BIT_COUNT) != 0) attributes.add("count");
return "Cannot build Timer, 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 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) ImmutableTimer.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);
}
}
}
}