org.glowroot.weaving.AdviceMatcher Maven / Gradle / Ivy
package org.glowroot.weaving;
import org.glowroot.shaded.google.common.base.MoreObjects;
import org.glowroot.shaded.google.common.base.Preconditions;
import org.glowroot.shaded.google.common.collect.Lists;
import java.util.Collection;
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 AdviceMatcherBase}.
*
* Use builder to create immutable instances:
* {@code AdviceMatcher.builder()}.
* Use static factory method to create immutable instances:
* {@code AdviceMatcher.of()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "AdviceMatcherBase"})
@Immutable
final class AdviceMatcher extends AdviceMatcherBase {
private final Advice advice;
private AdviceMatcher(Advice advice) {
this.advice = Preconditions.checkNotNull(advice);
}
private AdviceMatcher(AdviceMatcher.Builder builder) {
this.advice = builder.advice;
}
private AdviceMatcher(AdviceMatcher original, Advice advice) {
this.advice = advice;
}
/**
* {@inheritDoc}
* @return value of {@code advice} attribute
*/
@Override
public Advice advice() {
return advice;
}
/**
* Copy current immutable object by setting value for {@link AdviceMatcherBase#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 AdviceMatcher withAdvice(Advice value) {
if (this.advice == value) {
return this;
}
Advice newValue = Preconditions.checkNotNull(value);
return new AdviceMatcher(this, newValue);
}
/**
* This instance is equal to instances of {@code AdviceMatcher} with equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(@Nullable Object another) {
return this == another
|| (another instanceof AdviceMatcher && equalTo((AdviceMatcher) another));
}
private boolean equalTo(AdviceMatcher 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();
}
/**
* Construct new immutable {@code AdviceMatcher} instance.
* @param advice value for {@code advice}
* @return immutable AdviceMatcher instance
*/
public static AdviceMatcher of(Advice advice) {
return new AdviceMatcher(advice);
}
/**
* Creates immutable copy of {@link AdviceMatcherBase}.
* Uses accessors to get values to initialize immutable instance.
* If an instance is already immutable, it is returned as is.
* @return copied immutable AdviceMatcher instance
*/
static AdviceMatcher copyOf(AdviceMatcherBase instance) {
if (instance instanceof AdviceMatcher) {
return (AdviceMatcher) instance;
}
return AdviceMatcher.builder()
.from(instance)
.build();
}
/**
* Creates builder for {@link org.glowroot.weaving.AdviceMatcher}.
* @return new AdviceMatcher builder
*/
static AdviceMatcher.Builder builder() {
return new AdviceMatcher.Builder();
}
/**
* Builds instances of {@link org.glowroot.weaving.AdviceMatcher}.
* Initialized attributes and then invoke {@link #build()} method to create
* immutable instance.
*
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 INITIALIZED_BITSET_ALL = 0x1;
private static final long INITIALIZED_BIT_ADVICE = 0x1L;
private long initializedBitset;
private @Nullable Advice advice;
private Builder() {}
/**
* Adjust builder with values from provided {@link AdviceMatcherBase} instance.
* Regular attribute values will be overridden, i.e. replaced with ones of an instance.
* Instance's absent optional values will not be copied (will not override current).
* 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 from(AdviceMatcherBase instance) {
Preconditions.checkNotNull(instance);
advice(instance.advice());
return this;
}
/**
* Initializes value for {@link AdviceMatcherBase#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);
initializedBitset |= INITIALIZED_BIT_ADVICE;
return this;
}
/**
* Builds new {@link org.glowroot.weaving.AdviceMatcher}.
* @return immutable instance of AdviceMatcher
*/
public org.glowroot.weaving.AdviceMatcher build() {
checkRequiredAttributes();
return new AdviceMatcher(this);
}
private boolean adviceIsSet() {
return (initializedBitset & INITIALIZED_BIT_ADVICE) != 0;
}
private void checkRequiredAttributes() {
if (initializedBitset != INITIALIZED_BITSET_ALL) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
}
private String formatRequiredAttributesMessage() {
Collection attributes = Lists.newArrayList();
if (!adviceIsSet()) {
attributes.add("advice");
}
return "Cannot build AdviceMatcher, some of required attributes are not set " + attributes;
}
}
}