org.immutables.fixture.generics.ImmutableB Maven / Gradle / Ivy
package org.immutables.fixture.generics;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.List;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link GenericSubType.B}.
*
* Use the builder to create immutable instances:
* {@code ImmutableB.builder()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "GenericSubType.B"})
@Immutable
public final class ImmutableB implements GenericSubType.B {
private final T b;
private final int a;
private ImmutableB(T b, int a) {
this.b = b;
this.a = a;
}
/**
* @return The value of the {@code b} attribute
*/
@Override
public T b() {
return b;
}
/**
* @return The value of the {@code a} attribute
*/
@Override
public int a() {
return a;
}
/**
* Copy the current immutable object by setting a value for the {@link GenericSubType.B#b() b} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param b A new value for b
* @return A modified copy of the {@code this} object
*/
public final ImmutableB withB(T b) {
if (this.b == b) return this;
T newValue = Preconditions.checkNotNull(b, "b");
return new ImmutableB<>(newValue, this.a);
}
/**
* Copy the current immutable object by setting a value for the {@link GenericSubType.B#a() a} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param a A new value for a
* @return A modified copy of the {@code this} object
*/
public final ImmutableB withA(int a) {
if (this.a == a) return this;
return new ImmutableB<>(this.b, a);
}
/**
* This instance is equal to all instances of {@code ImmutableB} 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 ImmutableB>
&& equalTo((ImmutableB>) another);
}
private boolean equalTo(ImmutableB> another) {
return b.equals(another.b)
&& a == another.a;
}
/**
* Computes a hash code from attributes: {@code b}, {@code a}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + b.hashCode();
h = h * 17 + a;
return h;
}
/**
* Prints the immutable value {@code B} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("B")
.omitNullValues()
.add("b", b)
.add("a", a)
.toString();
}
/**
* Creates an immutable copy of a {@link GenericSubType.B} 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 B instance
*/
public static ImmutableB copyOf(GenericSubType.B instance) {
if (instance instanceof ImmutableB>) {
return (ImmutableB) instance;
}
return ImmutableB.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableB ImmutableB}.
* @param generic parameter T
* @return A new ImmutableB builder
*/
public static ImmutableB.Builder builder() {
return new ImmutableB.Builder();
}
/**
* Builds instances of type {@link ImmutableB ImmutableB}.
* 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_B = 0x1L;
private static final long INIT_BIT_A = 0x2L;
private long initBits = 0x3L;
private @Nullable T b;
private int a;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code org.immutables.fixture.generics.GenericSubType.B} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(GenericSubType.B instance) {
Preconditions.checkNotNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code org.immutables.fixture.generics.GenericSubType.A} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(GenericSubType.A instance) {
Preconditions.checkNotNull(instance, "instance");
from((Object) instance);
return this;
}
@SuppressWarnings("unchecked")
private void from(Object object) {
if (object instanceof GenericSubType.B>) {
GenericSubType.B instance = (GenericSubType.B) object;
b(instance.b());
}
if (object instanceof GenericSubType.A) {
GenericSubType.A instance = (GenericSubType.A) object;
a(instance.a());
}
}
/**
* Initializes the value for the {@link GenericSubType.B#b() b} attribute.
* @param b The value for b
* @return {@code this} builder for use in a chained invocation
*/
public final Builder b(T b) {
this.b = Preconditions.checkNotNull(b, "b");
initBits &= ~INIT_BIT_B;
return this;
}
/**
* Initializes the value for the {@link GenericSubType.B#a() a} attribute.
* @param a The value for a
* @return {@code this} builder for use in a chained invocation
*/
public final Builder a(int a) {
this.a = a;
initBits &= ~INIT_BIT_A;
return this;
}
/**
* Builds a new {@link ImmutableB ImmutableB}.
* @return An immutable instance of B
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableB build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableB(b, a);
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if ((initBits & INIT_BIT_B) != 0) attributes.add("b");
if ((initBits & INIT_BIT_A) != 0) attributes.add("a");
return "Cannot build B, some of required attributes are not set " + attributes;
}
}
}