All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glowroot.common.config.ImmutablePropertyDescriptor Maven / Gradle / Ivy

There is a newer version: 0.9.28
Show newest version
package org.glowroot.common.config;

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.Objects;
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.ArrayList;
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 PropertyDescriptor}.
 * 

* Use builder to create immutable instances: * {@code ImmutablePropertyDescriptor.builder()}. */ @SuppressWarnings("all") @ParametersAreNonnullByDefault @Generated({"Immutables.generator", "PropertyDescriptor"}) @Immutable public final class ImmutablePropertyDescriptor extends PropertyDescriptor { private final String name; private final PropertyValue.PropertyType type; private final @Nullable PropertyValue defaultValue; private final boolean hidden; private final String label; private final String checkboxLabel; private final String description; private ImmutablePropertyDescriptor(ImmutablePropertyDescriptor.Builder builder) { this.name = builder.name; this.type = builder.type; this.defaultValue = builder.defaultValue; this.label = builder.label; if (builder.hiddenIsSet()) { initShim.hidden(builder.hidden); } if (builder.checkboxLabel != null) { initShim.checkboxLabel(builder.checkboxLabel); } if (builder.description != null) { initShim.description(builder.description); } this.hidden = initShim.hidden(); this.checkboxLabel = initShim.checkboxLabel(); this.description = initShim.description(); 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 hidden; private byte hiddenStage; boolean hidden() { if (hiddenStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (hiddenStage == STAGE_UNINITIALIZED) { hiddenStage = STAGE_INITIALIZING; this.hidden = ImmutablePropertyDescriptor.super.hidden(); hiddenStage = STAGE_INITIALIZED; } return hidden; } boolean hidden(boolean value) { this.hidden = value; hiddenStage = STAGE_INITIALIZED; return value; } private String checkboxLabel; private byte checkboxLabelStage; String checkboxLabel() { if (checkboxLabelStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (checkboxLabelStage == STAGE_UNINITIALIZED) { checkboxLabelStage = STAGE_INITIALIZING; this.checkboxLabel = Preconditions.checkNotNull(ImmutablePropertyDescriptor.super.checkboxLabel()); checkboxLabelStage = STAGE_INITIALIZED; } return checkboxLabel; } String checkboxLabel(String value) { this.checkboxLabel = value; checkboxLabelStage = STAGE_INITIALIZED; return value; } private String description; private byte descriptionStage; String description() { if (descriptionStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (descriptionStage == STAGE_UNINITIALIZED) { descriptionStage = STAGE_INITIALIZING; this.description = Preconditions.checkNotNull(ImmutablePropertyDescriptor.super.description()); descriptionStage = STAGE_INITIALIZED; } return description; } String description(String value) { this.description = value; descriptionStage = STAGE_INITIALIZED; return value; } private String formatInitCycleMessage() { ArrayList attributes = Lists.newArrayList();; if (hiddenStage == STAGE_INITIALIZING) attributes.add("hidden"); if (checkboxLabelStage == STAGE_INITIALIZING) attributes.add("checkboxLabel"); if (descriptionStage == STAGE_INITIALIZING) attributes.add("description"); return "Cannot build PropertyDescriptor, attribute initializers form cycle" + attributes; } } private ImmutablePropertyDescriptor( String name, PropertyValue.PropertyType type, @Nullable PropertyValue defaultValue, boolean hidden, String label, String checkboxLabel, String description) { this.name = name; this.type = type; this.defaultValue = defaultValue; this.hidden = hidden; this.label = label; this.checkboxLabel = checkboxLabel; this.description = description; this.initShim = null; } /** * @return value of {@code name} attribute */ @JsonProperty @Override public String name() { return name; } /** * @return value of {@code type} attribute */ @JsonProperty @Override public PropertyValue.PropertyType type() { return type; } /** * @return value of {@code defaultValue} attribute */ @JsonProperty(value = "default") @Override public @Nullable PropertyValue defaultValue() { return defaultValue; } /** * @return value of {@code hidden} attribute */ @JsonProperty @Override public boolean hidden() { return initShim != null ? initShim.hidden() : hidden; } /** * @return value of {@code label} attribute */ @JsonProperty @Override public String label() { return label; } /** * @return value of {@code checkboxLabel} attribute */ @JsonProperty @Override public String checkboxLabel() { return initShim != null ? initShim.checkboxLabel() : checkboxLabel; } /** * @return value of {@code description} attribute */ @JsonProperty @Override public String description() { return initShim != null ? initShim.description() : description; } /** * Copy current immutable object by setting value for {@link PropertyDescriptor#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 ImmutablePropertyDescriptor withName(String value) { if (this.name == value) return this; String newValue = Preconditions.checkNotNull(value); return new ImmutablePropertyDescriptor( newValue, this.type, this.defaultValue, this.hidden, this.label, this.checkboxLabel, this.description); } /** * Copy current immutable object by setting value for {@link PropertyDescriptor#type() type}. * Shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value new value for type * @return modified copy of the {@code this} object */ public final ImmutablePropertyDescriptor withType(PropertyValue.PropertyType value) { if (this.type == value) return this; PropertyValue.PropertyType newValue = Preconditions.checkNotNull(value); return new ImmutablePropertyDescriptor( this.name, newValue, this.defaultValue, this.hidden, this.label, this.checkboxLabel, this.description); } /** * Copy current immutable object by setting value for {@link PropertyDescriptor#defaultValue() defaultValue}. * Shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value new value for defaultValue, can be {@code null} * @return modified copy of the {@code this} object */ public final ImmutablePropertyDescriptor withDefaultValue(@Nullable PropertyValue value) { if (this.defaultValue == value) return this; @Nullable PropertyValue newValue = value; return new ImmutablePropertyDescriptor(this.name, this.type, newValue, this.hidden, this.label, this.checkboxLabel, this.description); } /** * Copy current immutable object by setting value for {@link PropertyDescriptor#hidden() hidden}. * Value equality check is used to prevent copying of the same value by returning {@code this}. * @param value new value for hidden * @return modified copy of the {@code this} object */ public final ImmutablePropertyDescriptor withHidden(boolean value) { if (this.hidden == value) return this; boolean newValue = value; return new ImmutablePropertyDescriptor( this.name, this.type, this.defaultValue, newValue, this.label, this.checkboxLabel, this.description); } /** * Copy current immutable object by setting value for {@link PropertyDescriptor#label() label}. * Shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value new value for label * @return modified copy of the {@code this} object */ public final ImmutablePropertyDescriptor withLabel(String value) { if (this.label == value) return this; String newValue = Preconditions.checkNotNull(value); return new ImmutablePropertyDescriptor( this.name, this.type, this.defaultValue, this.hidden, newValue, this.checkboxLabel, this.description); } /** * Copy current immutable object by setting value for {@link PropertyDescriptor#checkboxLabel() checkboxLabel}. * Shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value new value for checkboxLabel * @return modified copy of the {@code this} object */ public final ImmutablePropertyDescriptor withCheckboxLabel(String value) { if (this.checkboxLabel == value) return this; String newValue = Preconditions.checkNotNull(value); return new ImmutablePropertyDescriptor(this.name, this.type, this.defaultValue, this.hidden, this.label, newValue, this.description); } /** * Copy current immutable object by setting value for {@link PropertyDescriptor#description() description}. * Shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value new value for description * @return modified copy of the {@code this} object */ public final ImmutablePropertyDescriptor withDescription(String value) { if (this.description == value) return this; String newValue = Preconditions.checkNotNull(value); return new ImmutablePropertyDescriptor(this.name, this.type, this.defaultValue, this.hidden, this.label, this.checkboxLabel, newValue); } /** * This instance is equal to instances of {@code ImmutablePropertyDescriptor} 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 ImmutablePropertyDescriptor && equalTo((ImmutablePropertyDescriptor) another); } private boolean equalTo(ImmutablePropertyDescriptor another) { return name.equals(another.name) && type.equals(another.type) && Objects.equal(defaultValue, another.defaultValue) && hidden == another.hidden && label.equals(another.label) && checkboxLabel.equals(another.checkboxLabel) && description.equals(another.description); } /** * Computes hash code from attributes: {@code name}, {@code type}, {@code defaultValue}, {@code hidden}, {@code label}, {@code checkboxLabel}, {@code description}. * @return hashCode value */ @Override public int hashCode() { int h = 31; h = h * 17 + name.hashCode(); h = h * 17 + type.hashCode(); h = h * 17 + Objects.hashCode(defaultValue); h = h * 17 + Booleans.hashCode(hidden); h = h * 17 + label.hashCode(); h = h * 17 + checkboxLabel.hashCode(); h = h * 17 + description.hashCode(); return h; } /** * Prints immutable value {@code PropertyDescriptor...} with attribute values, * excluding any non-generated and auxiliary attributes. * @return string representation of value */ @Override public String toString() { return MoreObjects.toStringHelper("PropertyDescriptor") .add("name", name) .add("type", type) .add("defaultValue", defaultValue) .add("hidden", hidden) .add("label", label) .add("checkboxLabel", checkboxLabel) .add("description", description) .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 PropertyValue.PropertyType type; @JsonProperty(value = "default") @Nullable PropertyValue defaultValue; @JsonProperty @Nullable Boolean hidden; @JsonProperty @Nullable String label; @JsonProperty @Nullable String checkboxLabel; @JsonProperty @Nullable String description; } /** * @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 ImmutablePropertyDescriptor fromJson(Json json) { ImmutablePropertyDescriptor.Builder builder = ImmutablePropertyDescriptor.builder(); if (json.name != null) { builder.name(json.name); } if (json.type != null) { builder.type(json.type); } if (json.defaultValue != null) { builder.defaultValue(json.defaultValue); } if (json.hidden != null) { builder.hidden(json.hidden); } if (json.label != null) { builder.label(json.label); } if (json.checkboxLabel != null) { builder.checkboxLabel(json.checkboxLabel); } if (json.description != null) { builder.description(json.description); } return builder.build(); } /** * Creates immutable copy of {@link PropertyDescriptor}. * 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 PropertyDescriptor instance */ public static ImmutablePropertyDescriptor copyOf(PropertyDescriptor instance) { if (instance instanceof ImmutablePropertyDescriptor) { return (ImmutablePropertyDescriptor) instance; } return ImmutablePropertyDescriptor.builder() .copyFrom(instance) .build(); } /** * Creates builder for {@link org.glowroot.common.config.ImmutablePropertyDescriptor ImmutablePropertyDescriptor}. * @return new ImmutablePropertyDescriptor builder */ public static ImmutablePropertyDescriptor.Builder builder() { return new ImmutablePropertyDescriptor.Builder(); } /** * Builds instances of {@link org.glowroot.common.config.ImmutablePropertyDescriptor ImmutablePropertyDescriptor}. * 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_TYPE = 0x2L; private static final long INIT_BIT_LABEL = 0x4L; private static final long OPT_BIT_HIDDEN = 0x1L; private long initBits = 0x7; private long optBits; private @Nullable String name; private @Nullable PropertyValue.PropertyType type; private @Nullable PropertyValue defaultValue; private boolean hidden; private @Nullable String label; private @Nullable String checkboxLabel; private @Nullable String description; private Builder() {} /** * Fill builder with attribute values from provided {@link PropertyDescriptor} 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(PropertyDescriptor instance) { Preconditions.checkNotNull(instance); name(instance.name()); type(instance.type()); @Nullable PropertyValue defaultValueValue = instance.defaultValue(); if (defaultValueValue != null) { defaultValue(defaultValueValue); } hidden(instance.hidden()); label(instance.label()); checkboxLabel(instance.checkboxLabel()); description(instance.description()); return this; } /** * Initializes value for {@link PropertyDescriptor#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 PropertyDescriptor#type() type}. * @param type value for type * @return {@code this} builder for chained invocation */ public final Builder type(PropertyValue.PropertyType type) { this.type = Preconditions.checkNotNull(type); initBits &= ~INIT_BIT_TYPE; return this; } /** * Initializes value for {@link PropertyDescriptor#defaultValue() defaultValue}. * @param defaultValue value for defaultValue, can be {@code null} * @return {@code this} builder for chained invocation */ public final Builder defaultValue(@Nullable PropertyValue defaultValue) { this.defaultValue = defaultValue; return this; } /** * Initializes value for {@link PropertyDescriptor#hidden() hidden}. *

If not set, this attribute will have default value returned by initializer of {@link PropertyDescriptor#hidden() hidden}. * @param hidden value for hidden * @return {@code this} builder for chained invocation */ public final Builder hidden(boolean hidden) { this.hidden = hidden; optBits |= OPT_BIT_HIDDEN; return this; } /** * Initializes value for {@link PropertyDescriptor#label() label}. * @param label value for label * @return {@code this} builder for chained invocation */ public final Builder label(String label) { this.label = Preconditions.checkNotNull(label); initBits &= ~INIT_BIT_LABEL; return this; } /** * Initializes value for {@link PropertyDescriptor#checkboxLabel() checkboxLabel}. *

If not set, this attribute will have default value returned by initializer of {@link PropertyDescriptor#checkboxLabel() checkboxLabel}. * @param checkboxLabel value for checkboxLabel * @return {@code this} builder for chained invocation */ public final Builder checkboxLabel(String checkboxLabel) { this.checkboxLabel = Preconditions.checkNotNull(checkboxLabel); return this; } /** * Initializes value for {@link PropertyDescriptor#description() description}. *

If not set, this attribute will have default value returned by initializer of {@link PropertyDescriptor#description() description}. * @param description value for description * @return {@code this} builder for chained invocation */ public final Builder description(String description) { this.description = Preconditions.checkNotNull(description); return this; } /** * Builds new {@link org.glowroot.common.config.ImmutablePropertyDescriptor ImmutablePropertyDescriptor}. * @return immutable instance of PropertyDescriptor * @throws exception {@code java.lang.IllegalStateException} if any required attributes are missing */ public ImmutablePropertyDescriptor build() throws IllegalStateException { checkRequiredAttributes(); return new ImmutablePropertyDescriptor(this); } private boolean hiddenIsSet() { return (optBits & OPT_BIT_HIDDEN) != 0; } private boolean nameIsSet() { return (initBits & INIT_BIT_NAME) == 0; } private boolean typeIsSet() { return (initBits & INIT_BIT_TYPE) == 0; } private boolean labelIsSet() { return (initBits & INIT_BIT_LABEL) == 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 (!typeIsSet()) attributes.add("type"); if (!labelIsSet()) attributes.add("label"); return "Cannot build PropertyDescriptor, some of required attributes are not set " + attributes; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy