com.arakelian.elastic.model.aggs.bucket.ImmutableNestedAggregation Maven / Gradle / Ivy
package com.arakelian.elastic.model.aggs.bucket;
import com.arakelian.elastic.model.aggs.Aggregation;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
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.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link NestedAggregation}.
*
* Use the builder to create immutable instances:
* {@code ImmutableNestedAggregation.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableNestedAggregation
implements NestedAggregation {
private final String path;
private final String name;
private final ImmutableList subAggregations;
private final boolean empty;
private ImmutableNestedAggregation(ImmutableNestedAggregation.Builder builder) {
this.path = builder.path;
this.name = builder.name;
if (builder.subAggregationsIsSet()) {
initShim.subAggregations(builder.subAggregations.build());
}
this.subAggregations = initShim.getSubAggregations();
this.empty = initShim.isEmpty();
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 subAggregationsBuildStage = STAGE_UNINITIALIZED;
private ImmutableList subAggregations;
ImmutableList getSubAggregations() {
if (subAggregationsBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (subAggregationsBuildStage == STAGE_UNINITIALIZED) {
subAggregationsBuildStage = STAGE_INITIALIZING;
this.subAggregations = ImmutableList.copyOf(getSubAggregationsInitialize());
subAggregationsBuildStage = STAGE_INITIALIZED;
}
return this.subAggregations;
}
void subAggregations(ImmutableList subAggregations) {
this.subAggregations = subAggregations;
subAggregationsBuildStage = STAGE_INITIALIZED;
}
private byte emptyBuildStage = STAGE_UNINITIALIZED;
private boolean empty;
boolean isEmpty() {
if (emptyBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (emptyBuildStage == STAGE_UNINITIALIZED) {
emptyBuildStage = STAGE_INITIALIZING;
this.empty = isEmptyInitialize();
emptyBuildStage = STAGE_INITIALIZED;
}
return this.empty;
}
private String formatInitCycleMessage() {
List attributes = new ArrayList<>();
if (subAggregationsBuildStage == STAGE_INITIALIZING) attributes.add("subAggregations");
if (emptyBuildStage == STAGE_INITIALIZING) attributes.add("empty");
return "Cannot build NestedAggregation, attribute initializers form cycle " + attributes;
}
}
private List getSubAggregationsInitialize() {
return NestedAggregation.super.getSubAggregations();
}
private boolean isEmptyInitialize() {
return NestedAggregation.super.isEmpty();
}
/**
* @return The value of the {@code path} attribute
*/
@JsonProperty("path")
@Override
public String getPath() {
return path;
}
/**
* Returns the name of the aggregation
* @return name of the aggregation
*/
@JsonProperty("name")
@Override
public String getName() {
return name;
}
/**
* @return The value of the {@code subAggregations} attribute
*/
@JsonProperty("subAggregations")
@Override
public ImmutableList getSubAggregations() {
InitShim shim = this.initShim;
return shim != null
? shim.getSubAggregations()
: this.subAggregations;
}
/**
* @return The computed-at-construction value of the {@code empty} attribute
*/
@JsonProperty("empty")
@JsonIgnore
@Override
public boolean isEmpty() {
InitShim shim = this.initShim;
return shim != null
? shim.isEmpty()
: this.empty;
}
/**
* This instance is equal to all instances of {@code ImmutableNestedAggregation} 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 ImmutableNestedAggregation
&& equalTo((ImmutableNestedAggregation) another);
}
private boolean equalTo(ImmutableNestedAggregation another) {
return path.equals(another.path)
&& name.equals(another.name)
&& subAggregations.equals(another.subAggregations);
}
/**
* Computes a hash code from attributes: {@code path}, {@code name}, {@code subAggregations}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + path.hashCode();
h += (h << 5) + name.hashCode();
h += (h << 5) + subAggregations.hashCode();
return h;
}
/**
* Prints the immutable value {@code NestedAggregation} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("NestedAggregation")
.omitNullValues()
.add("path", path)
.add("name", name)
.add("subAggregations", subAggregations)
.toString();
}
/**
* Creates a builder for {@link ImmutableNestedAggregation ImmutableNestedAggregation}.
* @return A new ImmutableNestedAggregation builder
*/
public static ImmutableNestedAggregation.Builder builder() {
return new ImmutableNestedAggregation.Builder();
}
/**
* Builds instances of type {@link ImmutableNestedAggregation ImmutableNestedAggregation}.
* 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
@JsonTypeName("nested")
public static final class Builder {
private static final long INIT_BIT_PATH = 0x1L;
private static final long INIT_BIT_NAME = 0x2L;
private static final long OPT_BIT_SUB_AGGREGATIONS = 0x1L;
private long initBits = 0x3L;
private long optBits;
private @Nullable String path;
private @Nullable String name;
private ImmutableList.Builder subAggregations = ImmutableList.builder();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code com.arakelian.elastic.model.aggs.bucket.NestedAggregation} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(NestedAggregation instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code com.arakelian.elastic.model.aggs.Aggregation} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(Aggregation instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
if (object instanceof NestedAggregation) {
NestedAggregation instance = (NestedAggregation) object;
path(instance.getPath());
}
if (object instanceof Aggregation) {
Aggregation instance = (Aggregation) object;
name(instance.getName());
addAllSubAggregations(instance.getSubAggregations());
}
}
/**
* Initializes the value for the {@link NestedAggregation#getPath() path} attribute.
* @param path The value for path
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("path")
public final Builder path(String path) {
this.path = Objects.requireNonNull(path, "path");
initBits &= ~INIT_BIT_PATH;
return this;
}
/**
* Initializes the value for the {@link NestedAggregation#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;
}
/**
* Adds one element to {@link NestedAggregation#getSubAggregations() subAggregations} list.
* @param element A subAggregations element
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addSubAggregation(Aggregation element) {
this.subAggregations.add(element);
optBits |= OPT_BIT_SUB_AGGREGATIONS;
return this;
}
/**
* Adds elements to {@link NestedAggregation#getSubAggregations() subAggregations} list.
* @param elements An array of subAggregations elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addSubAggregations(Aggregation... elements) {
this.subAggregations.add(elements);
optBits |= OPT_BIT_SUB_AGGREGATIONS;
return this;
}
/**
* Sets or replaces all elements for {@link NestedAggregation#getSubAggregations() subAggregations} list.
* @param elements An iterable of subAggregations elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("subAggregations")
public final Builder subAggregations(Iterable extends Aggregation> elements) {
this.subAggregations = ImmutableList.builder();
return addAllSubAggregations(elements);
}
/**
* Adds elements to {@link NestedAggregation#getSubAggregations() subAggregations} list.
* @param elements An iterable of subAggregations elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addAllSubAggregations(Iterable extends Aggregation> elements) {
this.subAggregations.addAll(elements);
optBits |= OPT_BIT_SUB_AGGREGATIONS;
return this;
}
/**
* Builds a new {@link ImmutableNestedAggregation ImmutableNestedAggregation}.
* @return An immutable instance of NestedAggregation
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableNestedAggregation build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableNestedAggregation(this);
}
private boolean subAggregationsIsSet() {
return (optBits & OPT_BIT_SUB_AGGREGATIONS) != 0;
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_PATH) != 0) attributes.add("path");
if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name");
return "Cannot build NestedAggregation, some of required attributes are not set " + attributes;
}
}
}