
org.glowroot.common.live.ImmutableMBeanMeta Maven / Gradle / Ivy
package org.glowroot.common.live;
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.ImmutableList;
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 LiveJvmService.MBeanMeta}.
*
* Use builder to create immutable instances:
* {@code ImmutableMBeanMeta.builder()}.
* Use static factory method to create immutable instances:
* {@code ImmutableMBeanMeta.of()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "LiveJvmService.MBeanMeta"})
@Immutable
public final class ImmutableMBeanMeta implements LiveJvmService.MBeanMeta {
private final boolean unmatched;
private final boolean unavailable;
private final ImmutableList attributeNames;
private ImmutableMBeanMeta(boolean unmatched, boolean unavailable, Iterable attributeNames) {
this.unmatched = unmatched;
this.unavailable = unavailable;
this.attributeNames = ImmutableList.copyOf(attributeNames);
}
private ImmutableMBeanMeta(
ImmutableMBeanMeta original,
boolean unmatched,
boolean unavailable,
ImmutableList attributeNames) {
this.unmatched = unmatched;
this.unavailable = unavailable;
this.attributeNames = attributeNames;
}
/**
* @return value of {@code unmatched} attribute
*/
@JsonProperty
@Override
public boolean unmatched() {
return unmatched;
}
/**
* @return value of {@code unavailable} attribute
*/
@JsonProperty
@Override
public boolean unavailable() {
return unavailable;
}
/**
* @return value of {@code attributeNames} attribute
*/
@JsonProperty
@Override
public ImmutableList attributeNames() {
return attributeNames;
}
/**
* Copy current immutable object by setting value for {@link LiveJvmService.MBeanMeta#unmatched() unmatched}.
* Value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for unmatched
* @return modified copy of the {@code this} object
*/
public final ImmutableMBeanMeta withUnmatched(boolean value) {
if (this.unmatched == value) return this;
boolean newValue = value;
return new ImmutableMBeanMeta(this, newValue, this.unavailable, this.attributeNames);
}
/**
* Copy current immutable object by setting value for {@link LiveJvmService.MBeanMeta#unavailable() unavailable}.
* Value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for unavailable
* @return modified copy of the {@code this} object
*/
public final ImmutableMBeanMeta withUnavailable(boolean value) {
if (this.unavailable == value) return this;
boolean newValue = value;
return new ImmutableMBeanMeta(this, this.unmatched, newValue, this.attributeNames);
}
/**
* Copy current immutable object with elements that replace content of {@link LiveJvmService.MBeanMeta#attributeNames() attributeNames}.
* @param elements elements to set
* @return modified copy of {@code this} object
*/
public final ImmutableMBeanMeta withAttributeNames(String... elements) {
ImmutableList newValue = ImmutableList.copyOf(elements);
return new ImmutableMBeanMeta(this, this.unmatched, this.unavailable, newValue);
}
/**
* Copy current immutable object with elements that replace content of {@link LiveJvmService.MBeanMeta#attributeNames() attributeNames}.
* Shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements iterable of attributeNames elements to set
* @return modified copy of {@code this} object
*/
public final ImmutableMBeanMeta withAttributeNames(Iterable elements) {
if (this.attributeNames == elements) return this;
ImmutableList newValue = ImmutableList.copyOf(elements);
return new ImmutableMBeanMeta(this, this.unmatched, this.unavailable, newValue);
}
/**
* This instance is equal to instances of {@code ImmutableMBeanMeta} 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 ImmutableMBeanMeta
&& equalTo((ImmutableMBeanMeta) another);
}
private boolean equalTo(ImmutableMBeanMeta another) {
return unmatched == another.unmatched
&& unavailable == another.unavailable
&& attributeNames.equals(another.attributeNames);
}
/**
* Computes hash code from attributes: {@code unmatched}, {@code unavailable}, {@code attributeNames}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + Booleans.hashCode(unmatched);
h = h * 17 + Booleans.hashCode(unavailable);
h = h * 17 + attributeNames.hashCode();
return h;
}
/**
* Prints immutable value {@code MBeanMeta...} with attribute values,
* excluding any non-generated and auxiliary attributes.
* @return string representation of value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("MBeanMeta")
.add("unmatched", unmatched)
.add("unavailable", unavailable)
.add("attributeNames", attributeNames)
.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 Boolean unmatched;
@JsonProperty
@Nullable Boolean unavailable;
@JsonProperty
@Nullable List attributeNames;
}
/**
* @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 ImmutableMBeanMeta fromJson(Json json) {
ImmutableMBeanMeta.Builder builder = ImmutableMBeanMeta.builder();
if (json.unmatched != null) {
builder.unmatched(json.unmatched);
}
if (json.unavailable != null) {
builder.unavailable(json.unavailable);
}
if (json.attributeNames != null) {
builder.addAllAttributeNames(json.attributeNames);
}
return builder.build();
}
/**
* Construct new immutable {@code MBeanMeta} instance.
* @param unmatched value for {@code unmatched}
* @param unavailable value for {@code unavailable}
* @param attributeNames value for {@code attributeNames}
* @return immutable MBeanMeta instance
*/
public static ImmutableMBeanMeta of(boolean unmatched, boolean unavailable, List attributeNames) {
return of(unmatched, unavailable, (Iterable) attributeNames);
}
/**
* Construct new immutable {@code MBeanMeta} instance.
* @param unmatched value for {@code unmatched}
* @param unavailable value for {@code unavailable}
* @param attributeNames value for {@code attributeNames}
* @return immutable MBeanMeta instance
*/
public static ImmutableMBeanMeta of(boolean unmatched, boolean unavailable, Iterable attributeNames) {
return new ImmutableMBeanMeta(unmatched, unavailable, attributeNames);
}
/**
* Creates immutable copy of {@link LiveJvmService.MBeanMeta}.
* 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 MBeanMeta instance
*/
public static ImmutableMBeanMeta copyOf(LiveJvmService.MBeanMeta instance) {
if (instance instanceof ImmutableMBeanMeta) {
return (ImmutableMBeanMeta) instance;
}
return ImmutableMBeanMeta.builder()
.copyFrom(instance)
.build();
}
/**
* Creates builder for {@link org.glowroot.common.live.ImmutableMBeanMeta ImmutableMBeanMeta}.
* @return new ImmutableMBeanMeta builder
*/
public static ImmutableMBeanMeta.Builder builder() {
return new ImmutableMBeanMeta.Builder();
}
/**
* Builds instances of {@link org.glowroot.common.live.ImmutableMBeanMeta ImmutableMBeanMeta}.
* 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_UNMATCHED = 0x1L;
private static final long INIT_BIT_UNAVAILABLE = 0x2L;
private long initBits = 0x3;
private boolean unmatched;
private boolean unavailable;
private ImmutableList.Builder attributeNamesBuilder = ImmutableList.builder();
private Builder() {}
/**
* Fill builder with attribute values from provided {@link LiveJvmService.MBeanMeta} 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(LiveJvmService.MBeanMeta instance) {
Preconditions.checkNotNull(instance);
unmatched(instance.unmatched());
unavailable(instance.unavailable());
addAllAttributeNames(instance.attributeNames());
return this;
}
/**
* Initializes value for {@link LiveJvmService.MBeanMeta#unmatched() unmatched}.
* @param unmatched value for unmatched
* @return {@code this} builder for chained invocation
*/
public final Builder unmatched(boolean unmatched) {
this.unmatched = unmatched;
initBits &= ~INIT_BIT_UNMATCHED;
return this;
}
/**
* Initializes value for {@link LiveJvmService.MBeanMeta#unavailable() unavailable}.
* @param unavailable value for unavailable
* @return {@code this} builder for chained invocation
*/
public final Builder unavailable(boolean unavailable) {
this.unavailable = unavailable;
initBits &= ~INIT_BIT_UNAVAILABLE;
return this;
}
/**
* Adds one element to {@link LiveJvmService.MBeanMeta#attributeNames() attributeNames} list.
* @param element attributeNames element
* @return {@code this} builder for chained invocation
*/
public final Builder addAttributeNames(String element) {
attributeNamesBuilder.add(element);
return this;
}
/**
* Adds elements to {@link LiveJvmService.MBeanMeta#attributeNames() attributeNames} list.
* @param elements array of attributeNames elements
* @return {@code this} builder for chained invocation
*/
public final Builder addAttributeNames(String... elements) {
attributeNamesBuilder.add(elements);
return this;
}
/**
* Sets or replaces all elements for {@link LiveJvmService.MBeanMeta#attributeNames() attributeNames} list.
* @param elements iterable of attributeNames elements
* @return {@code this} builder for chained invocation
*/
public final Builder attributeNames(Iterable elements) {
attributeNamesBuilder = ImmutableList.builder();
return addAllAttributeNames(elements);
}
/**
* Adds elements to {@link LiveJvmService.MBeanMeta#attributeNames() attributeNames} list.
* @param elements iterable of attributeNames elements
* @return {@code this} builder for chained invocation
*/
public final Builder addAllAttributeNames(Iterable elements) {
attributeNamesBuilder.addAll(elements);
return this;
}
/**
* Builds new {@link org.glowroot.common.live.ImmutableMBeanMeta ImmutableMBeanMeta}.
* @return immutable instance of MBeanMeta
* @throws exception {@code java.lang.IllegalStateException} if any required attributes are missing
*/
public ImmutableMBeanMeta build()
throws IllegalStateException {
checkRequiredAttributes();
return new ImmutableMBeanMeta(null, unmatched, unavailable, attributeNamesBuilder.build());
}
private boolean unmatchedIsSet() {
return (initBits & INIT_BIT_UNMATCHED) == 0;
}
private boolean unavailableIsSet() {
return (initBits & INIT_BIT_UNAVAILABLE) == 0;
}
private void checkRequiredAttributes() throws IllegalStateException {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if (!unmatchedIsSet()) attributes.add("unmatched");
if (!unavailableIsSet()) attributes.add("unavailable");
return "Cannot build MBeanMeta, some of required attributes are not set " + attributes;
}
}
}