com.arakelian.elastic.model.ImmutableIndex Maven / Gradle / Ivy
package com.arakelian.elastic.model;
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.io.ObjectStreamException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link Index}.
*
* Use the builder to create immutable instances:
* {@code ImmutableIndex.builder()}.
*/
@Generated(from = "Index", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableIndex implements Index {
private final ImmutableMap mappings;
private final String name;
private final IndexSettings settings;
private ImmutableIndex(ImmutableIndex.Builder builder) {
this.mappings = builder.mappings.build();
this.name = builder.name;
this.settings = builder.settings != null
? builder.settings
: Objects.requireNonNull(Index.super.getSettings(), "settings");
}
/**
* @return The value of the {@code mappings} attribute
*/
@JsonProperty("mappings")
@Override
public ImmutableMap getMappings() {
return mappings;
}
/**
* Returns the index name
* @return name of index
*/
@JsonProperty("name")
@Override
public String getName() {
return name;
}
/**
* @return The value of the {@code settings} attribute
*/
@JsonProperty("settings")
@Override
public IndexSettings getSettings() {
return settings;
}
/**
* This instance is equal to all instances of {@code ImmutableIndex} that have 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);
}
/**
* Computes a hash code from attributes: {@code name}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + name.hashCode();
return h;
}
/**
* Prints the immutable value {@code Index} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("Index")
.omitNullValues()
.add("name", name)
.toString();
}
private static ImmutableIndex validate(ImmutableIndex instance) {
instance.checkMappings();
return instance;
}
private Object readResolve() throws ObjectStreamException {
return validate(this);
}
/**
* Creates a builder for {@link ImmutableIndex ImmutableIndex}.
*
* ImmutableIndex.builder()
* .putMapping|putAllMappings(String => com.arakelian.elastic.model.Mapping) // {@link Index#getMappings() mappings} mappings
* .name(String) // required {@link Index#getName() name}
* .settings(com.arakelian.elastic.model.IndexSettings) // optional {@link Index#getSettings() settings}
* .build();
*
* @return A new ImmutableIndex builder
*/
public static ImmutableIndex.Builder builder() {
return new ImmutableIndex.Builder();
}
/**
* Builds instances of type {@link ImmutableIndex ImmutableIndex}.
* 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.
*/
@Generated(from = "Index", generator = "Immutables")
@NotThreadSafe
@JsonPropertyOrder({"name", "settings", "mappings"})
public static final class Builder {
private static final long INIT_BIT_NAME = 0x1L;
private long initBits = 0x1L;
private ImmutableMap.Builder mappings = ImmutableMap.builder();
private @Nullable String name;
private @Nullable IndexSettings settings;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Index} 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(Index instance) {
Objects.requireNonNull(instance, "instance");
putAllMappings(instance.getMappings());
name(instance.getName());
settings(instance.getSettings());
return this;
}
/**
* Put one entry to the {@link Index#getMappings() mappings} map.
* @param key The key in the mappings map
* @param value The associated value in the mappings map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putMapping(String key, Mapping value) {
this.mappings.put(key, value);
return this;
}
/**
* Put one entry to the {@link Index#getMappings() mappings} 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 putMapping(Map.Entry entry) {
this.mappings.put(entry);
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link Index#getMappings() mappings} map. Nulls are not permitted
* @param entries The entries that will be added to the mappings map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("mappings")
public final Builder mappings(Map entries) {
this.mappings = ImmutableMap.builder();
return putAllMappings(entries);
}
/**
* Put all mappings from the specified map as entries to {@link Index#getMappings() mappings} map. Nulls are not permitted
* @param entries The entries that will be added to the mappings map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putAllMappings(Map entries) {
this.mappings.putAll(entries);
return this;
}
/**
* Initializes the value for the {@link Index#getName() name} attribute.
* @param name The value for name
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("name")
public final Builder name(String name) {
this.name = Objects.requireNonNull(name, "name");
initBits &= ~INIT_BIT_NAME;
return this;
}
/**
* Initializes the value for the {@link Index#getSettings() settings} attribute.
* If not set, this attribute will have a default value as returned by the initializer of {@link Index#getSettings() settings}.
* @param settings The value for settings
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("settings")
public final Builder settings(IndexSettings settings) {
this.settings = Objects.requireNonNull(settings, "settings");
return this;
}
/**
* Builds a new {@link ImmutableIndex ImmutableIndex}.
* @return An immutable instance of Index
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableIndex build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return ImmutableIndex.validate(new ImmutableIndex(this));
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name");
return "Cannot build Index, some of required attributes are not set " + attributes;
}
}
}