
org.glowroot.storage.simplerepo.util.ImmutableIndex 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.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 Schema.Index}.
*
* Use builder to create immutable instances:
* {@code ImmutableIndex.builder()}.
* Use static factory method to create immutable instances:
* {@code ImmutableIndex.of()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "Schema.Index"})
@Immutable
public final class ImmutableIndex extends Schema.Index {
private final String name;
private final ImmutableList columns;
private final boolean unique;
private ImmutableIndex(String name, Iterable columns) {
this.name = Preconditions.checkNotNull(name);
this.columns = ImmutableList.copyOf(columns);
this.unique = super.unique();
}
private ImmutableIndex(ImmutableIndex.Builder builder) {
this.name = builder.name;
this.columns = builder.columnsBuilder.build();
this.unique = builder.uniqueIsSet()
? builder.unique
: super.unique();
}
private ImmutableIndex(
String name,
ImmutableList columns,
boolean unique) {
this.name = name;
this.columns = columns;
this.unique = unique;
}
/**
* @return value of {@code name} attribute
*/
@JsonProperty
@Override
public String name() {
return name;
}
/**
* @return value of {@code columns} attribute
*/
@JsonProperty
@Override
public ImmutableList columns() {
return columns;
}
/**
* @return value of {@code unique} attribute
*/
@JsonProperty
@Override
public boolean unique() {
return unique;
}
/**
* Copy current immutable object by setting value for {@link Schema.Index#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 ImmutableIndex withName(String value) {
if (this.name == value) return this;
String newValue = Preconditions.checkNotNull(value);
return new ImmutableIndex(newValue, this.columns, this.unique);
}
/**
* Copy current immutable object with elements that replace content of {@link Schema.Index#columns() columns}.
* @param elements elements to set
* @return modified copy of {@code this} object
*/
public final ImmutableIndex withColumns(String... elements) {
ImmutableList newValue = ImmutableList.copyOf(elements);
return new ImmutableIndex(this.name, newValue, this.unique);
}
/**
* Copy current immutable object with elements that replace content of {@link Schema.Index#columns() columns}.
* Shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements iterable of columns elements to set
* @return modified copy of {@code this} object
*/
public final ImmutableIndex withColumns(Iterable elements) {
if (this.columns == elements) return this;
ImmutableList newValue = ImmutableList.copyOf(elements);
return new ImmutableIndex(this.name, newValue, this.unique);
}
/**
* Copy current immutable object by setting value for {@link Schema.Index#unique() unique}.
* Value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for unique
* @return modified copy of the {@code this} object
*/
public final ImmutableIndex withUnique(boolean value) {
if (this.unique == value) return this;
boolean newValue = value;
return new ImmutableIndex(this.name, this.columns, newValue);
}
/**
* This instance is equal to instances of {@code ImmutableIndex} 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 ImmutableIndex
&& equalTo((ImmutableIndex) another);
}
private boolean equalTo(ImmutableIndex another) {
return name.equals(another.name)
&& columns.equals(another.columns)
&& unique == another.unique;
}
/**
* Computes hash code from attributes: {@code name}, {@code columns}, {@code unique}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + name.hashCode();
h = h * 17 + columns.hashCode();
h = h * 17 + Booleans.hashCode(unique);
return h;
}
/**
* Prints immutable value {@code Index...} with attribute values,
* excluding any non-generated and auxiliary attributes.
* @return string representation of value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("Index")
.add("name", name)
.add("columns", columns)
.add("unique", unique)
.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 ImmutableList columns;
@JsonProperty
@Nullable Boolean unique;
}
/**
* @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 ImmutableIndex fromJson(Json json) {
ImmutableIndex.Builder builder = ImmutableIndex.builder();
if (json.name != null) {
builder.name(json.name);
}
if (json.columns != null) {
builder.addAllColumns(json.columns);
}
if (json.unique != null) {
builder.unique(json.unique);
}
return builder.build();
}
/**
* Construct new immutable {@code Index} instance.
* @param name value for {@code name}
* @param columns value for {@code columns}
* @return immutable Index instance
*/
public static ImmutableIndex of(String name, ImmutableList columns) {
return of(name, (Iterable) columns);
}
/**
* Construct new immutable {@code Index} instance.
* @param name value for {@code name}
* @param columns value for {@code columns}
* @return immutable Index instance
*/
public static ImmutableIndex of(String name, Iterable columns) {
return new ImmutableIndex(name, columns);
}
/**
* Creates immutable copy of {@link Schema.Index}.
* 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 Index instance
*/
public static ImmutableIndex copyOf(Schema.Index instance) {
if (instance instanceof ImmutableIndex) {
return (ImmutableIndex) instance;
}
return ImmutableIndex.builder()
.copyFrom(instance)
.build();
}
/**
* Creates builder for {@link org.glowroot.storage.simplerepo.util.ImmutableIndex ImmutableIndex}.
* @return new ImmutableIndex builder
*/
public static ImmutableIndex.Builder builder() {
return new ImmutableIndex.Builder();
}
/**
* Builds instances of {@link org.glowroot.storage.simplerepo.util.ImmutableIndex ImmutableIndex}.
* 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 OPT_BIT_UNIQUE = 0x1L;
private long initBits = 0x1;
private long optBits;
private @Nullable String name;
private ImmutableList.Builder columnsBuilder = ImmutableList.builder();
private boolean unique;
private Builder() {}
/**
* Fill builder with attribute values from provided {@link Schema.Index} 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(Schema.Index instance) {
Preconditions.checkNotNull(instance);
name(instance.name());
addAllColumns(instance.columns());
unique(instance.unique());
return this;
}
/**
* Initializes value for {@link Schema.Index#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;
}
/**
* Adds one element to {@link Schema.Index#columns() columns} list.
* @param element columns element
* @return {@code this} builder for chained invocation
*/
public final Builder addColumns(String element) {
columnsBuilder.add(element);
return this;
}
/**
* Adds elements to {@link Schema.Index#columns() columns} list.
* @param elements array of columns elements
* @return {@code this} builder for chained invocation
*/
public final Builder addColumns(String... elements) {
columnsBuilder.add(elements);
return this;
}
/**
* Sets or replaces all elements for {@link Schema.Index#columns() columns} list.
* @param elements iterable of columns elements
* @return {@code this} builder for chained invocation
*/
public final Builder columns(Iterable elements) {
columnsBuilder = ImmutableList.builder();
return addAllColumns(elements);
}
/**
* Adds elements to {@link Schema.Index#columns() columns} list.
* @param elements iterable of columns elements
* @return {@code this} builder for chained invocation
*/
public final Builder addAllColumns(Iterable elements) {
columnsBuilder.addAll(elements);
return this;
}
/**
* Initializes value for {@link Schema.Index#unique() unique}.
* If not set, this attribute will have default value returned by initializer of {@link Schema.Index#unique() unique}.
* @param unique value for unique
* @return {@code this} builder for chained invocation
*/
public final Builder unique(boolean unique) {
this.unique = unique;
optBits |= OPT_BIT_UNIQUE;
return this;
}
/**
* Builds new {@link org.glowroot.storage.simplerepo.util.ImmutableIndex ImmutableIndex}.
* @return immutable instance of Index
* @throws exception {@code java.lang.IllegalStateException} if any required attributes are missing
*/
public ImmutableIndex build()
throws IllegalStateException {
checkRequiredAttributes(); return new ImmutableIndex(this);
}
private boolean uniqueIsSet() {
return (optBits & OPT_BIT_UNIQUE) != 0;
}
private boolean nameIsSet() {
return (initBits & INIT_BIT_NAME) == 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");
return "Cannot build Index, some of required attributes are not set " + attributes;
}
}
}