All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arakelian.elastic.model.search.ImmutableSearchHit Maven / Gradle / Ivy

package com.arakelian.elastic.model.search;

import com.arakelian.core.feature.Nullable;
import com.arakelian.jackson.AbstractMapPath;
import com.arakelian.jackson.MapPath;
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.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Ordering;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
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 SearchHit}.
 * 

* Use the builder to create immutable instances: * {@code ImmutableSearchHit.builder()}. */ @Generated(from = "SearchHit", generator = "Immutables") @SuppressWarnings({"all"}) @ParametersAreNonnullByDefault @javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor") @Immutable @CheckReturnValue public final class ImmutableSearchHit extends SearchHit { private final ImmutableMap properties; private final MapPath highlight; private final String id; private final @Nullable String index; private final ImmutableSortedSet matchedQueries; private final @Nullable Double score; private final Source source; private final @Nullable String type; private ImmutableSearchHit(ImmutableSearchHit.Builder builder) { this.id = builder.id; this.index = builder.index; this.score = builder.score; this.type = builder.type; if (builder.propertiesIsSet()) { initShim.properties(builder.properties.build()); } if (builder.highlight != null) { initShim.highlight(builder.highlight); } if (builder.matchedQueriesIsSet()) { initShim.matchedQueries(builder.matchedQueries.build()); } if (builder.source != null) { initShim.source(builder.source); } this.properties = initShim.getProperties(); this.highlight = initShim.getHighlight(); this.matchedQueries = initShim.getMatchedQueries(); this.source = initShim.getSource(); 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 = "SearchHit", generator = "Immutables") private final class InitShim { 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(ImmutableSearchHit.super.getProperties()); propertiesBuildStage = STAGE_INITIALIZED; } return this.properties; } void properties(ImmutableMap properties) { this.properties = properties; propertiesBuildStage = STAGE_INITIALIZED; } private byte highlightBuildStage = STAGE_UNINITIALIZED; private MapPath highlight; MapPath getHighlight() { if (highlightBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (highlightBuildStage == STAGE_UNINITIALIZED) { highlightBuildStage = STAGE_INITIALIZING; this.highlight = Objects.requireNonNull(ImmutableSearchHit.super.getHighlight(), "highlight"); highlightBuildStage = STAGE_INITIALIZED; } return this.highlight; } void highlight(MapPath highlight) { this.highlight = highlight; highlightBuildStage = STAGE_INITIALIZED; } private byte matchedQueriesBuildStage = STAGE_UNINITIALIZED; private ImmutableSortedSet matchedQueries; ImmutableSortedSet getMatchedQueries() { if (matchedQueriesBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (matchedQueriesBuildStage == STAGE_UNINITIALIZED) { matchedQueriesBuildStage = STAGE_INITIALIZING; this.matchedQueries = ImmutableSortedSet.copyOf( Ordering.natural(), ImmutableSearchHit.super.getMatchedQueries()); matchedQueriesBuildStage = STAGE_INITIALIZED; } return this.matchedQueries; } void matchedQueries(ImmutableSortedSet matchedQueries) { this.matchedQueries = matchedQueries; matchedQueriesBuildStage = STAGE_INITIALIZED; } private byte sourceBuildStage = STAGE_UNINITIALIZED; private Source source; Source getSource() { if (sourceBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (sourceBuildStage == STAGE_UNINITIALIZED) { sourceBuildStage = STAGE_INITIALIZING; this.source = Objects.requireNonNull(ImmutableSearchHit.super.getSource(), "source"); sourceBuildStage = STAGE_INITIALIZED; } return this.source; } void source(Source source) { this.source = source; sourceBuildStage = STAGE_INITIALIZED; } private String formatInitCycleMessage() { List attributes = new ArrayList<>(); if (propertiesBuildStage == STAGE_INITIALIZING) attributes.add("properties"); if (highlightBuildStage == STAGE_INITIALIZING) attributes.add("highlight"); if (matchedQueriesBuildStage == STAGE_INITIALIZING) attributes.add("matchedQueries"); if (sourceBuildStage == STAGE_INITIALIZING) attributes.add("source"); return "Cannot build SearchHit, attribute initializers form cycle " + attributes; } } /** * @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; } /** * @return The value of the {@code highlight} attribute */ @JsonProperty("highlight") @Override public MapPath getHighlight() { InitShim shim = this.initShim; return shim != null ? shim.getHighlight() : this.highlight; } /** * @return The value of the {@code id} attribute */ @JsonProperty("_id") @Override public String getId() { return id; } /** * @return The value of the {@code index} attribute */ @JsonProperty("_index") @Override public @Nullable String getIndex() { return index; } /** * @return The value of the {@code matchedQueries} attribute */ @JsonProperty("matched_queries") @Override public ImmutableSortedSet getMatchedQueries() { InitShim shim = this.initShim; return shim != null ? shim.getMatchedQueries() : this.matchedQueries; } /** * Returns the score of record. * Note that when custom sorting is used, Elastic will omit the scoring and return a null value. * @return the score of record, or null if scoring was not applied */ @JsonProperty("_score") @Override public @Nullable Double getScore() { return score; } /** * @return The value of the {@code source} attribute */ @JsonProperty("_source") @Override public Source getSource() { InitShim shim = this.initShim; return shim != null ? shim.getSource() : this.source; } /** * @return The value of the {@code type} attribute */ @JsonProperty("_type") @Override public @Nullable String getType() { return type; } /** * This instance is equal to all instances of {@code ImmutableSearchHit} 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 ImmutableSearchHit && equalTo((ImmutableSearchHit) another); } private boolean equalTo(ImmutableSearchHit another) { return properties.equals(another.properties) && highlight.equals(another.highlight) && id.equals(another.id) && Objects.equals(index, another.index) && matchedQueries.equals(another.matchedQueries) && Objects.equals(score, another.score) && source.equals(another.source) && Objects.equals(type, another.type); } /** * Computes a hash code from attributes: {@code properties}, {@code highlight}, {@code id}, {@code index}, {@code matchedQueries}, {@code score}, {@code source}, {@code type}. * @return hashCode value */ @Override public int hashCode() { @Var int h = 5381; h += (h << 5) + properties.hashCode(); h += (h << 5) + highlight.hashCode(); h += (h << 5) + id.hashCode(); h += (h << 5) + Objects.hashCode(index); h += (h << 5) + matchedQueries.hashCode(); h += (h << 5) + Objects.hashCode(score); h += (h << 5) + source.hashCode(); h += (h << 5) + Objects.hashCode(type); return h; } /** * Prints the immutable value {@code SearchHit} with attribute values. * @return A string representation of the value */ @Override public String toString() { return MoreObjects.toStringHelper("SearchHit") .omitNullValues() .add("properties", properties) .add("highlight", highlight) .add("id", id) .add("index", index) .add("matchedQueries", matchedQueries) .add("score", score) .add("source", source) .add("type", type) .toString(); } @SuppressWarnings("Immutable") private transient volatile long lazyInitBitmap; private static final long OBJECT_MAPPER_LAZY_INIT_BIT = 0x1L; @SuppressWarnings("Immutable") private transient ObjectMapper objectMapper; /** * {@inheritDoc} *

* Returns a lazily initialized value of the {@link SearchHit#getObjectMapper() objectMapper} attribute. * Initialized once and only once and stored for subsequent access with proper synchronization. * In case of any exception or error thrown by the lazy value initializer, * the result will not be memoised (i.e. remembered) and on next call computation * will be attempted again. * @return A lazily initialized value of the {@code objectMapper} attribute */ @Override public ObjectMapper getObjectMapper() { if ((lazyInitBitmap & OBJECT_MAPPER_LAZY_INIT_BIT) == 0) { synchronized (this) { if ((lazyInitBitmap & OBJECT_MAPPER_LAZY_INIT_BIT) == 0) { this.objectMapper = Objects.requireNonNull(super.getObjectMapper(), "objectMapper"); lazyInitBitmap |= OBJECT_MAPPER_LAZY_INIT_BIT; } } } return objectMapper; } /** * Creates a builder for {@link ImmutableSearchHit ImmutableSearchHit}. *

   * ImmutableSearchHit.builder()
   *    .putProperty|putAllProperties(Object => Object) // {@link SearchHit#getProperties() properties} mappings
   *    .highlight(com.arakelian.jackson.MapPath) // optional {@link SearchHit#getHighlight() highlight}
   *    .id(String) // required {@link SearchHit#getId() id}
   *    .index(String | null) // nullable {@link SearchHit#getIndex() index}
   *    .addMatchedQuery|addAllMatchedQueries(String) // {@link SearchHit#getMatchedQueries() matchedQueries} elements
   *    .score(Double | null) // nullable {@link SearchHit#getScore() score}
   *    .source(com.arakelian.elastic.model.search.Source) // optional {@link SearchHit#getSource() source}
   *    .type(String | null) // nullable {@link SearchHit#getType() type}
   *    .build();
   * 
* @return A new ImmutableSearchHit builder */ public static ImmutableSearchHit.Builder builder() { return new ImmutableSearchHit.Builder(); } /** * Builds instances of type {@link ImmutableSearchHit ImmutableSearchHit}. * 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 = "SearchHit", generator = "Immutables") @NotThreadSafe @JsonPropertyOrder({"_index", "_type", "_id", "_score", "_source", "matched_queries"}) public static final class Builder { private static final long INIT_BIT_ID = 0x1L; private static final long OPT_BIT_PROPERTIES = 0x1L; private static final long OPT_BIT_MATCHED_QUERIES = 0x2L; private long initBits = 0x1L; private long optBits; private ImmutableMap.Builder properties = ImmutableMap.builder(); private @javax.annotation.Nullable MapPath highlight; private @javax.annotation.Nullable String id; private @javax.annotation.Nullable String index; private ImmutableSortedSet.Builder matchedQueries = ImmutableSortedSet.naturalOrder(); private @javax.annotation.Nullable Double score; private @javax.annotation.Nullable Source source; private @javax.annotation.Nullable String type; private Builder() { } /** * Fill a builder with attribute values from the provided {@code com.arakelian.jackson.AbstractMapPath} 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(AbstractMapPath instance) { Objects.requireNonNull(instance, "instance"); from((Object) instance); return this; } /** * Fill a builder with attribute values from the provided {@code com.arakelian.elastic.model.search.SearchHit} 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(SearchHit instance) { Objects.requireNonNull(instance, "instance"); from((Object) instance); return this; } private void from(Object object) { if (object instanceof AbstractMapPath) { AbstractMapPath instance = (AbstractMapPath) object; putAllProperties(instance.getProperties()); } if (object instanceof SearchHit) { SearchHit instance = (SearchHit) object; highlight(instance.getHighlight()); Double scoreValue = instance.getScore(); if (scoreValue != null) { score(scoreValue); } addAllMatchedQueries(instance.getMatchedQueries()); String indexValue = instance.getIndex(); if (indexValue != null) { index(indexValue); } id(instance.getId()); source(instance.getSource()); String typeValue = instance.getType(); if (typeValue != null) { type(typeValue); } } } /** * Put one entry to the {@link SearchHit#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(Object key, Object value) { this.properties.put(key, value); optBits |= OPT_BIT_PROPERTIES; return this; } /** * Put one entry to the {@link SearchHit#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 SearchHit#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 SearchHit#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; } /** * Initializes the value for the {@link SearchHit#getHighlight() highlight} attribute. *

If not set, this attribute will have a default value as returned by the initializer of {@link SearchHit#getHighlight() highlight}. * @param highlight The value for highlight * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty("highlight") public final Builder highlight(MapPath highlight) { this.highlight = Objects.requireNonNull(highlight, "highlight"); return this; } /** * Initializes the value for the {@link SearchHit#getId() id} attribute. * @param id The value for id * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty("_id") public final Builder id(String id) { this.id = Objects.requireNonNull(id, "id"); initBits &= ~INIT_BIT_ID; return this; } /** * Initializes the value for the {@link SearchHit#getIndex() index} attribute. * @param index The value for index (can be {@code null}) * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty("_index") public final Builder index(@Nullable String index) { this.index = index; return this; } /** * Adds one element to {@link SearchHit#getMatchedQueries() matchedQueries} sortedSet. * @param element A matchedQueries element * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder addMatchedQuery(String element) { this.matchedQueries.add(element); optBits |= OPT_BIT_MATCHED_QUERIES; return this; } /** * Adds elements to {@link SearchHit#getMatchedQueries() matchedQueries} sortedSet. * @param elements An array of matchedQueries elements * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder addMatchedQueries(String... elements) { this.matchedQueries.addAll(Arrays.asList(elements)); optBits |= OPT_BIT_MATCHED_QUERIES; return this; } /** * Sets or replaces all elements for {@link SearchHit#getMatchedQueries() matchedQueries} sortedSet. * @param elements An iterable of matchedQueries elements * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty("matched_queries") public final Builder matchedQueries(Iterable elements) { this.matchedQueries = ImmutableSortedSet.naturalOrder(); return addAllMatchedQueries(elements); } /** * Adds elements to {@link SearchHit#getMatchedQueries() matchedQueries} sortedSet. * @param elements An iterable of matchedQueries elements * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder addAllMatchedQueries(Iterable elements) { this.matchedQueries.addAll(elements); optBits |= OPT_BIT_MATCHED_QUERIES; return this; } /** * Initializes the value for the {@link SearchHit#getScore() score} attribute. * @param score The value for score (can be {@code null}) * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty("_score") public final Builder score(@Nullable Double score) { this.score = score; return this; } /** * Initializes the value for the {@link SearchHit#getSource() source} attribute. *

If not set, this attribute will have a default value as returned by the initializer of {@link SearchHit#getSource() source}. * @param source The value for source * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty("_source") public final Builder source(Source source) { this.source = Objects.requireNonNull(source, "source"); return this; } /** * Initializes the value for the {@link SearchHit#getType() type} attribute. * @param type The value for type (can be {@code null}) * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty("_type") public final Builder type(@Nullable String type) { this.type = type; return this; } /** * Builds a new {@link ImmutableSearchHit ImmutableSearchHit}. * @return An immutable instance of SearchHit * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableSearchHit build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableSearchHit(this); } private boolean propertiesIsSet() { return (optBits & OPT_BIT_PROPERTIES) != 0; } private boolean matchedQueriesIsSet() { return (optBits & OPT_BIT_MATCHED_QUERIES) != 0; } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_ID) != 0) attributes.add("id"); return "Cannot build SearchHit, some of required attributes are not set " + attributes; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy