
org.glowroot.storage.simplerepo.util.ImmutableColumn Maven / Gradle / Ivy
package org.glowroot.storage.simplerepo.util;
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 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 Schema.Column}.
*
* Use builder to create immutable instances:
* {@code ImmutableColumn.builder()}.
* Use static factory method to create immutable instances:
* {@code ImmutableColumn.of()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "Schema.Column"})
@Immutable
public final class ImmutableColumn extends Schema.Column {
private final String name;
private final Schema.ColumnType type;
private final boolean primaryKey;
private ImmutableColumn(String name, Schema.ColumnType type) {
this.name = Preconditions.checkNotNull(name);
this.type = Preconditions.checkNotNull(type);
this.primaryKey = super.primaryKey();
}
private ImmutableColumn(ImmutableColumn.Builder builder) {
this.name = builder.name;
this.type = builder.type;
this.primaryKey = builder.primaryKeyIsSet()
? builder.primaryKey
: super.primaryKey();
}
private ImmutableColumn(
String name,
Schema.ColumnType type,
boolean primaryKey) {
this.name = name;
this.type = type;
this.primaryKey = primaryKey;
}
/**
* @return value of {@code name} attribute
*/
@JsonProperty
@Override
public String name() {
return name;
}
/**
* @return value of {@code type} attribute
*/
@JsonProperty
@Override
public Schema.ColumnType type() {
return type;
}
/**
* @return value of {@code primaryKey} attribute
*/
@JsonProperty
@Override
public boolean primaryKey() {
return primaryKey;
}
/**
* Copy current immutable object by setting value for {@link Schema.Column#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 ImmutableColumn withName(String value) {
if (this.name == value) return this;
String newValue = Preconditions.checkNotNull(value);
return new ImmutableColumn(newValue, this.type, this.primaryKey);
}
/**
* Copy current immutable object by setting value for {@link Schema.Column#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 ImmutableColumn withType(Schema.ColumnType value) {
if (this.type == value) return this;
Schema.ColumnType newValue = Preconditions.checkNotNull(value);
return new ImmutableColumn(this.name, newValue, this.primaryKey);
}
/**
* Copy current immutable object by setting value for {@link Schema.Column#primaryKey() primaryKey}.
* Value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for primaryKey
* @return modified copy of the {@code this} object
*/
public final ImmutableColumn withPrimaryKey(boolean value) {
if (this.primaryKey == value) return this;
boolean newValue = value;
return new ImmutableColumn(this.name, this.type, newValue);
}
/**
* This instance is equal to instances of {@code ImmutableColumn} 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 ImmutableColumn
&& equalTo((ImmutableColumn) another);
}
private boolean equalTo(ImmutableColumn another) {
return name.equals(another.name)
&& type.equals(another.type)
&& primaryKey == another.primaryKey;
}
/**
* Computes hash code from attributes: {@code name}, {@code type}, {@code primaryKey}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + name.hashCode();
h = h * 17 + type.hashCode();
h = h * 17 + Booleans.hashCode(primaryKey);
return h;
}
/**
* Prints immutable value {@code Column...} with attribute values,
* excluding any non-generated and auxiliary attributes.
* @return string representation of value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("Column")
.add("name", name)
.add("type", type)
.add("primaryKey", primaryKey)
.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 Schema.ColumnType type;
@JsonProperty
@Nullable Boolean primaryKey;
}
/**
* @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 ImmutableColumn fromJson(Json json) {
ImmutableColumn.Builder builder = ImmutableColumn.builder();
if (json.name != null) {
builder.name(json.name);
}
if (json.type != null) {
builder.type(json.type);
}
if (json.primaryKey != null) {
builder.primaryKey(json.primaryKey);
}
return builder.build();
}
/**
* Construct new immutable {@code Column} instance.
* @param name value for {@code name}
* @param type value for {@code type}
* @return immutable Column instance
*/
public static ImmutableColumn of(String name, Schema.ColumnType type) {
return new ImmutableColumn(name, type);
}
/**
* Creates immutable copy of {@link Schema.Column}.
* 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 Column instance
*/
public static ImmutableColumn copyOf(Schema.Column instance) {
if (instance instanceof ImmutableColumn) {
return (ImmutableColumn) instance;
}
return ImmutableColumn.builder()
.copyFrom(instance)
.build();
}
/**
* Creates builder for {@link org.glowroot.storage.simplerepo.util.ImmutableColumn ImmutableColumn}.
* @return new ImmutableColumn builder
*/
public static ImmutableColumn.Builder builder() {
return new ImmutableColumn.Builder();
}
/**
* Builds instances of {@link org.glowroot.storage.simplerepo.util.ImmutableColumn ImmutableColumn}.
* 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 OPT_BIT_PRIMARY_KEY = 0x1L;
private long initBits = 0x3;
private long optBits;
private @Nullable String name;
private @Nullable Schema.ColumnType type;
private boolean primaryKey;
private Builder() {}
/**
* Fill builder with attribute values from provided {@link Schema.Column} 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(Schema.Column instance) {
Preconditions.checkNotNull(instance);
name(instance.name());
type(instance.type());
primaryKey(instance.primaryKey());
return this;
}
/**
* Initializes value for {@link Schema.Column#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 Schema.Column#type() type}.
* @param type value for type
* @return {@code this} builder for chained invocation
*/
public final Builder type(Schema.ColumnType type) {
this.type = Preconditions.checkNotNull(type);
initBits &= ~INIT_BIT_TYPE;
return this;
}
/**
* Initializes value for {@link Schema.Column#primaryKey() primaryKey}.
*
If not set, this attribute will have default value returned by initializer of {@link Schema.Column#primaryKey() primaryKey}.
* @param primaryKey value for primaryKey
* @return {@code this} builder for chained invocation
*/
public final Builder primaryKey(boolean primaryKey) {
this.primaryKey = primaryKey;
optBits |= OPT_BIT_PRIMARY_KEY;
return this;
}
/**
* Builds new {@link org.glowroot.storage.simplerepo.util.ImmutableColumn ImmutableColumn}.
* @return immutable instance of Column
* @throws exception {@code java.lang.IllegalStateException} if any required attributes are missing
*/
public ImmutableColumn build()
throws IllegalStateException {
checkRequiredAttributes(); return new ImmutableColumn(this);
}
private boolean primaryKeyIsSet() {
return (optBits & OPT_BIT_PRIMARY_KEY) != 0;
}
private boolean nameIsSet() {
return (initBits & INIT_BIT_NAME) == 0;
}
private boolean typeIsSet() {
return (initBits & INIT_BIT_TYPE) == 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");
return "Cannot build Column, some of required attributes are not set " + attributes;
}
}
}