com.arakelian.elastic.bulk.ImmutableBulkOperation Maven / Gradle / Ivy
package com.arakelian.elastic.bulk;
import com.arakelian.core.feature.Nullable;
import com.arakelian.elastic.model.Index;
import com.google.common.base.MoreObjects;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.CheckReturnValue;
import javax.annotation.Generated;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link BulkOperation}.
*
* Use the builder to create immutable instances:
* {@code ImmutableBulkOperation.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableBulkOperation extends BulkOperation {
private final BulkOperation.Action action;
private transient final @Nullable CharSequence compactSource;
private final String id;
private final Index index;
private final @Nullable BulkOperationListener listener;
private transient final CharSequence operation;
private final @Nullable CharSequence source;
private final String type;
private final @Nullable Long version;
private final @Nullable BulkOperation.VersionType versionType;
private ImmutableBulkOperation(ImmutableBulkOperation.Builder builder) {
this.action = builder.action;
this.id = builder.id;
this.index = builder.index;
this.listener = builder.listener;
this.source = builder.source;
this.version = builder.version;
if (builder.type != null) {
initShim.type(builder.type);
}
if (builder.versionTypeIsSet()) {
initShim.versionType(builder.versionType);
}
this.compactSource = initShim.getCompactSource();
this.operation = initShim.getOperation();
this.type = initShim.getType();
this.versionType = initShim.getVersionType();
this.initShim = null;
}
private static final byte STAGE_INITIALIZING = -1;
private static final byte STAGE_UNINITIALIZED = 0;
private static final byte STAGE_INITIALIZED = 1;
@SuppressWarnings("Immutable")
private transient volatile InitShim initShim = new InitShim();
private final class InitShim {
private byte compactSourceBuildStage = STAGE_UNINITIALIZED;
private CharSequence compactSource;
CharSequence getCompactSource() {
if (compactSourceBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (compactSourceBuildStage == STAGE_UNINITIALIZED) {
compactSourceBuildStage = STAGE_INITIALIZING;
this.compactSource = ImmutableBulkOperation.super.getCompactSource();
compactSourceBuildStage = STAGE_INITIALIZED;
}
return this.compactSource;
}
private byte operationBuildStage = STAGE_UNINITIALIZED;
private CharSequence operation;
CharSequence getOperation() {
if (operationBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (operationBuildStage == STAGE_UNINITIALIZED) {
operationBuildStage = STAGE_INITIALIZING;
this.operation = Objects.requireNonNull(ImmutableBulkOperation.super.getOperation(), "operation");
operationBuildStage = STAGE_INITIALIZED;
}
return this.operation;
}
private byte typeBuildStage = STAGE_UNINITIALIZED;
private String type;
String getType() {
if (typeBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (typeBuildStage == STAGE_UNINITIALIZED) {
typeBuildStage = STAGE_INITIALIZING;
this.type = Objects.requireNonNull(ImmutableBulkOperation.super.getType(), "type");
typeBuildStage = STAGE_INITIALIZED;
}
return this.type;
}
void type(String type) {
this.type = type;
typeBuildStage = STAGE_INITIALIZED;
}
private byte versionTypeBuildStage = STAGE_UNINITIALIZED;
private BulkOperation.VersionType versionType;
BulkOperation.VersionType getVersionType() {
if (versionTypeBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (versionTypeBuildStage == STAGE_UNINITIALIZED) {
versionTypeBuildStage = STAGE_INITIALIZING;
this.versionType = ImmutableBulkOperation.super.getVersionType();
versionTypeBuildStage = STAGE_INITIALIZED;
}
return this.versionType;
}
void versionType(BulkOperation.VersionType versionType) {
this.versionType = versionType;
versionTypeBuildStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
List attributes = new ArrayList<>();
if (compactSourceBuildStage == STAGE_INITIALIZING) attributes.add("compactSource");
if (operationBuildStage == STAGE_INITIALIZING) attributes.add("operation");
if (typeBuildStage == STAGE_INITIALIZING) attributes.add("type");
if (versionTypeBuildStage == STAGE_INITIALIZING) attributes.add("versionType");
return "Cannot build BulkOperation, attribute initializers form cycle " + attributes;
}
}
/**
* Returns mapping type within index.
* @return mapping type within index.
*/
@Override
public BulkOperation.Action getAction() {
return action;
}
/**
* @return The computed-at-construction value of the {@code compactSource} attribute
*/
@Override
public @Nullable CharSequence getCompactSource() {
InitShim shim = this.initShim;
return shim != null
? shim.getCompactSource()
: this.compactSource;
}
/**
* Returns the id of the document.
* @return id of the document.
*/
@Override
public String getId() {
return id;
}
/**
* Returns index that this document will be indexed into.
* @return index that this document will be indexed into.
*/
@Override
public Index getIndex() {
return index;
}
/**
* @return The value of the {@code listener} attribute
*/
@Override
public @Nullable BulkOperationListener getListener() {
return listener;
}
/**
* @return The computed-at-construction value of the {@code operation} attribute
*/
@Override
public CharSequence getOperation() {
InitShim shim = this.initShim;
return shim != null
? shim.getOperation()
: this.operation;
}
/**
* Returns source JSON document that will be indexed by {@link BulkIndexer}
* @return source JSON document that will be indexed by {@link BulkIndexer}
*/
@Override
public @Nullable CharSequence getSource() {
return source;
}
/**
* Returns mapping type within index.
* @return mapping type within index.
*/
@Override
public String getType() {
InitShim shim = this.initShim;
return shim != null
? shim.getType()
: this.type;
}
/**
* @return The value of the {@code version} attribute
*/
@Override
public @Nullable Long getVersion() {
return version;
}
/**
* @return The value of the {@code versionType} attribute
*/
@Override
public @Nullable BulkOperation.VersionType getVersionType() {
InitShim shim = this.initShim;
return shim != null
? shim.getVersionType()
: this.versionType;
}
/**
* This instance is equal to all instances of {@code ImmutableBulkOperation} that have equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(@javax.annotation.Nullable Object another) {
if (this == another) return true;
return another instanceof ImmutableBulkOperation
&& equalTo((ImmutableBulkOperation) another);
}
private boolean equalTo(ImmutableBulkOperation another) {
return action.equals(another.action)
&& id.equals(another.id)
&& index.equals(another.index)
&& type.equals(another.type)
&& Objects.equals(version, another.version)
&& Objects.equals(versionType, another.versionType);
}
/**
* Computes a hash code from attributes: {@code action}, {@code id}, {@code index}, {@code type}, {@code version}, {@code versionType}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + action.hashCode();
h += (h << 5) + id.hashCode();
h += (h << 5) + index.hashCode();
h += (h << 5) + type.hashCode();
h += (h << 5) + Objects.hashCode(version);
h += (h << 5) + Objects.hashCode(versionType);
return h;
}
/**
* Prints the immutable value {@code BulkOperation} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("BulkOperation")
.omitNullValues()
.add("action", action)
.add("id", id)
.add("index", index)
.add("type", type)
.add("version", version)
.add("versionType", versionType)
.toString();
}
private static ImmutableBulkOperation validate(ImmutableBulkOperation instance) {
instance.checkSource();
return instance;
}
/**
* Creates a builder for {@link ImmutableBulkOperation ImmutableBulkOperation}.
* @return A new ImmutableBulkOperation builder
*/
public static ImmutableBulkOperation.Builder builder() {
return new ImmutableBulkOperation.Builder();
}
/**
* Builds instances of type {@link ImmutableBulkOperation ImmutableBulkOperation}.
* Initialize attributes and then invoke the {@link #build()} method to create an
* immutable instance.
* {@code Builder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.
*/
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_ACTION = 0x1L;
private static final long INIT_BIT_ID = 0x2L;
private static final long INIT_BIT_INDEX = 0x4L;
private static final long OPT_BIT_VERSION_TYPE = 0x1L;
private long initBits = 0x7L;
private long optBits;
private @javax.annotation.Nullable BulkOperation.Action action;
private @javax.annotation.Nullable String id;
private @javax.annotation.Nullable Index index;
private @javax.annotation.Nullable BulkOperationListener listener;
private @javax.annotation.Nullable CharSequence source;
private @javax.annotation.Nullable String type;
private @javax.annotation.Nullable Long version;
private @javax.annotation.Nullable BulkOperation.VersionType versionType;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code BulkOperation} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(BulkOperation instance) {
Objects.requireNonNull(instance, "instance");
action(instance.getAction());
id(instance.getId());
index(instance.getIndex());
BulkOperationListener listenerValue = instance.getListener();
if (listenerValue != null) {
listener(listenerValue);
}
CharSequence sourceValue = instance.getSource();
if (sourceValue != null) {
source(sourceValue);
}
type(instance.getType());
Long versionValue = instance.getVersion();
if (versionValue != null) {
version(versionValue);
}
BulkOperation.VersionType versionTypeValue = instance.getVersionType();
if (versionTypeValue != null) {
versionType(versionTypeValue);
}
return this;
}
/**
* Initializes the value for the {@link BulkOperation#getAction() action} attribute.
* @param action The value for action
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder action(BulkOperation.Action action) {
this.action = Objects.requireNonNull(action, "action");
initBits &= ~INIT_BIT_ACTION;
return this;
}
/**
* Initializes the value for the {@link BulkOperation#getId() id} attribute.
* @param id The value for id
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder id(String id) {
this.id = Objects.requireNonNull(id, "id");
initBits &= ~INIT_BIT_ID;
return this;
}
/**
* Initializes the value for the {@link BulkOperation#getIndex() index} attribute.
* @param index The value for index
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder index(Index index) {
this.index = Objects.requireNonNull(index, "index");
initBits &= ~INIT_BIT_INDEX;
return this;
}
/**
* Initializes the value for the {@link BulkOperation#getListener() listener} attribute.
* @param listener The value for listener (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder listener(@Nullable BulkOperationListener listener) {
this.listener = listener;
return this;
}
/**
* Initializes the value for the {@link BulkOperation#getSource() source} attribute.
* @param source The value for source (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder source(@Nullable CharSequence source) {
this.source = source;
return this;
}
/**
* Initializes the value for the {@link BulkOperation#getType() type} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link BulkOperation#getType() type}.
* @param type The value for type
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder type(String type) {
this.type = Objects.requireNonNull(type, "type");
return this;
}
/**
* Initializes the value for the {@link BulkOperation#getVersion() version} attribute.
* @param version The value for version (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder version(@Nullable Long version) {
this.version = version;
return this;
}
/**
* Initializes the value for the {@link BulkOperation#getVersionType() versionType} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link BulkOperation#getVersionType() versionType}.
* @param versionType The value for versionType (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder versionType(@Nullable BulkOperation.VersionType versionType) {
this.versionType = versionType;
optBits |= OPT_BIT_VERSION_TYPE;
return this;
}
/**
* Builds a new {@link ImmutableBulkOperation ImmutableBulkOperation}.
* @return An immutable instance of BulkOperation
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableBulkOperation build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return ImmutableBulkOperation.validate(new ImmutableBulkOperation(this));
}
private boolean versionTypeIsSet() {
return (optBits & OPT_BIT_VERSION_TYPE) != 0;
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_ACTION) != 0) attributes.add("action");
if ((initBits & INIT_BIT_ID) != 0) attributes.add("id");
if ((initBits & INIT_BIT_INDEX) != 0) attributes.add("index");
return "Cannot build BulkOperation, some of required attributes are not set " + attributes;
}
}
}