org.immutables.fixture.nested.ImmutableAAA Maven / Gradle / Ivy
Show all versions of value-fixture Show documentation
package org.immutables.fixture.nested;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
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.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link BaseFromComplicated.AAA}.
*
* Use the builder to create immutable instances:
* {@code ImmutableAAA.builder()}.
*/
@Generated(from = "BaseFromComplicated.AAA", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableAAA implements BaseFromComplicated.AAA {
private final int a;
private final ImmutableList b;
private ImmutableAAA(int a, ImmutableList b) {
this.a = a;
this.b = b;
}
/**
* @return The value of the {@code a} attribute
*/
@Override
public int a() {
return a;
}
/**
* @return The value of the {@code b} attribute
*/
@Override
public ImmutableList b() {
return b;
}
/**
* Copy the current immutable object by setting a value for the {@link BaseFromComplicated.AAA#a() a} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for a
* @return A modified copy of the {@code this} object
*/
public final ImmutableAAA withA(int value) {
if (this.a == value) return this;
return new ImmutableAAA(value, this.b);
}
/**
* Copy the current immutable object with elements that replace the content of {@link BaseFromComplicated.AAA#b() b}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableAAA withB(String... elements) {
ImmutableList newValue = ImmutableList.copyOf(elements);
return new ImmutableAAA(this.a, newValue);
}
/**
* Copy the current immutable object with elements that replace the content of {@link BaseFromComplicated.AAA#b() b}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of b elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableAAA withB(Iterable elements) {
if (this.b == elements) return this;
ImmutableList newValue = ImmutableList.copyOf(elements);
return new ImmutableAAA(this.a, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableAAA} 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 ImmutableAAA
&& equalTo(0, (ImmutableAAA) another);
}
private boolean equalTo(int synthetic, ImmutableAAA another) {
return a == another.a
&& b.equals(another.b);
}
/**
* Computes a hash code from attributes: {@code a}, {@code b}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + a;
h += (h << 5) + b.hashCode();
return h;
}
/**
* Prints the immutable value {@code AAA} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("AAA")
.omitNullValues()
.add("a", a)
.add("b", b)
.toString();
}
/**
* Creates an immutable copy of a {@link BaseFromComplicated.AAA} value.
* Uses accessors to get values to initialize the new immutable instance.
* If an instance is already immutable, it is returned as is.
* @param instance The instance to copy
* @return A copied immutable AAA instance
*/
public static ImmutableAAA copyOf(BaseFromComplicated.AAA instance) {
if (instance instanceof ImmutableAAA) {
return (ImmutableAAA) instance;
}
return ImmutableAAA.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableAAA ImmutableAAA}.
*
* ImmutableAAA.builder()
* .a(int) // required {@link BaseFromComplicated.AAA#a() a}
* .addB|addAllB(String) // {@link BaseFromComplicated.AAA#b() b} elements
* .build();
*
* @return A new ImmutableAAA builder
*/
public static ImmutableAAA.Builder builder() {
return new ImmutableAAA.Builder();
}
/**
* Builds instances of type {@link ImmutableAAA ImmutableAAA}.
* 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 = "BaseFromComplicated.AAA", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_A = 0x1L;
private long initBits = 0x1L;
private int a;
private ImmutableList.Builder b = ImmutableList.builder();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code ModifiableAAA} 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(ModifiableAAA instance) {
Objects.requireNonNull(instance, "instance");
if (instance.aIsSet()) {
a(instance.a());
}
addAllB(instance.b());
return this;
}
/**
* Fill a builder with attribute values from the provided {@code org.immutables.fixture.nested.BaseFromComplicated.AA} 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(BaseFromComplicated.AA instance) {
Objects.requireNonNull(instance, "instance");
from((short) 0, (Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code org.immutables.fixture.nested.BaseFromComplicated.AAA} 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(BaseFromComplicated.AAA instance) {
Objects.requireNonNull(instance, "instance");
from((short) 0, (Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code org.immutables.fixture.nested.BaseFromComplicated.A} 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(BaseFromComplicated.A instance) {
Objects.requireNonNull(instance, "instance");
from((short) 0, (Object) instance);
return this;
}
private void from(short _unused, Object object) {
if (object instanceof ModifiableAAA) {
from((ModifiableAAA) object);
return;
}
@Var long bits = 0;
if (object instanceof BaseFromComplicated.AA) {
BaseFromComplicated.AA instance = (BaseFromComplicated.AA) object;
if ((bits & 0x1L) == 0) {
a(instance.a());
bits |= 0x1L;
}
}
if (object instanceof BaseFromComplicated.AAA) {
BaseFromComplicated.AAA instance = (BaseFromComplicated.AAA) object;
if ((bits & 0x1L) == 0) {
a(instance.a());
bits |= 0x1L;
}
if ((bits & 0x2L) == 0) {
addAllB(instance.b());
bits |= 0x2L;
}
}
if (object instanceof BaseFromComplicated.A) {
BaseFromComplicated.A instance = (BaseFromComplicated.A) object;
if ((bits & 0x1L) == 0) {
a(instance.a());
bits |= 0x1L;
}
if ((bits & 0x2L) == 0) {
addAllB(instance.b());
bits |= 0x2L;
}
}
}
/**
* Initializes the value for the {@link BaseFromComplicated.AAA#a() a} attribute.
* @param a The value for a
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder a(int a) {
this.a = a;
initBits &= ~INIT_BIT_A;
return this;
}
/**
* Adds one element to {@link BaseFromComplicated.AAA#b() b} list.
* @param element A b element
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addB(String element) {
this.b.add(element);
return this;
}
/**
* Adds elements to {@link BaseFromComplicated.AAA#b() b} list.
* @param elements An array of b elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addB(String... elements) {
this.b.add(elements);
return this;
}
/**
* Sets or replaces all elements for {@link BaseFromComplicated.AAA#b() b} list.
* @param elements An iterable of b elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder b(Iterable elements) {
this.b = ImmutableList.builder();
return addAllB(elements);
}
/**
* Adds elements to {@link BaseFromComplicated.AAA#b() b} list.
* @param elements An iterable of b elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addAllB(Iterable elements) {
this.b.addAll(elements);
return this;
}
/**
* Builds a new {@link ImmutableAAA ImmutableAAA}.
* @return An immutable instance of AAA
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableAAA build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableAAA(a, b.build());
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_A) != 0) attributes.add("a");
return "Cannot build AAA, some of required attributes are not set " + attributes;
}
}
}