
org.glowroot.agent.weaving.ImmutableAdviceMatcher Maven / Gradle / Ivy
package org.glowroot.agent.weaving;
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.Lists;
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 AdviceMatcher}.
*
* Use builder to create immutable instances:
* {@code ImmutableAdviceMatcher.builder()}.
* Use static factory method to create immutable instances:
* {@code ImmutableAdviceMatcher.of()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "AdviceMatcher"})
@Immutable
final class ImmutableAdviceMatcher extends AdviceMatcher {
private final Advice advice;
private ImmutableAdviceMatcher(Advice advice) {
this.advice = Preconditions.checkNotNull(advice);
}
private ImmutableAdviceMatcher(ImmutableAdviceMatcher original, Advice advice) {
this.advice = advice;
}
/**
* @return value of {@code advice} attribute
*/
@JsonProperty
@Override
Advice advice() {
return advice;
}
/**
* Copy current immutable object by setting value for {@link AdviceMatcher#advice() advice}.
* Shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for advice
* @return modified copy of the {@code this} object
*/
public final ImmutableAdviceMatcher withAdvice(Advice value) {
if (this.advice == value) return this;
Advice newValue = Preconditions.checkNotNull(value);
return new ImmutableAdviceMatcher(this, newValue);
}
/**
* This instance is equal to instances of {@code ImmutableAdviceMatcher} 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 ImmutableAdviceMatcher
&& equalTo((ImmutableAdviceMatcher) another);
}
private boolean equalTo(ImmutableAdviceMatcher another) {
return advice.equals(another.advice);
}
/**
* Computes hash code from attributes: {@code advice}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + advice.hashCode();
return h;
}
/**
* Prints immutable value {@code AdviceMatcher...} with attribute values,
* excluding any non-generated and auxiliary attributes.
* @return string representation of value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("AdviceMatcher")
.add("advice", advice)
.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 Advice advice;
}
/**
* @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 ImmutableAdviceMatcher fromJson(Json json) {
ImmutableAdviceMatcher.Builder builder = ImmutableAdviceMatcher.builder();
if (json.advice != null) {
builder.advice(json.advice);
}
return builder.build();
}
/**
* Construct new immutable {@code AdviceMatcher} instance.
* @param advice value for {@code advice}
* @return immutable AdviceMatcher instance
*/
public static ImmutableAdviceMatcher of(Advice advice) {
return new ImmutableAdviceMatcher(advice);
}
/**
* Creates immutable copy of {@link AdviceMatcher}.
* 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 AdviceMatcher instance
*/
static ImmutableAdviceMatcher copyOf(AdviceMatcher instance) {
if (instance instanceof ImmutableAdviceMatcher) {
return (ImmutableAdviceMatcher) instance;
}
return ImmutableAdviceMatcher.builder()
.copyFrom(instance)
.build();
}
/**
* Creates builder for {@link org.glowroot.agent.weaving.ImmutableAdviceMatcher ImmutableAdviceMatcher}.
* @return new ImmutableAdviceMatcher builder
*/
static ImmutableAdviceMatcher.Builder builder() {
return new ImmutableAdviceMatcher.Builder();
}
/**
* Builds instances of {@link org.glowroot.agent.weaving.ImmutableAdviceMatcher ImmutableAdviceMatcher}.
* 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
static final class Builder {
private static final long INIT_BIT_ADVICE = 0x1L;
private long initBits = 0x1;
private @Nullable Advice advice;
private Builder() {}
/**
* Fill builder with attribute values from provided {@link AdviceMatcher} 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(AdviceMatcher instance) {
Preconditions.checkNotNull(instance);
advice(instance.advice());
return this;
}
/**
* Initializes value for {@link AdviceMatcher#advice() advice}.
* @param advice value for advice
* @return {@code this} builder for chained invocation
*/
public final Builder advice(Advice advice) {
this.advice = Preconditions.checkNotNull(advice);
initBits &= ~INIT_BIT_ADVICE;
return this;
}
/**
* Builds new {@link org.glowroot.agent.weaving.ImmutableAdviceMatcher ImmutableAdviceMatcher}.
* @return immutable instance of AdviceMatcher
* @throws exception {@code java.lang.IllegalStateException} if any required attributes are missing
*/
public ImmutableAdviceMatcher build()
throws IllegalStateException {
checkRequiredAttributes(); return new ImmutableAdviceMatcher(null, advice);
}
private boolean adviceIsSet() {
return (initBits & INIT_BIT_ADVICE) == 0;
}
private void checkRequiredAttributes() throws IllegalStateException {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if (!adviceIsSet()) attributes.add("advice");
return "Cannot build AdviceMatcher, some of required attributes are not set " + attributes;
}
}
}