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

io.stargate.db.datastore.query.ImmutableValue Maven / Gradle / Ivy

There is a newer version: 2.1.0-BETA-19
Show newest version
package io.stargate.db.datastore.query;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.stargate.db.schema.Column;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
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 Value}.
 * 

* Use the builder to create immutable instances: * {@code ImmutableValue.builder()}. */ @Generated(from = "Value", generator = "Immutables") @SuppressWarnings({"all"}) @SuppressFBWarnings @ParametersAreNonnullByDefault @javax.annotation.Generated("org.immutables.processor.ProxyProcessor") @Immutable final class ImmutableValue extends Value { private final Column column; private final @Nullable Object value; private final @Nullable Function bindingFunction; private ImmutableValue( Column column, @Nullable Object value, @Nullable Function bindingFunction) { this.column = column; this.value = value; this.bindingFunction = bindingFunction; } /** * @return The value of the {@code column} attribute */ @Override public Column column() { return column; } /** * @return The value of the {@code value} attribute */ @Override public Optional value() { return Optional.ofNullable(value); } /** * @return The value of the {@code bindingFunction} attribute */ @Override public Optional> bindingFunction() { return Optional.ofNullable(bindingFunction); } /** * Copy the current immutable object by setting a value for the {@link Value#column() column} attribute. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for column * @return A modified copy of the {@code this} object */ public final ImmutableValue withColumn(Column value) { if (this.column == value) return this; Column newValue = Objects.requireNonNull(value, "column"); return new ImmutableValue<>(newValue, this.value, this.bindingFunction); } /** * Copy the current immutable object by setting a present value for the optional {@link Value#value() value} attribute. * @param value The value for value * @return A modified copy of {@code this} object */ public final ImmutableValue withValue(Object value) { @Nullable Object newValue = Objects.requireNonNull(value, "value"); if (this.value == newValue) return this; return new ImmutableValue<>(this.column, newValue, this.bindingFunction); } /** * Copy the current immutable object by setting an optional value for the {@link Value#value() value} attribute. * A shallow reference equality check is used on unboxed optional value to prevent copying of the same value by returning {@code this}. * @param optional A value for value * @return A modified copy of {@code this} object */ @SuppressWarnings("unchecked") // safe covariant cast public final ImmutableValue withValue(Optional optional) { @Nullable Object value = optional.orElse(null); if (this.value == value) return this; return new ImmutableValue<>(this.column, value, this.bindingFunction); } /** * Copy the current immutable object by setting a present value for the optional {@link Value#bindingFunction() bindingFunction} attribute. * @param value The value for bindingFunction * @return A modified copy of {@code this} object */ public final ImmutableValue withBindingFunction(Function value) { @Nullable Function newValue = Objects.requireNonNull(value, "bindingFunction"); if (this.bindingFunction == newValue) return this; return new ImmutableValue<>(this.column, this.value, newValue); } /** * Copy the current immutable object by setting an optional value for the {@link Value#bindingFunction() bindingFunction} attribute. * A shallow reference equality check is used on unboxed optional value to prevent copying of the same value by returning {@code this}. * @param optional A value for bindingFunction * @return A modified copy of {@code this} object */ @SuppressWarnings("unchecked") // safe covariant cast public final ImmutableValue withBindingFunction(Optional> optional) { @Nullable Function value = optional.orElse(null); if (this.bindingFunction == value) return this; return new ImmutableValue<>(this.column, this.value, value); } /** * This instance is equal to all instances of {@code ImmutableValue} 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 ImmutableValue && equalTo((ImmutableValue) another); } private boolean equalTo(ImmutableValue another) { return column.equals(another.column) && Objects.equals(value, another.value) && Objects.equals(bindingFunction, another.bindingFunction); } /** * Computes a hash code from attributes: {@code column}, {@code value}, {@code bindingFunction}. * @return hashCode value */ @Override public int hashCode() { int h = 5381; h += (h << 5) + column.hashCode(); h += (h << 5) + Objects.hashCode(value); h += (h << 5) + Objects.hashCode(bindingFunction); return h; } /** * Prints the immutable value {@code Value} with attribute values. * @return A string representation of the value */ @Override public String toString() { StringBuilder builder = new StringBuilder("Value{"); builder.append("column=").append(column); if (value != null) { builder.append(", "); builder.append("value=").append(value); } if (bindingFunction != null) { builder.append(", "); builder.append("bindingFunction=").append(bindingFunction); } return builder.append("}").toString(); } /** * Creates an immutable copy of a {@link Value} 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 Value instance */ public static ImmutableValue copyOf(Value instance) { if (instance instanceof ImmutableValue) { return (ImmutableValue) instance; } return ImmutableValue.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableValue ImmutableValue}. *
   * ImmutableValue.&lt;T&gt;builder()
   *    .column(io.stargate.db.schema.Column) // required {@link Value#column() column}
   *    .value(Object) // optional {@link Value#value() value}
   *    .bindingFunction(function.Function&lt;T, Object&gt;) // optional {@link Value#bindingFunction() bindingFunction}
   *    .build();
   * 
* @param generic parameter T * @return A new ImmutableValue builder */ public static ImmutableValue.Builder builder() { return new ImmutableValue.Builder<>(); } /** * Builds instances of type {@link ImmutableValue ImmutableValue}. * 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 = "Value", generator = "Immutables") @NotThreadSafe public static final class Builder { private static final long INIT_BIT_COLUMN = 0x1L; private long initBits = 0x1L; private @Nullable Column column; private @Nullable Object value; private @Nullable Function bindingFunction; private Builder() { } /** * Fill a builder with attribute values from the provided {@code io.stargate.db.datastore.query.Parameter} instance. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(Parameter 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.Value} instance. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(Value instance) { Objects.requireNonNull(instance, "instance"); from((Object) instance); return this; } @SuppressWarnings("unchecked") private void from(Object object) { if (object instanceof Parameter) { Parameter instance = (Parameter) object; column(instance.column()); Optional valueOptional = instance.value(); if (valueOptional.isPresent()) { value(valueOptional); } Optional> bindingFunctionOptional = instance.bindingFunction(); if (bindingFunctionOptional.isPresent()) { bindingFunction(bindingFunctionOptional); } } } /** * Initializes the value for the {@link Value#column() column} attribute. * @param column The value for column * @return {@code this} builder for use in a chained invocation */ public final Builder column(Column column) { this.column = Objects.requireNonNull(column, "column"); initBits &= ~INIT_BIT_COLUMN; return this; } /** * Initializes the optional value {@link Value#value() value} to value. * @param value The value for value * @return {@code this} builder for chained invocation */ public final Builder value(Object value) { this.value = Objects.requireNonNull(value, "value"); return this; } /** * Initializes the optional value {@link Value#value() value} to value. * @param value The value for value * @return {@code this} builder for use in a chained invocation */ public final Builder value(Optional value) { this.value = value.orElse(null); return this; } /** * Initializes the optional value {@link Value#bindingFunction() bindingFunction} to bindingFunction. * @param bindingFunction The value for bindingFunction * @return {@code this} builder for chained invocation */ public final Builder bindingFunction(Function bindingFunction) { this.bindingFunction = Objects.requireNonNull(bindingFunction, "bindingFunction"); return this; } /** * Initializes the optional value {@link Value#bindingFunction() bindingFunction} to bindingFunction. * @param bindingFunction The value for bindingFunction * @return {@code this} builder for use in a chained invocation */ public final Builder bindingFunction(Optional> bindingFunction) { this.bindingFunction = bindingFunction.orElse(null); return this; } /** * Builds a new {@link ImmutableValue ImmutableValue}. * @return An immutable instance of Value * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableValue build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableValue<>(column, value, bindingFunction); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_COLUMN) != 0) attributes.add("column"); return "Cannot build Value, some of required attributes are not set " + attributes; } } }