
org.glowroot.common.config.ImmutablePluginConfig 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.Optional;
import org.glowroot.agent.shaded.google.common.base.Preconditions;
import org.glowroot.agent.shaded.google.common.collect.ImmutableMap;
import org.glowroot.agent.shaded.google.common.collect.Lists;
import org.glowroot.agent.shaded.google.common.primitives.Booleans;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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 PluginConfig}.
*
* Use builder to create immutable instances:
* {@code ImmutablePluginConfig.builder()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "PluginConfig"})
@Immutable
public final class ImmutablePluginConfig extends PluginConfig {
private final String id;
private final boolean enabled;
private final ImmutableMap properties;
private final ImmutableMap booleanProperties;
private final ImmutableMap stringProperties;
private final ImmutableMap> doubleProperties;
private final String version;
private ImmutablePluginConfig(ImmutablePluginConfig.Builder builder) {
this.id = builder.id;
this.properties = builder.propertiesBuilder.build();
if (builder.enabledIsSet()) {
initShim.enabled(builder.enabled);
}
this.enabled = initShim.enabled();
this.booleanProperties = Preconditions.checkNotNull(super.booleanProperties());
this.stringProperties = Preconditions.checkNotNull(super.stringProperties());
this.doubleProperties = Preconditions.checkNotNull(super.doubleProperties());
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 boolean enabled;
private byte enabledStage;
boolean enabled() {
if (enabledStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (enabledStage == STAGE_UNINITIALIZED) {
enabledStage = STAGE_INITIALIZING;
this.enabled = ImmutablePluginConfig.super.enabled();
enabledStage = STAGE_INITIALIZED;
}
return enabled;
}
boolean enabled(boolean value) {
this.enabled = value;
enabledStage = STAGE_INITIALIZED;
return value;
}
private ImmutableMap booleanProperties;
private byte booleanPropertiesStage;
ImmutableMap booleanProperties() {
if (booleanPropertiesStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (booleanPropertiesStage == STAGE_UNINITIALIZED) {
booleanPropertiesStage = STAGE_INITIALIZING;
this.booleanProperties = Preconditions.checkNotNull(ImmutablePluginConfig.super.booleanProperties());
booleanPropertiesStage = STAGE_INITIALIZED;
}
return booleanProperties;
}
private ImmutableMap stringProperties;
private byte stringPropertiesStage;
ImmutableMap stringProperties() {
if (stringPropertiesStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (stringPropertiesStage == STAGE_UNINITIALIZED) {
stringPropertiesStage = STAGE_INITIALIZING;
this.stringProperties = Preconditions.checkNotNull(ImmutablePluginConfig.super.stringProperties());
stringPropertiesStage = STAGE_INITIALIZED;
}
return stringProperties;
}
private ImmutableMap> doubleProperties;
private byte doublePropertiesStage;
ImmutableMap> doubleProperties() {
if (doublePropertiesStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (doublePropertiesStage == STAGE_UNINITIALIZED) {
doublePropertiesStage = STAGE_INITIALIZING;
this.doubleProperties = Preconditions.checkNotNull(ImmutablePluginConfig.super.doubleProperties());
doublePropertiesStage = STAGE_INITIALIZED;
}
return doubleProperties;
}
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(ImmutablePluginConfig.super.version());
versionStage = STAGE_INITIALIZED;
}
return version;
}
private String formatInitCycleMessage() {
ArrayList attributes = Lists.newArrayList();;
if (enabledStage == STAGE_INITIALIZING) attributes.add("enabled");
if (booleanPropertiesStage == STAGE_INITIALIZING) attributes.add("booleanProperties");
if (stringPropertiesStage == STAGE_INITIALIZING) attributes.add("stringProperties");
if (doublePropertiesStage == STAGE_INITIALIZING) attributes.add("doubleProperties");
if (versionStage == STAGE_INITIALIZING) attributes.add("version");
return "Cannot build PluginConfig, attribute initializers form cycle" + attributes;
}
}
private ImmutablePluginConfig(
String id,
boolean enabled,
ImmutableMap properties) {
this.id = id;
this.enabled = enabled;
this.properties = properties;
this.booleanProperties = Preconditions.checkNotNull(super.booleanProperties());
this.stringProperties = Preconditions.checkNotNull(super.stringProperties());
this.doubleProperties = Preconditions.checkNotNull(super.doubleProperties());
this.version = Preconditions.checkNotNull(super.version());
this.initShim = null;
}
/**
* @return value of {@code id} attribute
*/
@JsonProperty
@Override
public String id() {
return id;
}
/**
* @return value of {@code enabled} attribute
*/
@JsonProperty
@Override
public boolean enabled() {
return initShim != null
? initShim.enabled()
: enabled;
}
/**
* @return value of {@code properties} attribute
*/
@JsonProperty
@Override
public ImmutableMap properties() {
return properties;
}
/**
* @return computed at construction value of {@code booleanProperties} attribute
*/
@JsonProperty
@JsonIgnore
@Override
public ImmutableMap booleanProperties() {
return initShim != null
? initShim.booleanProperties()
: booleanProperties;
}
/**
* @return computed at construction value of {@code stringProperties} attribute
*/
@JsonProperty
@JsonIgnore
@Override
public ImmutableMap stringProperties() {
return initShim != null
? initShim.stringProperties()
: stringProperties;
}
/**
* @return computed at construction value of {@code doubleProperties} attribute
*/
@JsonProperty
@JsonIgnore
@Override
public ImmutableMap> doubleProperties() {
return initShim != null
? initShim.doubleProperties()
: doubleProperties;
}
/**
* @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 PluginConfig#id() id}.
* Shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for id
* @return modified copy of the {@code this} object
*/
public final ImmutablePluginConfig withId(String value) {
if (this.id == value) return this;
String newValue = Preconditions.checkNotNull(value);
return new ImmutablePluginConfig(newValue, this.enabled, this.properties);
}
/**
* Copy current immutable object by setting value for {@link PluginConfig#enabled() enabled}.
* Value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for enabled
* @return modified copy of the {@code this} object
*/
public final ImmutablePluginConfig withEnabled(boolean value) {
if (this.enabled == value) return this;
boolean newValue = value;
return new ImmutablePluginConfig(this.id, newValue, this.properties);
}
/**
* Copy current immutable object by replacing {@link PluginConfig#properties() properties} map with specified map.
* Nulls are not permitted as keys or values.
* Shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param entries to be added to properties map
* @return modified copy of {@code this} object
*/
public final ImmutablePluginConfig withProperties(Map entries) {
if (this.properties == entries) return this;
ImmutableMap newValue = ImmutableMap.copyOf(entries);
return new ImmutablePluginConfig(this.id, this.enabled, newValue);
}
/**
* This instance is equal to instances of {@code ImmutablePluginConfig} 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 ImmutablePluginConfig
&& equalTo((ImmutablePluginConfig) another);
}
private boolean equalTo(ImmutablePluginConfig another) {
return id.equals(another.id)
&& enabled == another.enabled
&& properties.equals(another.properties)
&& booleanProperties.equals(another.booleanProperties)
&& stringProperties.equals(another.stringProperties)
&& doubleProperties.equals(another.doubleProperties)
&& version.equals(another.version);
}
/**
* Computes hash code from attributes: {@code id}, {@code enabled}, {@code properties}, {@code booleanProperties}, {@code stringProperties}, {@code doubleProperties}, {@code version}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + id.hashCode();
h = h * 17 + Booleans.hashCode(enabled);
h = h * 17 + properties.hashCode();
h = h * 17 + booleanProperties.hashCode();
h = h * 17 + stringProperties.hashCode();
h = h * 17 + doubleProperties.hashCode();
h = h * 17 + version.hashCode();
return h;
}
/**
* Prints immutable value {@code PluginConfig...} with attribute values,
* excluding any non-generated and auxiliary attributes.
* @return string representation of value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("PluginConfig")
.add("id", id)
.add("enabled", enabled)
.add("properties", properties)
.add("booleanProperties", booleanProperties)
.add("stringProperties", stringProperties)
.add("doubleProperties", doubleProperties)
.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 String id;
@JsonProperty
@Nullable Boolean enabled;
@JsonProperty
@Nullable Map properties;
@JsonProperty
@JsonIgnore
@Nullable ImmutableMap booleanProperties;
@JsonProperty
@JsonIgnore
@Nullable ImmutableMap stringProperties;
@JsonProperty
@JsonIgnore
@Nullable ImmutableMap> doubleProperties;
@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 ImmutablePluginConfig fromJson(Json json) {
ImmutablePluginConfig.Builder builder = ImmutablePluginConfig.builder();
if (json.id != null) {
builder.id(json.id);
}
if (json.enabled != null) {
builder.enabled(json.enabled);
}
if (json.properties != null) {
builder.putAllProperties(json.properties);
}
return builder.build();
}
/**
* Creates immutable copy of {@link PluginConfig}.
* 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 PluginConfig instance
*/
public static ImmutablePluginConfig copyOf(PluginConfig instance) {
if (instance instanceof ImmutablePluginConfig) {
return (ImmutablePluginConfig) instance;
}
return ImmutablePluginConfig.builder()
.copyFrom(instance)
.build();
}
/**
* Creates builder for {@link org.glowroot.common.config.ImmutablePluginConfig ImmutablePluginConfig}.
* @return new ImmutablePluginConfig builder
*/
public static ImmutablePluginConfig.Builder builder() {
return new ImmutablePluginConfig.Builder();
}
/**
* Builds instances of {@link org.glowroot.common.config.ImmutablePluginConfig ImmutablePluginConfig}.
* 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_ID = 0x1L;
private static final long OPT_BIT_ENABLED = 0x1L;
private long initBits = 0x1;
private long optBits;
private @Nullable String id;
private boolean enabled;
private ImmutableMap.Builder propertiesBuilder = ImmutableMap.builder();
private Builder() {}
/**
* Fill builder with attribute values from provided {@link PluginConfig} instance.
* Regular attribute values will be replaced with ones of an instance.
* Instance's absent optional values will not replace present values.
* Collection elements and entries will be added, not replaced.
* @param instance instance to copy values from
* @return {@code this} builder for chained invocation
*/
public final Builder copyFrom(PluginConfig instance) {
Preconditions.checkNotNull(instance);
id(instance.id());
enabled(instance.enabled());
putAllProperties(instance.properties());
return this;
}
/**
* Initializes value for {@link PluginConfig#id() id}.
* @param id value for id
* @return {@code this} builder for chained invocation
*/
public final Builder id(String id) {
this.id = Preconditions.checkNotNull(id);
initBits &= ~INIT_BIT_ID;
return this;
}
/**
* Initializes value for {@link PluginConfig#enabled() enabled}.
* If not set, this attribute will have default value returned by initializer of {@link PluginConfig#enabled() enabled}.
* @param enabled value for enabled
* @return {@code this} builder for chained invocation
*/
public final Builder enabled(boolean enabled) {
this.enabled = enabled;
optBits |= OPT_BIT_ENABLED;
return this;
}
/**
* Put one entry to {@link PluginConfig#properties() properties} map.
* @param key the key in properties map
* @param value the associated value in properties map
* @return {@code this} builder for chained invocation
*/
public final Builder putProperties(String key, PropertyValue value) {
propertiesBuilder.put(key, value);
return this;
}
/**
* Put one entry to {@link PluginConfig#properties() properties} map. Nulls are not permitted
* @param entry the key and value entry
* @return {@code this} builder for chained invocation
*/
public final Builder putProperties(Map.Entry entry) {
propertiesBuilder.put(entry);
return this;
}
/**
* Sets or replaces all mappings from specified map as entries for {@link PluginConfig#properties() properties} map. Nulls are not permitted
* @param entries to be added to properties map
* @return {@code this} builder for chained invocation
*/
public final Builder properties(Map entries) {
propertiesBuilder = ImmutableMap.builder();
return putAllProperties(entries);
}
/**
* Put all mappings from specified map as entries to {@link PluginConfig#properties() properties} map. Nulls are not permitted
* @param entries to be added to properties map
* @return {@code this} builder for chained invocation
*/
public final Builder putAllProperties(Map entries) {
propertiesBuilder.putAll(entries);
return this;
}
/**
* Builds new {@link org.glowroot.common.config.ImmutablePluginConfig ImmutablePluginConfig}.
* @return immutable instance of PluginConfig
* @throws exception {@code java.lang.IllegalStateException} if any required attributes are missing
*/
public ImmutablePluginConfig build()
throws IllegalStateException {
checkRequiredAttributes(); return new ImmutablePluginConfig(this);
}
private boolean enabledIsSet() {
return (optBits & OPT_BIT_ENABLED) != 0;
}
private boolean idIsSet() {
return (initBits & INIT_BIT_ID) == 0;
}
private void checkRequiredAttributes() throws IllegalStateException {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if (!idIsSet()) attributes.add("id");
return "Cannot build PluginConfig, some of required attributes are not set " + attributes;
}
}
}