com.arakelian.elastic.model.aggs.bucket.ImmutableRange Maven / Gradle / Ivy
package com.arakelian.elastic.model.aggs.bucket;
import com.arakelian.core.feature.Nullable;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
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.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link Range}.
*
* Use the builder to create immutable instances:
* {@code ImmutableRange.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableRange implements Range {
private final @Nullable Object from;
private final String key;
private final @Nullable String mask;
private final @Nullable Object to;
private ImmutableRange(ImmutableRange.Builder builder) {
this.from = builder.from;
this.key = builder.key;
this.mask = builder.mask;
this.to = builder.to;
}
/**
* @return The value of the {@code from} attribute
*/
@JsonProperty("from")
@Override
public @Nullable Object getFrom() {
return from;
}
/**
* @return The value of the {@code key} attribute
*/
@JsonProperty("key")
@Override
public String getKey() {
return key;
}
/**
* Returns a CIDR mask that defines a range.
* @return a CIDR mask that defines a range.
*/
@JsonProperty("mask")
@Override
public @Nullable String getMask() {
return mask;
}
/**
* @return The value of the {@code to} attribute
*/
@JsonProperty("to")
@Override
public @Nullable Object getTo() {
return to;
}
/**
* This instance is equal to all instances of {@code ImmutableRange} 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 ImmutableRange
&& equalTo((ImmutableRange) another);
}
private boolean equalTo(ImmutableRange another) {
return Objects.equals(from, another.from)
&& key.equals(another.key)
&& Objects.equals(mask, another.mask)
&& Objects.equals(to, another.to);
}
/**
* Computes a hash code from attributes: {@code from}, {@code key}, {@code mask}, {@code to}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + Objects.hashCode(from);
h += (h << 5) + key.hashCode();
h += (h << 5) + Objects.hashCode(mask);
h += (h << 5) + Objects.hashCode(to);
return h;
}
/**
* Prints the immutable value {@code Range} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("Range")
.omitNullValues()
.add("from", from)
.add("key", key)
.add("mask", mask)
.add("to", to)
.toString();
}
private static ImmutableRange validate(ImmutableRange instance) {
instance.checkRange();
return instance;
}
/**
* Creates a builder for {@link ImmutableRange ImmutableRange}.
* @return A new ImmutableRange builder
*/
public static ImmutableRange.Builder builder() {
return new ImmutableRange.Builder();
}
/**
* Builds instances of type {@link ImmutableRange ImmutableRange}.
* 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
public static final class Builder {
private static final long INIT_BIT_KEY = 0x1L;
private long initBits = 0x1L;
private @javax.annotation.Nullable Object from;
private @javax.annotation.Nullable String key;
private @javax.annotation.Nullable String mask;
private @javax.annotation.Nullable Object to;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Range} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder using(Range instance) {
Objects.requireNonNull(instance, "instance");
Object fromValue = instance.getFrom();
if (fromValue != null) {
from(fromValue);
}
key(instance.getKey());
String maskValue = instance.getMask();
if (maskValue != null) {
mask(maskValue);
}
Object toValue = instance.getTo();
if (toValue != null) {
to(toValue);
}
return this;
}
/**
* Initializes the value for the {@link Range#getFrom() from} attribute.
* @param from The value for from (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("from")
public final Builder from(@Nullable Object from) {
this.from = from;
return this;
}
/**
* Initializes the value for the {@link Range#getKey() key} attribute.
* @param key The value for key
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("key")
public final Builder key(String key) {
this.key = Objects.requireNonNull(key, "key");
initBits &= ~INIT_BIT_KEY;
return this;
}
/**
* Initializes the value for the {@link Range#getMask() mask} attribute.
* @param mask The value for mask (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("mask")
public final Builder mask(@Nullable String mask) {
this.mask = mask;
return this;
}
/**
* Initializes the value for the {@link Range#getTo() to} attribute.
* @param to The value for to (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("to")
public final Builder to(@Nullable Object to) {
this.to = to;
return this;
}
/**
* Builds a new {@link ImmutableRange ImmutableRange}.
* @return An immutable instance of Range
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableRange build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return ImmutableRange.validate(new ImmutableRange(this));
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_KEY) != 0) attributes.add("key");
return "Cannot build Range, some of required attributes are not set " + attributes;
}
}
}