com.arakelian.elastic.model.ImmutableIndexSettings Maven / Gradle / Ivy
package com.arakelian.elastic.model;
import com.arakelian.core.feature.Nullable;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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 IndexSettings}.
*
* Use the builder to create immutable instances:
* {@code ImmutableIndexSettings.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableIndexSettings implements IndexSettings {
private final @Nullable IndexSettings.Analysis analysis;
private final int numberOfReplicas;
private final int numberOfShards;
private final ImmutableMap properties;
private ImmutableIndexSettings(ImmutableIndexSettings.Builder builder) {
this.analysis = builder.analysis;
if (builder.numberOfReplicasIsSet()) {
initShim.numberOfReplicas(builder.numberOfReplicas);
}
if (builder.numberOfShardsIsSet()) {
initShim.numberOfShards(builder.numberOfShards);
}
if (builder.propertiesIsSet()) {
initShim.properties(builder.properties.build());
}
this.numberOfReplicas = initShim.getNumberOfReplicas();
this.numberOfShards = initShim.getNumberOfShards();
this.properties = initShim.getProperties();
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 numberOfReplicasBuildStage = STAGE_UNINITIALIZED;
private int numberOfReplicas;
int getNumberOfReplicas() {
if (numberOfReplicasBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (numberOfReplicasBuildStage == STAGE_UNINITIALIZED) {
numberOfReplicasBuildStage = STAGE_INITIALIZING;
this.numberOfReplicas = getNumberOfReplicasInitialize();
numberOfReplicasBuildStage = STAGE_INITIALIZED;
}
return this.numberOfReplicas;
}
void numberOfReplicas(int numberOfReplicas) {
this.numberOfReplicas = numberOfReplicas;
numberOfReplicasBuildStage = STAGE_INITIALIZED;
}
private byte numberOfShardsBuildStage = STAGE_UNINITIALIZED;
private int numberOfShards;
int getNumberOfShards() {
if (numberOfShardsBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (numberOfShardsBuildStage == STAGE_UNINITIALIZED) {
numberOfShardsBuildStage = STAGE_INITIALIZING;
this.numberOfShards = getNumberOfShardsInitialize();
numberOfShardsBuildStage = STAGE_INITIALIZED;
}
return this.numberOfShards;
}
void numberOfShards(int numberOfShards) {
this.numberOfShards = numberOfShards;
numberOfShardsBuildStage = STAGE_INITIALIZED;
}
private byte propertiesBuildStage = STAGE_UNINITIALIZED;
private ImmutableMap properties;
ImmutableMap getProperties() {
if (propertiesBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (propertiesBuildStage == STAGE_UNINITIALIZED) {
propertiesBuildStage = STAGE_INITIALIZING;
this.properties = ImmutableMap.copyOf(getPropertiesInitialize());
propertiesBuildStage = STAGE_INITIALIZED;
}
return this.properties;
}
void properties(ImmutableMap properties) {
this.properties = properties;
propertiesBuildStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
List attributes = new ArrayList<>();
if (numberOfReplicasBuildStage == STAGE_INITIALIZING) attributes.add("numberOfReplicas");
if (numberOfShardsBuildStage == STAGE_INITIALIZING) attributes.add("numberOfShards");
if (propertiesBuildStage == STAGE_INITIALIZING) attributes.add("properties");
return "Cannot build IndexSettings, attribute initializers form cycle " + attributes;
}
}
private int getNumberOfReplicasInitialize() {
return IndexSettings.super.getNumberOfReplicas();
}
private int getNumberOfShardsInitialize() {
return IndexSettings.super.getNumberOfShards();
}
private Map getPropertiesInitialize() {
return IndexSettings.super.getProperties();
}
/**
* @return The value of the {@code analysis} attribute
*/
@JsonProperty("analysis")
@Override
public @Nullable IndexSettings.Analysis getAnalysis() {
return analysis;
}
/**
* @return The value of the {@code numberOfReplicas} attribute
*/
@JsonProperty("number_of_replicas")
@Override
public int getNumberOfReplicas() {
InitShim shim = this.initShim;
return shim != null
? shim.getNumberOfReplicas()
: this.numberOfReplicas;
}
/**
* @return The value of the {@code numberOfShards} attribute
*/
@JsonProperty("number_of_shards")
@Override
public int getNumberOfShards() {
InitShim shim = this.initShim;
return shim != null
? shim.getNumberOfShards()
: this.numberOfShards;
}
/**
* @return The value of the {@code properties} attribute
*/
@JsonProperty("properties")
@JsonAnyGetter
@Override
public ImmutableMap getProperties() {
InitShim shim = this.initShim;
return shim != null
? shim.getProperties()
: this.properties;
}
/**
* This instance is equal to all instances of {@code ImmutableIndexSettings} 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 ImmutableIndexSettings
&& equalTo((ImmutableIndexSettings) another);
}
private boolean equalTo(ImmutableIndexSettings another) {
return Objects.equals(analysis, another.analysis)
&& numberOfReplicas == another.numberOfReplicas
&& numberOfShards == another.numberOfShards
&& properties.equals(another.properties);
}
/**
* Computes a hash code from attributes: {@code analysis}, {@code numberOfReplicas}, {@code numberOfShards}, {@code properties}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + Objects.hashCode(analysis);
h += (h << 5) + numberOfReplicas;
h += (h << 5) + numberOfShards;
h += (h << 5) + properties.hashCode();
return h;
}
/**
* Prints the immutable value {@code IndexSettings} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("IndexSettings")
.omitNullValues()
.add("analysis", analysis)
.add("numberOfReplicas", numberOfReplicas)
.add("numberOfShards", numberOfShards)
.add("properties", properties)
.toString();
}
/**
* Creates a builder for {@link ImmutableIndexSettings ImmutableIndexSettings}.
* @return A new ImmutableIndexSettings builder
*/
public static ImmutableIndexSettings.Builder builder() {
return new ImmutableIndexSettings.Builder();
}
/**
* Builds instances of type {@link ImmutableIndexSettings ImmutableIndexSettings}.
* 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
@JsonPropertyOrder({"number_of_replicas", "number_of_shards", "analysis"})
public static final class Builder {
private static final long OPT_BIT_NUMBER_OF_REPLICAS = 0x1L;
private static final long OPT_BIT_NUMBER_OF_SHARDS = 0x2L;
private static final long OPT_BIT_PROPERTIES = 0x4L;
private long optBits;
private @javax.annotation.Nullable IndexSettings.Analysis analysis;
private int numberOfReplicas;
private int numberOfShards;
private ImmutableMap.Builder properties = ImmutableMap.builder();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code IndexSettings} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* Collection elements and entries will be added, not replaced.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(IndexSettings instance) {
Objects.requireNonNull(instance, "instance");
IndexSettings.Analysis analysisValue = instance.getAnalysis();
if (analysisValue != null) {
analysis(analysisValue);
}
numberOfReplicas(instance.getNumberOfReplicas());
numberOfShards(instance.getNumberOfShards());
putAllProperties(instance.getProperties());
return this;
}
/**
* Initializes the value for the {@link IndexSettings#getAnalysis() analysis} attribute.
* @param analysis The value for analysis (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("analysis")
public final Builder analysis(@Nullable IndexSettings.Analysis analysis) {
this.analysis = analysis;
return this;
}
/**
* Initializes the value for the {@link IndexSettings#getNumberOfReplicas() numberOfReplicas} attribute.
* If not set, this attribute will have a default value as returned by the initializer of {@link IndexSettings#getNumberOfReplicas() numberOfReplicas}.
* @param numberOfReplicas The value for numberOfReplicas
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("number_of_replicas")
public final Builder numberOfReplicas(int numberOfReplicas) {
this.numberOfReplicas = numberOfReplicas;
optBits |= OPT_BIT_NUMBER_OF_REPLICAS;
return this;
}
/**
* Initializes the value for the {@link IndexSettings#getNumberOfShards() numberOfShards} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link IndexSettings#getNumberOfShards() numberOfShards}.
* @param numberOfShards The value for numberOfShards
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("number_of_shards")
public final Builder numberOfShards(int numberOfShards) {
this.numberOfShards = numberOfShards;
optBits |= OPT_BIT_NUMBER_OF_SHARDS;
return this;
}
/**
* Put one entry to the {@link IndexSettings#getProperties() properties} map.
* @param key The key in the properties map
* @param value The associated value in the properties map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonAnySetter
public final Builder putProperty(String key, Object value) {
this.properties.put(key, value);
optBits |= OPT_BIT_PROPERTIES;
return this;
}
/**
* Put one entry to the {@link IndexSettings#getProperties() properties} map. Nulls are not permitted
* @param entry The key and value entry
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putProperty(Map.Entry entry) {
this.properties.put(entry);
optBits |= OPT_BIT_PROPERTIES;
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link IndexSettings#getProperties() properties} map. Nulls are not permitted
* @param entries The entries that will be added to the properties map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("properties")
public final Builder properties(Map entries) {
this.properties = ImmutableMap.builder();
optBits |= OPT_BIT_PROPERTIES;
return putAllProperties(entries);
}
/**
* Put all mappings from the specified map as entries to {@link IndexSettings#getProperties() properties} map. Nulls are not permitted
* @param entries The entries that will be added to the properties map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putAllProperties(Map entries) {
this.properties.putAll(entries);
optBits |= OPT_BIT_PROPERTIES;
return this;
}
/**
* Builds a new {@link ImmutableIndexSettings ImmutableIndexSettings}.
* @return An immutable instance of IndexSettings
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableIndexSettings build() {
return new ImmutableIndexSettings(this);
}
private boolean numberOfReplicasIsSet() {
return (optBits & OPT_BIT_NUMBER_OF_REPLICAS) != 0;
}
private boolean numberOfShardsIsSet() {
return (optBits & OPT_BIT_NUMBER_OF_SHARDS) != 0;
}
private boolean propertiesIsSet() {
return (optBits & OPT_BIT_PROPERTIES) != 0;
}
}
}