
org.glowroot.agent.config.ImmutablePluginConfig Maven / Gradle / Ivy
Show all versions of glowroot-agent-it-harness Show documentation
package org.glowroot.agent.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 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 the builder to create immutable instances:
* {@code ImmutablePluginConfig.builder()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "PluginConfig"})
@Immutable
public final class ImmutablePluginConfig extends PluginConfig {
private final PluginDescriptor pluginDescriptor;
private final String id;
private final ImmutableMap properties;
private final ImmutableMap booleanProperties;
private final ImmutableMap stringProperties;
private final ImmutableMap> doubleProperties;
private ImmutablePluginConfig(
PluginDescriptor pluginDescriptor,
ImmutableMap properties) {
this.pluginDescriptor = pluginDescriptor;
this.properties = properties;
this.id = initShim.id();
this.booleanProperties = initShim.booleanProperties();
this.stringProperties = initShim.stringProperties();
this.doubleProperties = initShim.doubleProperties();
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 String id;
private byte idStage;
String id() {
if (idStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (idStage == STAGE_UNINITIALIZED) {
idStage = STAGE_INITIALIZING;
this.id = Preconditions.checkNotNull(ImmutablePluginConfig.super.id(), "id");
idStage = STAGE_INITIALIZED;
}
return id;
}
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(), "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(), "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(), "doubleProperties");
doublePropertiesStage = STAGE_INITIALIZED;
}
return doubleProperties;
}
private String formatInitCycleMessage() {
ArrayList attributes = Lists.newArrayList();
if (idStage == STAGE_INITIALIZING) attributes.add("id");
if (booleanPropertiesStage == STAGE_INITIALIZING) attributes.add("booleanProperties");
if (stringPropertiesStage == STAGE_INITIALIZING) attributes.add("stringProperties");
if (doublePropertiesStage == STAGE_INITIALIZING) attributes.add("doubleProperties");
return "Cannot build PluginConfig, attribute initializers form cycle" + attributes;
}
}
/**
* @return The value of the {@code pluginDescriptor} attribute
*/
@JsonProperty
@JsonIgnore
@Override
public PluginDescriptor pluginDescriptor() {
return pluginDescriptor;
}
/**
* @return The computed-at-construction value of the {@code id} attribute
*/
@JsonProperty
@Override
public String id() {
return initShim != null
? initShim.id()
: id;
}
/**
* @return The value of the {@code properties} attribute
*/
@JsonProperty
@Override
public ImmutableMap properties() {
return properties;
}
/**
* @return The computed-at-construction value of the {@code booleanProperties} attribute
*/
@JsonProperty
@JsonIgnore
@Override
ImmutableMap booleanProperties() {
return initShim != null
? initShim.booleanProperties()
: booleanProperties;
}
/**
* @return The computed-at-construction value of the {@code stringProperties} attribute
*/
@JsonProperty
@JsonIgnore
@Override
ImmutableMap stringProperties() {
return initShim != null
? initShim.stringProperties()
: stringProperties;
}
/**
* @return The computed-at-construction value of the {@code doubleProperties} attribute
*/
@JsonProperty
@JsonIgnore
@Override
ImmutableMap> doubleProperties() {
return initShim != null
? initShim.doubleProperties()
: doubleProperties;
}
/**
* Copy the current immutable object by setting a value for the {@link PluginConfig#pluginDescriptor() pluginDescriptor} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for pluginDescriptor
* @return A modified copy of the {@code this} object
*/
public final ImmutablePluginConfig withPluginDescriptor(PluginDescriptor value) {
if (this.pluginDescriptor == value) return this;
return new ImmutablePluginConfig(Preconditions.checkNotNull(value, "pluginDescriptor"), this.properties);
}
/**
* Copy the current immutable object by replacing the {@link PluginConfig#properties() properties} map with the specified map.
* Nulls are not permitted as keys or values.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param entries The entries to be added to the properties map
* @return A modified copy of {@code this} object
*/
public final ImmutablePluginConfig withProperties(Map entries) {
if (this.properties == entries) return this;
ImmutableMap value = ImmutableMap.copyOf(entries);
return new ImmutablePluginConfig(this.pluginDescriptor, value);
}
/**
* This instance is equal to all instances of {@code ImmutablePluginConfig} that have 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 pluginDescriptor.equals(another.pluginDescriptor)
&& id.equals(another.id)
&& properties.equals(another.properties)
&& booleanProperties.equals(another.booleanProperties)
&& stringProperties.equals(another.stringProperties)
&& doubleProperties.equals(another.doubleProperties);
}
/**
* Computes a hash code from attributes: {@code pluginDescriptor}, {@code id}, {@code properties}, {@code booleanProperties}, {@code stringProperties}, {@code doubleProperties}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + pluginDescriptor.hashCode();
h = h * 17 + id.hashCode();
h = h * 17 + properties.hashCode();
h = h * 17 + booleanProperties.hashCode();
h = h * 17 + stringProperties.hashCode();
h = h * 17 + doubleProperties.hashCode();
return h;
}
/**
* Prints the immutable value {@code PluginConfig...} with all non-generated
* and non-auxiliary attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("PluginConfig")
.add("pluginDescriptor", pluginDescriptor)
.add("id", id)
.add("properties", properties)
.add("booleanProperties", booleanProperties)
.add("stringProperties", stringProperties)
.add("doubleProperties", doubleProperties)
.toString();
}
/**
* Utility type used to correctly read immutable object from JSON representation.
* @deprecated Do not use this type directly, it exists only for the Jackson-binding infrastructure
*/
@Deprecated
static final class Json extends PluginConfig {
@Nullable PluginDescriptor pluginDescriptor;
@Nullable Map properties;
@JsonProperty
@JsonIgnore
public void setPluginDescriptor(PluginDescriptor pluginDescriptor) {
this.pluginDescriptor = pluginDescriptor;
}
@JsonProperty
public void setProperties(Map properties) {
this.properties = properties;
}
@Override
public PluginDescriptor pluginDescriptor() { throw new UnsupportedOperationException(); }
@Override
public String id() { throw new UnsupportedOperationException(); }
@Override
public Map properties() { throw new UnsupportedOperationException(); }
@Override
ImmutableMap booleanProperties() { throw new UnsupportedOperationException(); }
@Override
ImmutableMap stringProperties() { throw new UnsupportedOperationException(); }
@Override
ImmutableMap> doubleProperties() { throw new UnsupportedOperationException(); }
}
/**
* @param json A JSON-bindable data structure
* @return An immutable value type
* @deprecated Do not use this method directly, it exists only for the Jackson-binding infrastructure
*/
@Deprecated
@JsonCreator
static ImmutablePluginConfig fromJson(Json json) {
ImmutablePluginConfig.Builder builder = ImmutablePluginConfig.builder();
if (json.pluginDescriptor != null) {
builder.pluginDescriptor(json.pluginDescriptor);
}
if (json.properties != null) {
builder.putAllProperties(json.properties);
}
return builder.build();
}
/**
* Creates an immutable copy of a {@link PluginConfig} 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 PluginConfig instance
*/
public static ImmutablePluginConfig copyOf(PluginConfig instance) {
if (instance instanceof ImmutablePluginConfig) {
return (ImmutablePluginConfig) instance;
}
return ImmutablePluginConfig.builder()
.copyFrom(instance)
.build();
}
/**
* Creates a builder for {@link ImmutablePluginConfig ImmutablePluginConfig}.
* @return A new ImmutablePluginConfig builder
*/
public static ImmutablePluginConfig.Builder builder() {
return new ImmutablePluginConfig.Builder();
}
/**
* Builds instances of type {@link ImmutablePluginConfig ImmutablePluginConfig}.
* 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.
*/
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_PLUGIN_DESCRIPTOR = 0x1L;
private long initBits = 0x1;
private @Nullable PluginDescriptor pluginDescriptor;
private ImmutableMap.Builder propertiesBuilder = ImmutableMap.builder();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code PluginConfig} 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
*/
public final Builder copyFrom(PluginConfig instance) {
Preconditions.checkNotNull(instance, "instance");
pluginDescriptor(instance.pluginDescriptor());
putAllProperties(instance.properties());
return this;
}
/**
* Initializes the value for the {@link PluginConfig#pluginDescriptor() pluginDescriptor} attribute.
* @param pluginDescriptor The value for pluginDescriptor
* @return {@code this} builder for use in a chained invocation
*/
public final Builder pluginDescriptor(PluginDescriptor pluginDescriptor) {
this.pluginDescriptor = Preconditions.checkNotNull(pluginDescriptor, "pluginDescriptor");
initBits &= ~INIT_BIT_PLUGIN_DESCRIPTOR;
return this;
}
/**
* Put one entry to the {@link PluginConfig#properties() properties} map.
* @param key The key in the properties map
* @param value The associated value in the properties map
* @return {@code this} builder for use in a chained invocation
*/
public final Builder putProperties(String key, PropertyValue value) {
propertiesBuilder.put(key, value);
return this;
}
/**
* Put one entry to the {@link PluginConfig#properties() properties} map. Nulls are not permitted
* @param entry The key and value entry
* @return {@code this} builder for use in a chained invocation
*/
public final Builder putProperties(Map.Entry entry) {
propertiesBuilder.put(entry);
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link PluginConfig#properties() properties} map. Nulls are not permitted
* @param entries The entries that will be added to the properties map
* @return {@code this} builder for use in a chained invocation
*/
public final Builder properties(Map entries) {
propertiesBuilder = ImmutableMap.builder();
return putAllProperties(entries);
}
/**
* Put all mappings from the specified map as entries to {@link PluginConfig#properties() properties} map. Nulls are not permitted
* @param entries The entries that will be added to the properties map
* @return {@code this} builder for use in a chained invocation
*/
public final Builder putAllProperties(Map entries) {
propertiesBuilder.putAll(entries);
return this;
}
/**
* Builds a new {@link ImmutablePluginConfig ImmutablePluginConfig}.
* @return An immutable instance of PluginConfig
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutablePluginConfig build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutablePluginConfig(pluginDescriptor, propertiesBuilder.build());
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if ((initBits & INIT_BIT_PLUGIN_DESCRIPTOR) != 0) attributes.add("pluginDescriptor");
return "Cannot build PluginConfig, some of required attributes are not set " + attributes;
}
}
}