io.stargate.db.datastore.query.ImmutableAndWhere Maven / Gradle / Ivy
package io.stargate.db.datastore.query;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link AndWhere}.
*
* Use the builder to create immutable instances:
* {@code ImmutableAndWhere.builder()}.
*/
@Generated(from = "AndWhere", generator = "Immutables")
@SuppressWarnings({"all"})
@SuppressFBWarnings
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
public final class ImmutableAndWhere extends AndWhere {
private final List> children;
private transient final int hashCode;
private ImmutableAndWhere(List> children) {
this.children = children;
this.hashCode = computeHashCode();
}
/**
* @return The value of the {@code children} attribute
*/
@Override
public List> children() {
return children;
}
/**
* Copy the current immutable object with elements that replace the content of {@link AndWhere#children() children}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
@SafeVarargs @SuppressWarnings("varargs")
public final ImmutableAndWhere withChildren(Where... elements) {
List> newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new ImmutableAndWhere<>(newValue);
}
/**
* Copy the current immutable object with elements that replace the content of {@link AndWhere#children() children}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of children elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableAndWhere withChildren(Iterable extends Where> elements) {
if (this.children == elements) return this;
List> newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new ImmutableAndWhere<>(newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableAndWhere} 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 ImmutableAndWhere>
&& equalTo((ImmutableAndWhere>) another);
}
private boolean equalTo(ImmutableAndWhere> another) {
if (hashCode != another.hashCode) return false;
return children.equals(another.children);
}
/**
* Returns a precomputed-on-construction hash code from attributes: {@code children}.
* @return hashCode value
*/
@Override
public int hashCode() {
return hashCode;
}
private int computeHashCode() {
int h = 5381;
h += (h << 5) + children.hashCode();
return h;
}
/**
* Creates an immutable copy of a {@link AndWhere} value.
* Uses accessors to get values to initialize the new immutable instance.
* If an instance is already immutable, it is returned as is.
* @param generic parameter T
* @param instance The instance to copy
* @return A copied immutable AndWhere instance
*/
public static ImmutableAndWhere copyOf(AndWhere instance) {
if (instance instanceof ImmutableAndWhere>) {
return (ImmutableAndWhere) instance;
}
return ImmutableAndWhere.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableAndWhere ImmutableAndWhere}.
*
* ImmutableAndWhere.<T>builder()
* .addChildren|addAllChildren(io.stargate.db.datastore.query.Where<T>) // {@link AndWhere#children() children} elements
* .build();
*
* @param generic parameter T
* @return A new ImmutableAndWhere builder
*/
public static ImmutableAndWhere.Builder builder() {
return new ImmutableAndWhere.Builder<>();
}
/**
* Builds instances of type {@link ImmutableAndWhere ImmutableAndWhere}.
* 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 = "AndWhere", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private List> children = new ArrayList>();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code io.stargate.db.datastore.query.NWhere} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(NWhere instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code io.stargate.db.datastore.query.AndWhere} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(AndWhere instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
@SuppressWarnings("unchecked")
private void from(Object object) {
if (object instanceof NWhere>) {
NWhere instance = (NWhere) object;
addAllChildren(instance.children());
}
}
/**
* Adds one element to {@link AndWhere#children() children} list.
* @param element A children element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addChildren(Where element) {
this.children.add(Objects.requireNonNull(element, "children element"));
return this;
}
/**
* Adds elements to {@link AndWhere#children() children} list.
* @param elements An array of children elements
* @return {@code this} builder for use in a chained invocation
*/
@SafeVarargs @SuppressWarnings("varargs")
public final Builder addChildren(Where... elements) {
for (Where element : elements) {
this.children.add(Objects.requireNonNull(element, "children element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link AndWhere#children() children} list.
* @param elements An iterable of children elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder children(Iterable extends Where> elements) {
this.children.clear();
return addAllChildren(elements);
}
/**
* Adds elements to {@link AndWhere#children() children} list.
* @param elements An iterable of children elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAllChildren(Iterable extends Where> elements) {
for (Where element : elements) {
this.children.add(Objects.requireNonNull(element, "children element"));
}
return this;
}
/**
* Builds a new {@link ImmutableAndWhere ImmutableAndWhere}.
* @return An immutable instance of AndWhere
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableAndWhere build() {
return new ImmutableAndWhere<>(createUnmodifiableList(true, children));
}
}
private static List createSafeList(Iterable extends T> iterable, boolean checkNulls, boolean skipNulls) {
ArrayList list;
if (iterable instanceof Collection>) {
int size = ((Collection>) iterable).size();
if (size == 0) return Collections.emptyList();
list = new ArrayList<>();
} else {
list = new ArrayList<>();
}
for (T element : iterable) {
if (skipNulls && element == null) continue;
if (checkNulls) Objects.requireNonNull(element, "element");
list.add(element);
}
return list;
}
private static List createUnmodifiableList(boolean clone, List list) {
switch(list.size()) {
case 0: return Collections.emptyList();
case 1: return Collections.singletonList(list.get(0));
default:
if (clone) {
return Collections.unmodifiableList(new ArrayList<>(list));
} else {
if (list instanceof ArrayList>) {
((ArrayList>) list).trimToSize();
}
return Collections.unmodifiableList(list);
}
}
}
}