com.arakelian.elastic.model.search.ImmutableRegexpQuery Maven / Gradle / Ivy
package com.arakelian.elastic.model.search;
import com.arakelian.core.feature.Nullable;
import com.arakelian.elastic.model.enums.RegexpFlag;
import com.arakelian.elastic.model.enums.Rewrite;
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.ImmutableSet;
import com.google.common.collect.Sets;
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.Objects;
import java.util.Set;
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 RegexpQuery}.
*
* Use the builder to create immutable instances:
* {@code ImmutableRegexpQuery.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableRegexpQuery implements RegexpQuery {
private final String fieldName;
private final ImmutableSet flags;
private final @Nullable Integer maxDeterminizedStates;
private final @Nullable Rewrite rewrite;
private final String value;
private final @Nullable Float boost;
private final @Nullable String name;
private final boolean hasStandardDefaults;
private ImmutableRegexpQuery(ImmutableRegexpQuery.Builder builder) {
this.fieldName = builder.fieldName;
this.maxDeterminizedStates = builder.maxDeterminizedStates;
this.rewrite = builder.rewrite;
this.value = builder.value;
this.boost = builder.boost;
this.name = builder.name;
if (builder.flagsIsSet()) {
initShim.flags(Sets.immutableEnumSet(builder.flags.build()));
}
this.flags = initShim.getFlags();
this.hasStandardDefaults = initShim.hasStandardDefaults();
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 flagsBuildStage = STAGE_UNINITIALIZED;
private ImmutableSet flags;
ImmutableSet getFlags() {
if (flagsBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (flagsBuildStage == STAGE_UNINITIALIZED) {
flagsBuildStage = STAGE_INITIALIZING;
this.flags = Sets.immutableEnumSet(getFlagsInitialize());
flagsBuildStage = STAGE_INITIALIZED;
}
return this.flags;
}
void flags(ImmutableSet flags) {
this.flags = flags;
flagsBuildStage = STAGE_INITIALIZED;
}
private byte hasStandardDefaultsBuildStage = STAGE_UNINITIALIZED;
private boolean hasStandardDefaults;
boolean hasStandardDefaults() {
if (hasStandardDefaultsBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (hasStandardDefaultsBuildStage == STAGE_UNINITIALIZED) {
hasStandardDefaultsBuildStage = STAGE_INITIALIZING;
this.hasStandardDefaults = hasStandardDefaultsInitialize();
hasStandardDefaultsBuildStage = STAGE_INITIALIZED;
}
return this.hasStandardDefaults;
}
private String formatInitCycleMessage() {
List attributes = new ArrayList<>();
if (flagsBuildStage == STAGE_INITIALIZING) attributes.add("flags");
if (hasStandardDefaultsBuildStage == STAGE_INITIALIZING) attributes.add("hasStandardDefaults");
return "Cannot build RegexpQuery, attribute initializers form cycle " + attributes;
}
}
private Set getFlagsInitialize() {
return RegexpQuery.super.getFlags();
}
private boolean hasStandardDefaultsInitialize() {
return RegexpQuery.super.hasStandardDefaults();
}
/**
* @return The value of the {@code fieldName} attribute
*/
@JsonProperty("field")
@Override
public String getFieldName() {
return fieldName;
}
/**
* Returns the set of regular expression flags to be applied.
* @return the set of regular expression flags to be applied.
* @see Lucene
* documentation
*/
@JsonProperty("flags")
@Override
public ImmutableSet getFlags() {
InitShim shim = this.initShim;
return shim != null
? shim.getFlags()
: this.flags;
}
/**
* @return The value of the {@code maxDeterminizedStates} attribute
*/
@JsonProperty("maxDeterminizedStates")
@Override
public @Nullable Integer getMaxDeterminizedStates() {
return maxDeterminizedStates;
}
/**
* @return The value of the {@code rewrite} attribute
*/
@JsonProperty("rewrite")
@Override
public @Nullable Rewrite getRewrite() {
return rewrite;
}
/**
* Returns the regular expression that will be matched.
* @return the regular expression that will be matched.
* @see Lucene
* documentation
* @see Elastic
* documentation
*/
@JsonProperty("value")
@Override
public String getValue() {
return value;
}
/**
* @return The value of the {@code boost} attribute
*/
@JsonProperty("boost")
@Override
public @Nullable Float getBoost() {
return boost;
}
/**
* Returns the name of the query
* @return name of the query
* @see Named
* Queries
*/
@JsonProperty("_name")
@Override
public @Nullable String getName() {
return name;
}
/**
* @return The computed-at-construction value of the {@code hasStandardDefaults} attribute
*/
@JsonProperty("hasStandardDefaults")
@JsonIgnore
@Override
public boolean hasStandardDefaults() {
InitShim shim = this.initShim;
return shim != null
? shim.hasStandardDefaults()
: this.hasStandardDefaults;
}
/**
* This instance is equal to all instances of {@code ImmutableRegexpQuery} 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 ImmutableRegexpQuery
&& equalTo((ImmutableRegexpQuery) another);
}
private boolean equalTo(ImmutableRegexpQuery another) {
return fieldName.equals(another.fieldName)
&& flags.equals(another.flags)
&& Objects.equals(maxDeterminizedStates, another.maxDeterminizedStates)
&& Objects.equals(rewrite, another.rewrite)
&& value.equals(another.value)
&& Objects.equals(boost, another.boost)
&& Objects.equals(name, another.name);
}
/**
* Computes a hash code from attributes: {@code fieldName}, {@code flags}, {@code maxDeterminizedStates}, {@code rewrite}, {@code value}, {@code boost}, {@code name}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + fieldName.hashCode();
h += (h << 5) + flags.hashCode();
h += (h << 5) + Objects.hashCode(maxDeterminizedStates);
h += (h << 5) + Objects.hashCode(rewrite);
h += (h << 5) + value.hashCode();
h += (h << 5) + Objects.hashCode(boost);
h += (h << 5) + Objects.hashCode(name);
return h;
}
/**
* Prints the immutable value {@code RegexpQuery} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("RegexpQuery")
.omitNullValues()
.add("fieldName", fieldName)
.add("flags", flags)
.add("maxDeterminizedStates", maxDeterminizedStates)
.add("rewrite", rewrite)
.add("value", value)
.add("boost", boost)
.add("name", name)
.toString();
}
/**
* Creates a builder for {@link ImmutableRegexpQuery ImmutableRegexpQuery}.
* @return A new ImmutableRegexpQuery builder
*/
public static ImmutableRegexpQuery.Builder builder() {
return new ImmutableRegexpQuery.Builder();
}
/**
* Builds instances of type {@link ImmutableRegexpQuery ImmutableRegexpQuery}.
* 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("regexp")
public static final class Builder {
private static final long INIT_BIT_FIELD_NAME = 0x1L;
private static final long INIT_BIT_VALUE = 0x2L;
private static final long OPT_BIT_FLAGS = 0x1L;
private long initBits = 0x3L;
private long optBits;
private @javax.annotation.Nullable String fieldName;
private ImmutableSet.Builder flags = ImmutableSet.builder();
private @javax.annotation.Nullable Integer maxDeterminizedStates;
private @javax.annotation.Nullable Rewrite rewrite;
private @javax.annotation.Nullable String value;
private @javax.annotation.Nullable Float boost;
private @javax.annotation.Nullable String name;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code com.arakelian.elastic.model.search.RegexpQuery} 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(RegexpQuery 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.StandardQuery} 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(StandardQuery instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
if (object instanceof RegexpQuery) {
RegexpQuery instance = (RegexpQuery) object;
addAllFlags(instance.getFlags());
Integer maxDeterminizedStatesValue = instance.getMaxDeterminizedStates();
if (maxDeterminizedStatesValue != null) {
maxDeterminizedStates(maxDeterminizedStatesValue);
}
fieldName(instance.getFieldName());
value(instance.getValue());
Rewrite rewriteValue = instance.getRewrite();
if (rewriteValue != null) {
rewrite(rewriteValue);
}
}
if (object instanceof StandardQuery) {
StandardQuery instance = (StandardQuery) object;
String nameValue = instance.getName();
if (nameValue != null) {
name(nameValue);
}
Float boostValue = instance.getBoost();
if (boostValue != null) {
boost(boostValue);
}
}
}
/**
* Initializes the value for the {@link RegexpQuery#getFieldName() fieldName} attribute.
* @param fieldName The value for fieldName
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("field")
public final Builder fieldName(String fieldName) {
this.fieldName = Objects.requireNonNull(fieldName, "fieldName");
initBits &= ~INIT_BIT_FIELD_NAME;
return this;
}
/**
* Adds one element to {@link RegexpQuery#getFlags() flags} set.
* @param element A flags element
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addFlag(RegexpFlag element) {
this.flags.add(element);
optBits |= OPT_BIT_FLAGS;
return this;
}
/**
* Adds elements to {@link RegexpQuery#getFlags() flags} set.
* @param elements An array of flags elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addFlags(RegexpFlag... elements) {
this.flags.addAll(Arrays.asList(elements));
optBits |= OPT_BIT_FLAGS;
return this;
}
/**
* Sets or replaces all elements for {@link RegexpQuery#getFlags() flags} set.
* @param elements An iterable of flags elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("flags")
public final Builder flags(Iterable elements) {
this.flags = ImmutableSet.builder();
return addAllFlags(elements);
}
/**
* Adds elements to {@link RegexpQuery#getFlags() flags} set.
* @param elements An iterable of flags elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addAllFlags(Iterable elements) {
this.flags.addAll(elements);
optBits |= OPT_BIT_FLAGS;
return this;
}
/**
* Initializes the value for the {@link RegexpQuery#getMaxDeterminizedStates() maxDeterminizedStates} attribute.
* @param maxDeterminizedStates The value for maxDeterminizedStates (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("maxDeterminizedStates")
public final Builder maxDeterminizedStates(@Nullable Integer maxDeterminizedStates) {
this.maxDeterminizedStates = maxDeterminizedStates;
return this;
}
/**
* Initializes the value for the {@link RegexpQuery#getRewrite() rewrite} attribute.
* @param rewrite The value for rewrite (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("rewrite")
public final Builder rewrite(@Nullable Rewrite rewrite) {
this.rewrite = rewrite;
return this;
}
/**
* Initializes the value for the {@link RegexpQuery#getValue() value} attribute.
* @param value The value for value
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("value")
public final Builder value(String value) {
this.value = Objects.requireNonNull(value, "value");
initBits &= ~INIT_BIT_VALUE;
return this;
}
/**
* Initializes the value for the {@link RegexpQuery#getBoost() boost} attribute.
* @param boost The value for boost (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("boost")
public final Builder boost(@Nullable Float boost) {
this.boost = boost;
return this;
}
/**
* Initializes the value for the {@link RegexpQuery#getName() name} attribute.
* @param name The value for name (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("_name")
public final Builder name(@Nullable String name) {
this.name = name;
return this;
}
/**
* Builds a new {@link ImmutableRegexpQuery ImmutableRegexpQuery}.
* @return An immutable instance of RegexpQuery
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableRegexpQuery build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableRegexpQuery(this);
}
private boolean flagsIsSet() {
return (optBits & OPT_BIT_FLAGS) != 0;
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_FIELD_NAME) != 0) attributes.add("fieldName");
if ((initBits & INIT_BIT_VALUE) != 0) attributes.add("value");
return "Cannot build RegexpQuery, some of required attributes are not set " + attributes;
}
}
}