
org.glowroot.common.config.ImmutableTransactionConfig Maven / Gradle / Ivy
package org.glowroot.common.config;
import org.glowroot.agent.shaded.fasterxml.jackson.annotation.JsonCreator;
import org.glowroot.agent.shaded.fasterxml.jackson.annotation.JsonIgnore;
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 java.util.ArrayList;
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 TransactionConfig}.
*
* Use builder to create immutable instances:
* {@code ImmutableTransactionConfig.builder()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "TransactionConfig"})
@Immutable
public final class ImmutableTransactionConfig extends TransactionConfig {
private final int profilingIntervalMillis;
private final int slowThresholdMillis;
private final String version;
private ImmutableTransactionConfig(ImmutableTransactionConfig.Builder builder) {
if (builder.profilingIntervalMillisIsSet()) {
initShim.profilingIntervalMillis(builder.profilingIntervalMillis);
}
if (builder.slowThresholdMillisIsSet()) {
initShim.slowThresholdMillis(builder.slowThresholdMillis);
}
this.profilingIntervalMillis = initShim.profilingIntervalMillis();
this.slowThresholdMillis = initShim.slowThresholdMillis();
this.version = Preconditions.checkNotNull(super.version());
this.initShim = null;
}
private static final int STAGE_INITIALIZING = -1;
private static final int STAGE_UNINITIALIZED = 0;
private static final int STAGE_INITIALIZED = 1;
private volatile InitShim initShim = new InitShim();
private final class InitShim {
private int profilingIntervalMillis;
private byte profilingIntervalMillisStage;
int profilingIntervalMillis() {
if (profilingIntervalMillisStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (profilingIntervalMillisStage == STAGE_UNINITIALIZED) {
profilingIntervalMillisStage = STAGE_INITIALIZING;
this.profilingIntervalMillis = ImmutableTransactionConfig.super.profilingIntervalMillis();
profilingIntervalMillisStage = STAGE_INITIALIZED;
}
return profilingIntervalMillis;
}
int profilingIntervalMillis(int value) {
this.profilingIntervalMillis = value;
profilingIntervalMillisStage = STAGE_INITIALIZED;
return value;
}
private int slowThresholdMillis;
private byte slowThresholdMillisStage;
int slowThresholdMillis() {
if (slowThresholdMillisStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (slowThresholdMillisStage == STAGE_UNINITIALIZED) {
slowThresholdMillisStage = STAGE_INITIALIZING;
this.slowThresholdMillis = ImmutableTransactionConfig.super.slowThresholdMillis();
slowThresholdMillisStage = STAGE_INITIALIZED;
}
return slowThresholdMillis;
}
int slowThresholdMillis(int value) {
this.slowThresholdMillis = value;
slowThresholdMillisStage = STAGE_INITIALIZED;
return value;
}
private String version;
private byte versionStage;
String version() {
if (versionStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (versionStage == STAGE_UNINITIALIZED) {
versionStage = STAGE_INITIALIZING;
this.version = Preconditions.checkNotNull(ImmutableTransactionConfig.super.version());
versionStage = STAGE_INITIALIZED;
}
return version;
}
private String formatInitCycleMessage() {
ArrayList attributes = Lists.newArrayList();;
if (profilingIntervalMillisStage == STAGE_INITIALIZING) attributes.add("profilingIntervalMillis");
if (slowThresholdMillisStage == STAGE_INITIALIZING) attributes.add("slowThresholdMillis");
if (versionStage == STAGE_INITIALIZING) attributes.add("version");
return "Cannot build TransactionConfig, attribute initializers form cycle" + attributes;
}
}
private ImmutableTransactionConfig(int profilingIntervalMillis, int slowThresholdMillis) {
this.profilingIntervalMillis = profilingIntervalMillis;
this.slowThresholdMillis = slowThresholdMillis;
this.version = Preconditions.checkNotNull(super.version());
this.initShim = null;
}
/**
* @return value of {@code profilingIntervalMillis} attribute
*/
@JsonProperty
@Override
public int profilingIntervalMillis() {
return initShim != null
? initShim.profilingIntervalMillis()
: profilingIntervalMillis;
}
/**
* @return value of {@code slowThresholdMillis} attribute
*/
@JsonProperty
@Override
public int slowThresholdMillis() {
return initShim != null
? initShim.slowThresholdMillis()
: slowThresholdMillis;
}
/**
* @return computed at construction value of {@code version} attribute
*/
@JsonProperty
@JsonIgnore
@Override
public String version() {
return initShim != null
? initShim.version()
: version;
}
/**
* Copy current immutable object by setting value for {@link TransactionConfig#profilingIntervalMillis() profilingIntervalMillis}.
* Value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for profilingIntervalMillis
* @return modified copy of the {@code this} object
*/
public final ImmutableTransactionConfig withProfilingIntervalMillis(int value) {
if (this.profilingIntervalMillis == value) return this;
int newValue = value;
return new ImmutableTransactionConfig(newValue, this.slowThresholdMillis);
}
/**
* Copy current immutable object by setting value for {@link TransactionConfig#slowThresholdMillis() slowThresholdMillis}.
* Value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for slowThresholdMillis
* @return modified copy of the {@code this} object
*/
public final ImmutableTransactionConfig withSlowThresholdMillis(int value) {
if (this.slowThresholdMillis == value) return this;
int newValue = value;
return new ImmutableTransactionConfig(this.profilingIntervalMillis, newValue);
}
/**
* This instance is equal to instances of {@code ImmutableTransactionConfig} 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 ImmutableTransactionConfig
&& equalTo((ImmutableTransactionConfig) another);
}
private boolean equalTo(ImmutableTransactionConfig another) {
return profilingIntervalMillis == another.profilingIntervalMillis
&& slowThresholdMillis == another.slowThresholdMillis
&& version.equals(another.version);
}
/**
* Computes hash code from attributes: {@code profilingIntervalMillis}, {@code slowThresholdMillis}, {@code version}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + profilingIntervalMillis;
h = h * 17 + slowThresholdMillis;
h = h * 17 + version.hashCode();
return h;
}
/**
* Prints immutable value {@code TransactionConfig...} with attribute values,
* excluding any non-generated and auxiliary attributes.
* @return string representation of value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("TransactionConfig")
.add("profilingIntervalMillis", profilingIntervalMillis)
.add("slowThresholdMillis", slowThresholdMillis)
.add("version", version)
.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 Integer profilingIntervalMillis;
@JsonProperty
@Nullable Integer slowThresholdMillis;
@JsonProperty
@JsonIgnore
@Nullable String version;
}
/**
* @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 ImmutableTransactionConfig fromJson(Json json) {
ImmutableTransactionConfig.Builder builder = ImmutableTransactionConfig.builder();
if (json.profilingIntervalMillis != null) {
builder.profilingIntervalMillis(json.profilingIntervalMillis);
}
if (json.slowThresholdMillis != null) {
builder.slowThresholdMillis(json.slowThresholdMillis);
}
return builder.build();
}
/**
* Creates immutable copy of {@link TransactionConfig}.
* 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 TransactionConfig instance
*/
public static ImmutableTransactionConfig copyOf(TransactionConfig instance) {
if (instance instanceof ImmutableTransactionConfig) {
return (ImmutableTransactionConfig) instance;
}
return ImmutableTransactionConfig.builder()
.copyFrom(instance)
.build();
}
/**
* Creates builder for {@link org.glowroot.common.config.ImmutableTransactionConfig ImmutableTransactionConfig}.
* @return new ImmutableTransactionConfig builder
*/
public static ImmutableTransactionConfig.Builder builder() {
return new ImmutableTransactionConfig.Builder();
}
/**
* Builds instances of {@link org.glowroot.common.config.ImmutableTransactionConfig ImmutableTransactionConfig}.
* 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 OPT_BIT_PROFILING_INTERVAL_MILLIS = 0x1L;
private static final long OPT_BIT_SLOW_THRESHOLD_MILLIS = 0x2L;
private long optBits;
private int profilingIntervalMillis;
private int slowThresholdMillis;
private Builder() {}
/**
* Fill builder with attribute values from provided {@link TransactionConfig} 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(TransactionConfig instance) {
Preconditions.checkNotNull(instance);
profilingIntervalMillis(instance.profilingIntervalMillis());
slowThresholdMillis(instance.slowThresholdMillis());
return this;
}
/**
* Initializes value for {@link TransactionConfig#profilingIntervalMillis() profilingIntervalMillis}.
*
If not set, this attribute will have default value returned by initializer of {@link TransactionConfig#profilingIntervalMillis() profilingIntervalMillis}.
* @param profilingIntervalMillis value for profilingIntervalMillis
* @return {@code this} builder for chained invocation
*/
public final Builder profilingIntervalMillis(int profilingIntervalMillis) {
this.profilingIntervalMillis = profilingIntervalMillis;
optBits |= OPT_BIT_PROFILING_INTERVAL_MILLIS;
return this;
}
/**
* Initializes value for {@link TransactionConfig#slowThresholdMillis() slowThresholdMillis}.
*
If not set, this attribute will have default value returned by initializer of {@link TransactionConfig#slowThresholdMillis() slowThresholdMillis}.
* @param slowThresholdMillis value for slowThresholdMillis
* @return {@code this} builder for chained invocation
*/
public final Builder slowThresholdMillis(int slowThresholdMillis) {
this.slowThresholdMillis = slowThresholdMillis;
optBits |= OPT_BIT_SLOW_THRESHOLD_MILLIS;
return this;
}
/**
* Builds new {@link org.glowroot.common.config.ImmutableTransactionConfig ImmutableTransactionConfig}.
* @return immutable instance of TransactionConfig
* @throws exception {@code java.lang.IllegalStateException} if any required attributes are missing
*/
public ImmutableTransactionConfig build()
throws IllegalStateException {
return new ImmutableTransactionConfig(this);
}
private boolean profilingIntervalMillisIsSet() {
return (optBits & OPT_BIT_PROFILING_INTERVAL_MILLIS) != 0;
}
private boolean slowThresholdMillisIsSet() {
return (optBits & OPT_BIT_SLOW_THRESHOLD_MILLIS) != 0;
}
}
}