com.arakelian.elastic.model.search.ImmutableSearchHits Maven / Gradle / Ivy
package com.arakelian.elastic.model.search;
import com.arakelian.core.feature.Nullable;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Longs;
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.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link SearchHits}.
*
* Use the builder to create immutable instances:
* {@code ImmutableSearchHits.builder()}.
*/
@Generated(from = "SearchHits", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableSearchHits extends SearchHits {
private final ImmutableList hits;
private final @Nullable Float maxScore;
private final int size;
private final long total;
private ImmutableSearchHits(ImmutableSearchHits.Builder builder) {
this.maxScore = builder.maxScore;
if (builder.hitsIsSet()) {
initShim.hits(builder.hits.build());
}
if (builder.totalIsSet()) {
initShim.total(builder.total);
}
this.hits = initShim.getHits();
this.size = initShim.getSize();
this.total = initShim.getTotal();
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();
@Generated(from = "SearchHits", generator = "Immutables")
private final class InitShim {
private byte hitsBuildStage = STAGE_UNINITIALIZED;
private ImmutableList hits;
ImmutableList getHits() {
if (hitsBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (hitsBuildStage == STAGE_UNINITIALIZED) {
hitsBuildStage = STAGE_INITIALIZING;
this.hits = ImmutableList.copyOf(ImmutableSearchHits.super.getHits());
hitsBuildStage = STAGE_INITIALIZED;
}
return this.hits;
}
void hits(ImmutableList hits) {
this.hits = hits;
hitsBuildStage = STAGE_INITIALIZED;
}
private byte sizeBuildStage = STAGE_UNINITIALIZED;
private int size;
int getSize() {
if (sizeBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (sizeBuildStage == STAGE_UNINITIALIZED) {
sizeBuildStage = STAGE_INITIALIZING;
this.size = ImmutableSearchHits.super.getSize();
sizeBuildStage = STAGE_INITIALIZED;
}
return this.size;
}
private byte totalBuildStage = STAGE_UNINITIALIZED;
private long total;
long getTotal() {
if (totalBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (totalBuildStage == STAGE_UNINITIALIZED) {
totalBuildStage = STAGE_INITIALIZING;
this.total = ImmutableSearchHits.super.getTotal();
totalBuildStage = STAGE_INITIALIZED;
}
return this.total;
}
void total(long total) {
this.total = total;
totalBuildStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
List attributes = new ArrayList<>();
if (hitsBuildStage == STAGE_INITIALIZING) attributes.add("hits");
if (sizeBuildStage == STAGE_INITIALIZING) attributes.add("size");
if (totalBuildStage == STAGE_INITIALIZING) attributes.add("total");
return "Cannot build SearchHits, attribute initializers form cycle " + attributes;
}
}
/**
* @return The value of the {@code hits} attribute
*/
@JsonProperty("hits")
@Override
public ImmutableList getHits() {
InitShim shim = this.initShim;
return shim != null
? shim.getHits()
: this.hits;
}
/**
* @return The value of the {@code maxScore} attribute
*/
@JsonProperty("max_score")
@Override
public @Nullable Float getMaxScore() {
return maxScore;
}
/**
* @return The computed-at-construction value of the {@code size} attribute
*/
@JsonProperty("size")
@JsonIgnore
@Override
public int getSize() {
InitShim shim = this.initShim;
return shim != null
? shim.getSize()
: this.size;
}
/**
* @return The value of the {@code total} attribute
*/
@JsonProperty("total")
@Override
public long getTotal() {
InitShim shim = this.initShim;
return shim != null
? shim.getTotal()
: this.total;
}
/**
* This instance is equal to all instances of {@code ImmutableSearchHits} 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 ImmutableSearchHits
&& equalTo((ImmutableSearchHits) another);
}
private boolean equalTo(ImmutableSearchHits another) {
return hits.equals(another.hits)
&& Objects.equals(maxScore, another.maxScore)
&& size == another.size
&& total == another.total;
}
/**
* Computes a hash code from attributes: {@code hits}, {@code maxScore}, {@code size}, {@code total}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + hits.hashCode();
h += (h << 5) + Objects.hashCode(maxScore);
h += (h << 5) + size;
h += (h << 5) + Longs.hashCode(total);
return h;
}
/**
* Prints the immutable value {@code SearchHits} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("SearchHits")
.omitNullValues()
.add("hits", hits)
.add("maxScore", maxScore)
.add("size", size)
.add("total", total)
.toString();
}
/**
* Creates a builder for {@link ImmutableSearchHits ImmutableSearchHits}.
* @return A new ImmutableSearchHits builder
*/
public static ImmutableSearchHits.Builder builder() {
return new ImmutableSearchHits.Builder();
}
/**
* Builds instances of type {@link ImmutableSearchHits ImmutableSearchHits}.
* 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 = "SearchHits", generator = "Immutables")
@NotThreadSafe
@JsonPropertyOrder({"total", "max_score", "hits"})
public static final class Builder {
private static final long OPT_BIT_HITS = 0x1L;
private static final long OPT_BIT_TOTAL = 0x2L;
private long optBits;
private ImmutableList.Builder hits = ImmutableList.builder();
private @javax.annotation.Nullable Float maxScore;
private long total;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code SearchHits} 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(SearchHits instance) {
Objects.requireNonNull(instance, "instance");
addAllHits(instance.getHits());
Float maxScoreValue = instance.getMaxScore();
if (maxScoreValue != null) {
maxScore(maxScoreValue);
}
total(instance.getTotal());
return this;
}
/**
* Adds one element to {@link SearchHits#getHits() hits} list.
* @param element A hits element
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addHit(SearchHit element) {
this.hits.add(element);
optBits |= OPT_BIT_HITS;
return this;
}
/**
* Adds elements to {@link SearchHits#getHits() hits} list.
* @param elements An array of hits elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addHits(SearchHit... elements) {
this.hits.add(elements);
optBits |= OPT_BIT_HITS;
return this;
}
/**
* Sets or replaces all elements for {@link SearchHits#getHits() hits} list.
* @param elements An iterable of hits elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("hits")
public final Builder hits(Iterable extends SearchHit> elements) {
this.hits = ImmutableList.builder();
return addAllHits(elements);
}
/**
* Adds elements to {@link SearchHits#getHits() hits} list.
* @param elements An iterable of hits elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addAllHits(Iterable extends SearchHit> elements) {
this.hits.addAll(elements);
optBits |= OPT_BIT_HITS;
return this;
}
/**
* Initializes the value for the {@link SearchHits#getMaxScore() maxScore} attribute.
* @param maxScore The value for maxScore (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("max_score")
public final Builder maxScore(@Nullable Float maxScore) {
this.maxScore = maxScore;
return this;
}
/**
* Initializes the value for the {@link SearchHits#getTotal() total} attribute.
* If not set, this attribute will have a default value as returned by the initializer of {@link SearchHits#getTotal() total}.
* @param total The value for total
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("total")
public final Builder total(long total) {
this.total = total;
optBits |= OPT_BIT_TOTAL;
return this;
}
/**
* Builds a new {@link ImmutableSearchHits ImmutableSearchHits}.
* @return An immutable instance of SearchHits
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableSearchHits build() {
return new ImmutableSearchHits(this);
}
private boolean hitsIsSet() {
return (optBits & OPT_BIT_HITS) != 0;
}
private boolean totalIsSet() {
return (optBits & OPT_BIT_TOTAL) != 0;
}
}
}