
org.glowroot.agent.init.ImmutableRawCounterValue Maven / Gradle / Ivy
package org.glowroot.agent.init;
import org.glowroot.agent.shaded.fasterxml.jackson.annotation.JsonCreator;
import org.glowroot.agent.shaded.fasterxml.jackson.annotation.JsonProperty;
import org.glowroot.agent.shaded.google.common.base.MoreObjects;
import org.glowroot.agent.shaded.google.common.base.Preconditions;
import org.glowroot.agent.shaded.google.common.collect.Lists;
import org.glowroot.agent.shaded.google.common.primitives.Doubles;
import org.glowroot.agent.shaded.google.common.primitives.Longs;
import java.util.List;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link GaugeCollector.RawCounterValue}.
*
* Use builder to create immutable instances:
* {@code ImmutableRawCounterValue.builder()}.
* Use static factory method to create immutable instances:
* {@code ImmutableRawCounterValue.of()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "GaugeCollector.RawCounterValue"})
@Immutable
final class ImmutableRawCounterValue implements GaugeCollector.RawCounterValue {
private final double value;
private final long captureTick;
private ImmutableRawCounterValue(double value, long captureTick) {
this.value = value;
this.captureTick = captureTick;
}
/**
* @return value of {@code value} attribute
*/
@JsonProperty
@Override
public double value() {
return value;
}
/**
* @return value of {@code captureTick} attribute
*/
@JsonProperty
@Override
public long captureTick() {
return captureTick;
}
/**
* Copy current immutable object by setting value for {@link GaugeCollector.RawCounterValue#value() value}.
* @param value new value for value
* @return modified copy of the {@code this} object
*/
public final ImmutableRawCounterValue withValue(double value) {
double newValue = value;
return new ImmutableRawCounterValue(newValue, this.captureTick);
}
/**
* Copy current immutable object by setting value for {@link GaugeCollector.RawCounterValue#captureTick() captureTick}.
* Value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for captureTick
* @return modified copy of the {@code this} object
*/
public final ImmutableRawCounterValue withCaptureTick(long value) {
if (this.captureTick == value) return this;
long newValue = value;
return new ImmutableRawCounterValue(this.value, newValue);
}
/**
* This instance is equal to instances of {@code ImmutableRawCounterValue} with equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(@Nullable Object another) {
if (this == another) return true;
return another instanceof ImmutableRawCounterValue
&& equalTo((ImmutableRawCounterValue) another);
}
private boolean equalTo(ImmutableRawCounterValue another) {
return Double.doubleToLongBits(value) == Double.doubleToLongBits(another.value)
&& captureTick == another.captureTick;
}
/**
* Computes hash code from attributes: {@code value}, {@code captureTick}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + Doubles.hashCode(value);
h = h * 17 + Longs.hashCode(captureTick);
return h;
}
/**
* Prints immutable value {@code RawCounterValue...} with attribute values,
* excluding any non-generated and auxiliary attributes.
* @return string representation of value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("RawCounterValue")
.add("value", value)
.add("captureTick", captureTick)
.toString();
}
/**
* Simple representation of this value type suitable Jackson binding
* @deprecated Do not use this type directly, it exists only for Jackson-binding infrastructure
*/
@Deprecated
static final class Json {
@JsonProperty
@Nullable java.lang.Double value;
@JsonProperty
@Nullable Long captureTick;
}
/**
* @param json JSON-bindable data structure
* @return immutable value type
* @deprecated Do not use this method directly, it exists only for Jackson-binding infrastructure
*/
@Deprecated
@JsonCreator
static ImmutableRawCounterValue fromJson(Json json) {
ImmutableRawCounterValue.Builder builder = ImmutableRawCounterValue.builder();
if (json.value != null) {
builder.value(json.value);
}
if (json.captureTick != null) {
builder.captureTick(json.captureTick);
}
return builder.build();
}
/**
* Construct new immutable {@code RawCounterValue} instance.
* @param value value for {@code value}
* @param captureTick value for {@code captureTick}
* @return immutable RawCounterValue instance
*/
public static ImmutableRawCounterValue of(double value, long captureTick) {
return new ImmutableRawCounterValue(value, captureTick);
}
/**
* Creates immutable copy of {@link GaugeCollector.RawCounterValue}.
* Uses accessors to get values to initialize immutable instance.
* If an instance is already immutable, it is returned as is.
* @param instance instance to copy
* @return copied immutable RawCounterValue instance
*/
static ImmutableRawCounterValue copyOf(GaugeCollector.RawCounterValue instance) {
if (instance instanceof ImmutableRawCounterValue) {
return (ImmutableRawCounterValue) instance;
}
return ImmutableRawCounterValue.builder()
.copyFrom(instance)
.build();
}
/**
* Creates builder for {@link org.glowroot.agent.init.ImmutableRawCounterValue ImmutableRawCounterValue}.
* @return new ImmutableRawCounterValue builder
*/
static ImmutableRawCounterValue.Builder builder() {
return new ImmutableRawCounterValue.Builder();
}
/**
* Builds instances of {@link org.glowroot.agent.init.ImmutableRawCounterValue ImmutableRawCounterValue}.
* Initialize attributes and then invoke {@link #build()} method to create
* immutable instance.
*
{@code Builder} is not thread safe and generally should not be stored in field or collection,
* but used immediately to create instances.
*/
@NotThreadSafe
static final class Builder {
private static final long INIT_BIT_VALUE = 0x1L;
private static final long INIT_BIT_CAPTURE_TICK = 0x2L;
private long initBits = 0x3;
private double value;
private long captureTick;
private Builder() {}
/**
* Fill builder with attribute values from provided {@link GaugeCollector.RawCounterValue} instance.
* Regular attribute values will be replaced with ones of an instance.
* Instance's absent optional values will not replace present values.
* @param instance instance to copy values from
* @return {@code this} builder for chained invocation
*/
public final Builder copyFrom(GaugeCollector.RawCounterValue instance) {
Preconditions.checkNotNull(instance);
value(instance.value());
captureTick(instance.captureTick());
return this;
}
/**
* Initializes value for {@link GaugeCollector.RawCounterValue#value() value}.
* @param value value for value
* @return {@code this} builder for chained invocation
*/
public final Builder value(double value) {
this.value = value;
initBits &= ~INIT_BIT_VALUE;
return this;
}
/**
* Initializes value for {@link GaugeCollector.RawCounterValue#captureTick() captureTick}.
* @param captureTick value for captureTick
* @return {@code this} builder for chained invocation
*/
public final Builder captureTick(long captureTick) {
this.captureTick = captureTick;
initBits &= ~INIT_BIT_CAPTURE_TICK;
return this;
}
/**
* Builds new {@link org.glowroot.agent.init.ImmutableRawCounterValue ImmutableRawCounterValue}.
* @return immutable instance of RawCounterValue
* @throws exception {@code java.lang.IllegalStateException} if any required attributes are missing
*/
public ImmutableRawCounterValue build()
throws IllegalStateException {
checkRequiredAttributes(); return new ImmutableRawCounterValue(value, captureTick);
}
private boolean valueIsSet() {
return (initBits & INIT_BIT_VALUE) == 0;
}
private boolean captureTickIsSet() {
return (initBits & INIT_BIT_CAPTURE_TICK) == 0;
}
private void checkRequiredAttributes() throws IllegalStateException {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if (!valueIsSet()) attributes.add("value");
if (!captureTickIsSet()) attributes.add("captureTick");
return "Cannot build RawCounterValue, some of required attributes are not set " + attributes;
}
}
}