
org.glowroot.agent.weaving.ImmutableParseContext 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.Objects;
import org.glowroot.agent.shaded.google.common.base.Preconditions;
import org.glowroot.agent.shaded.google.common.collect.Lists;
import java.security.CodeSource;
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 AnalyzedWorld.ParseContext}.
*
* Use builder to create immutable instances:
* {@code ImmutableParseContext.builder()}.
* Use static factory method to create immutable instances:
* {@code ImmutableParseContext.of()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "AnalyzedWorld.ParseContext"})
@Immutable
final class ImmutableParseContext extends AnalyzedWorld.ParseContext {
private final String className;
private final @Nullable CodeSource codeSource;
private ImmutableParseContext(String className, @Nullable CodeSource codeSource) {
this.className = Preconditions.checkNotNull(className);
this.codeSource = codeSource;
}
private ImmutableParseContext(
ImmutableParseContext original,
String className,
@Nullable CodeSource codeSource) {
this.className = className;
this.codeSource = codeSource;
}
/**
* @return value of {@code className} attribute
*/
@JsonProperty
@Override
String className() {
return className;
}
/**
* @return value of {@code codeSource} attribute
*/
@JsonProperty
@Override
@Nullable CodeSource codeSource() {
return codeSource;
}
/**
* Copy current immutable object by setting value for {@link AnalyzedWorld.ParseContext#className() className}.
* Shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for className
* @return modified copy of the {@code this} object
*/
public final ImmutableParseContext withClassName(String value) {
if (this.className == value) return this;
String newValue = Preconditions.checkNotNull(value);
return new ImmutableParseContext(this, newValue, this.codeSource);
}
/**
* Copy current immutable object by setting value for {@link AnalyzedWorld.ParseContext#codeSource() codeSource}.
* Shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for codeSource, can be {@code null}
* @return modified copy of the {@code this} object
*/
public final ImmutableParseContext withCodeSource(@Nullable CodeSource value) {
if (this.codeSource == value) return this;
@Nullable CodeSource newValue = value;
return new ImmutableParseContext(this, this.className, newValue);
}
/**
* This instance is equal to instances of {@code ImmutableParseContext} 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 ImmutableParseContext
&& equalTo((ImmutableParseContext) another);
}
private boolean equalTo(ImmutableParseContext another) {
return className.equals(another.className)
&& Objects.equal(codeSource, another.codeSource);
}
/**
* Computes hash code from attributes: {@code className}, {@code codeSource}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + className.hashCode();
h = h * 17 + Objects.hashCode(codeSource);
return h;
}
/**
* 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 className;
@JsonProperty
@Nullable CodeSource codeSource;
}
/**
* @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 ImmutableParseContext fromJson(Json json) {
ImmutableParseContext.Builder builder = ImmutableParseContext.builder();
if (json.className != null) {
builder.className(json.className);
}
if (json.codeSource != null) {
builder.codeSource(json.codeSource);
}
return builder.build();
}
/**
* Construct new immutable {@code ParseContext} instance.
* @param className value for {@code className}
* @param codeSource value for {@code codeSource}
* @return immutable ParseContext instance
*/
public static ImmutableParseContext of(String className, @Nullable CodeSource codeSource) {
return new ImmutableParseContext(className, codeSource);
}
/**
* Creates immutable copy of {@link AnalyzedWorld.ParseContext}.
* 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 ParseContext instance
*/
static ImmutableParseContext copyOf(AnalyzedWorld.ParseContext instance) {
if (instance instanceof ImmutableParseContext) {
return (ImmutableParseContext) instance;
}
return ImmutableParseContext.builder()
.copyFrom(instance)
.build();
}
/**
* Creates builder for {@link org.glowroot.agent.weaving.ImmutableParseContext ImmutableParseContext}.
* @return new ImmutableParseContext builder
*/
static ImmutableParseContext.Builder builder() {
return new ImmutableParseContext.Builder();
}
/**
* Builds instances of {@link org.glowroot.agent.weaving.ImmutableParseContext ImmutableParseContext}.
* 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_CLASS_NAME = 0x1L;
private long initBits = 0x1;
private @Nullable String className;
private @Nullable CodeSource codeSource;
private Builder() {}
/**
* Fill builder with attribute values from provided {@link AnalyzedWorld.ParseContext} 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(AnalyzedWorld.ParseContext instance) {
Preconditions.checkNotNull(instance);
className(instance.className());
@Nullable CodeSource codeSourceValue = instance.codeSource();
if (codeSourceValue != null) {
codeSource(codeSourceValue);
}
return this;
}
/**
* Initializes value for {@link AnalyzedWorld.ParseContext#className() className}.
* @param className value for className
* @return {@code this} builder for chained invocation
*/
public final Builder className(String className) {
this.className = Preconditions.checkNotNull(className);
initBits &= ~INIT_BIT_CLASS_NAME;
return this;
}
/**
* Initializes value for {@link AnalyzedWorld.ParseContext#codeSource() codeSource}.
* @param codeSource value for codeSource, can be {@code null}
* @return {@code this} builder for chained invocation
*/
public final Builder codeSource(@Nullable CodeSource codeSource) {
this.codeSource = codeSource;
return this;
}
/**
* Builds new {@link org.glowroot.agent.weaving.ImmutableParseContext ImmutableParseContext}.
* @return immutable instance of ParseContext
* @throws exception {@code java.lang.IllegalStateException} if any required attributes are missing
*/
public ImmutableParseContext build()
throws IllegalStateException {
checkRequiredAttributes(); return new ImmutableParseContext(null, className, codeSource);
}
private boolean classNameIsSet() {
return (initBits & INIT_BIT_CLASS_NAME) == 0;
}
private void checkRequiredAttributes() throws IllegalStateException {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if (!classNameIsSet()) attributes.add("className");
return "Cannot build ParseContext, some of required attributes are not set " + attributes;
}
}
}