
org.glowroot.storage.repo.ImmutableGauge Maven / Gradle / Ivy
package org.glowroot.storage.repo;
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.Booleans;
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 GaugeValueRepository.Gauge}.
*
* Use builder to create immutable instances:
* {@code ImmutableGauge.builder()}.
* Use static factory method to create immutable instances:
* {@code ImmutableGauge.of()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "GaugeValueRepository.Gauge"})
@Immutable
public final class ImmutableGauge implements GaugeValueRepository.Gauge {
private final String name;
private final String display;
private final boolean counter;
private ImmutableGauge(String name, String display, boolean counter) {
this.name = Preconditions.checkNotNull(name);
this.display = Preconditions.checkNotNull(display);
this.counter = counter;
}
private ImmutableGauge(ImmutableGauge original, String name, String display, boolean counter) {
this.name = name;
this.display = display;
this.counter = counter;
}
/**
* @return value of {@code name} attribute
*/
@JsonProperty
@Override
public String name() {
return name;
}
/**
* @return value of {@code display} attribute
*/
@JsonProperty
@Override
public String display() {
return display;
}
/**
* @return value of {@code counter} attribute
*/
@JsonProperty
@Override
public boolean counter() {
return counter;
}
/**
* Copy current immutable object by setting value for {@link GaugeValueRepository.Gauge#name() name}.
* Shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for name
* @return modified copy of the {@code this} object
*/
public final ImmutableGauge withName(String value) {
if (this.name == value) return this;
String newValue = Preconditions.checkNotNull(value);
return new ImmutableGauge(this, newValue, this.display, this.counter);
}
/**
* Copy current immutable object by setting value for {@link GaugeValueRepository.Gauge#display() display}.
* Shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for display
* @return modified copy of the {@code this} object
*/
public final ImmutableGauge withDisplay(String value) {
if (this.display == value) return this;
String newValue = Preconditions.checkNotNull(value);
return new ImmutableGauge(this, this.name, newValue, this.counter);
}
/**
* Copy current immutable object by setting value for {@link GaugeValueRepository.Gauge#counter() counter}.
* Value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for counter
* @return modified copy of the {@code this} object
*/
public final ImmutableGauge withCounter(boolean value) {
if (this.counter == value) return this;
boolean newValue = value;
return new ImmutableGauge(this, this.name, this.display, newValue);
}
/**
* This instance is equal to instances of {@code ImmutableGauge} 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 ImmutableGauge
&& equalTo((ImmutableGauge) another);
}
private boolean equalTo(ImmutableGauge another) {
return name.equals(another.name)
&& display.equals(another.display)
&& counter == another.counter;
}
/**
* Computes hash code from attributes: {@code name}, {@code display}, {@code counter}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + name.hashCode();
h = h * 17 + display.hashCode();
h = h * 17 + Booleans.hashCode(counter);
return h;
}
/**
* Prints immutable value {@code Gauge...} with attribute values,
* excluding any non-generated and auxiliary attributes.
* @return string representation of value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("Gauge")
.add("name", name)
.add("display", display)
.add("counter", counter)
.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 String name;
@JsonProperty
@Nullable String display;
@JsonProperty
@Nullable Boolean counter;
}
/**
* @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 ImmutableGauge fromJson(Json json) {
ImmutableGauge.Builder builder = ImmutableGauge.builder();
if (json.name != null) {
builder.name(json.name);
}
if (json.display != null) {
builder.display(json.display);
}
if (json.counter != null) {
builder.counter(json.counter);
}
return builder.build();
}
/**
* Construct new immutable {@code Gauge} instance.
* @param name value for {@code name}
* @param display value for {@code display}
* @param counter value for {@code counter}
* @return immutable Gauge instance
*/
public static ImmutableGauge of(String name, String display, boolean counter) {
return new ImmutableGauge(name, display, counter);
}
/**
* Creates immutable copy of {@link GaugeValueRepository.Gauge}.
* 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 Gauge instance
*/
public static ImmutableGauge copyOf(GaugeValueRepository.Gauge instance) {
if (instance instanceof ImmutableGauge) {
return (ImmutableGauge) instance;
}
return ImmutableGauge.builder()
.copyFrom(instance)
.build();
}
/**
* Creates builder for {@link org.glowroot.storage.repo.ImmutableGauge ImmutableGauge}.
* @return new ImmutableGauge builder
*/
public static ImmutableGauge.Builder builder() {
return new ImmutableGauge.Builder();
}
/**
* Builds instances of {@link org.glowroot.storage.repo.ImmutableGauge ImmutableGauge}.
* 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
public static final class Builder {
private static final long INIT_BIT_NAME = 0x1L;
private static final long INIT_BIT_DISPLAY = 0x2L;
private static final long INIT_BIT_COUNTER = 0x4L;
private long initBits = 0x7;
private @Nullable String name;
private @Nullable String display;
private boolean counter;
private Builder() {}
/**
* Fill builder with attribute values from provided {@link GaugeValueRepository.Gauge} 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(GaugeValueRepository.Gauge instance) {
Preconditions.checkNotNull(instance);
name(instance.name());
display(instance.display());
counter(instance.counter());
return this;
}
/**
* Initializes value for {@link GaugeValueRepository.Gauge#name() name}.
* @param name value for name
* @return {@code this} builder for chained invocation
*/
public final Builder name(String name) {
this.name = Preconditions.checkNotNull(name);
initBits &= ~INIT_BIT_NAME;
return this;
}
/**
* Initializes value for {@link GaugeValueRepository.Gauge#display() display}.
* @param display value for display
* @return {@code this} builder for chained invocation
*/
public final Builder display(String display) {
this.display = Preconditions.checkNotNull(display);
initBits &= ~INIT_BIT_DISPLAY;
return this;
}
/**
* Initializes value for {@link GaugeValueRepository.Gauge#counter() counter}.
* @param counter value for counter
* @return {@code this} builder for chained invocation
*/
public final Builder counter(boolean counter) {
this.counter = counter;
initBits &= ~INIT_BIT_COUNTER;
return this;
}
/**
* Builds new {@link org.glowroot.storage.repo.ImmutableGauge ImmutableGauge}.
* @return immutable instance of Gauge
* @throws exception {@code java.lang.IllegalStateException} if any required attributes are missing
*/
public ImmutableGauge build()
throws IllegalStateException {
checkRequiredAttributes(); return new ImmutableGauge(null, name, display, counter);
}
private boolean nameIsSet() {
return (initBits & INIT_BIT_NAME) == 0;
}
private boolean displayIsSet() {
return (initBits & INIT_BIT_DISPLAY) == 0;
}
private boolean counterIsSet() {
return (initBits & INIT_BIT_COUNTER) == 0;
}
private void checkRequiredAttributes() throws IllegalStateException {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if (!nameIsSet()) attributes.add("name");
if (!displayIsSet()) attributes.add("display");
if (!counterIsSet()) attributes.add("counter");
return "Cannot build Gauge, some of required attributes are not set " + attributes;
}
}
}