org.immutables.fixture.style.ImmutableOptionalWithNullable Maven / Gradle / Ivy
package org.immutables.fixture.style;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import java.util.OptionalInt;
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 OptionalWithNullable}.
*
* Use the builder to create immutable instances:
* {@code ImmutableOptionalWithNullable.builder()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "OptionalWithNullable"})
@Immutable
public final class ImmutableOptionalWithNullable implements OptionalWithNullable {
private final @Nullable String javaOptional;
private final @Nullable Integer javaOptionalInt;
private final @Nullable Integer javaOptionalInteger;
private final Optional guavaOptional;
private ImmutableOptionalWithNullable(
@Nullable String javaOptional,
@Nullable Integer javaOptionalInt,
@Nullable Integer javaOptionalInteger,
Optional guavaOptional) {
this.javaOptional = javaOptional;
this.javaOptionalInt = javaOptionalInt;
this.javaOptionalInteger = javaOptionalInteger;
this.guavaOptional = guavaOptional;
}
/**
* @return The value of the {@code javaOptional} attribute
*/
@Override
public java.util.Optional getJavaOptional() {
return java.util.Optional.ofNullable(javaOptional);
}
/**
* @return The value of the {@code javaOptionalInt} attribute
*/
@Override
public OptionalInt getJavaOptionalInt() {
return javaOptionalInt != null
? OptionalInt.of(javaOptionalInt)
: OptionalInt.empty();
}
/**
* @return The value of the {@code javaOptionalInteger} attribute
*/
@Override
public java.util.Optional getJavaOptionalInteger() {
return java.util.Optional.ofNullable(javaOptionalInteger);
}
/**
* @return The value of the {@code guavaOptional} attribute
*/
@Override
public Optional getGuavaOptional() {
return guavaOptional;
}
/**
* Copy the current immutable object by setting a present value for the optional {@link OptionalWithNullable#getJavaOptional() javaOptional} attribute.
* @param value The value for javaOptional, {@code null} is accepted as {@code java.util.Optional.empty()}
* @return A modified copy of {@code this} object
*/
public final ImmutableOptionalWithNullable withJavaOptional(@Nullable String value) {
@Nullable String newValue = value;
if (Objects.equal(this.javaOptional, newValue)) return this;
return new ImmutableOptionalWithNullable(newValue, this.javaOptionalInt, this.javaOptionalInteger, this.guavaOptional);
}
/**
* Copy the current immutable object by setting an optional value for the {@link OptionalWithNullable#getJavaOptional() javaOptional} attribute.
* An equality check is used on inner nullable value to prevent copying of the same value by returning {@code this}.
* @param optional A value for javaOptional
* @return A modified copy of {@code this} object
*/
public final ImmutableOptionalWithNullable withJavaOptional(java.util.Optional optional) {
@Nullable String value = optional.orElse(null);
if (Objects.equal(this.javaOptional, value)) return this;
return new ImmutableOptionalWithNullable(value, this.javaOptionalInt, this.javaOptionalInteger, this.guavaOptional);
}
/**
* Copy the current immutable object by setting a present value for the optional {@link OptionalWithNullable#getJavaOptionalInt() javaOptionalInt} attribute.
* @param value The value for javaOptionalInt
* @return A modified copy of {@code this} object
*/
public final ImmutableOptionalWithNullable withJavaOptionalInt(int value) {
@Nullable Integer newValue = value;
if (Objects.equal(this.javaOptionalInt, newValue)) return this;
return new ImmutableOptionalWithNullable(this.javaOptional, newValue, this.javaOptionalInteger, this.guavaOptional);
}
/**
* Copy the current immutable object by setting an optional value for the {@link OptionalWithNullable#getJavaOptionalInt() javaOptionalInt} attribute.
* An equality check is used on inner nullable value to prevent copying of the same value by returning {@code this}.
* @param optional A value for javaOptionalInt
* @return A modified copy of {@code this} object
*/
public final ImmutableOptionalWithNullable withJavaOptionalInt(OptionalInt optional) {
@Nullable Integer value = optional.isPresent() ? optional.getAsInt() : null;
if (Objects.equal(this.javaOptionalInt, value)) return this;
return new ImmutableOptionalWithNullable(this.javaOptional, value, this.javaOptionalInteger, this.guavaOptional);
}
/**
* Copy the current immutable object by setting a present value for the optional {@link OptionalWithNullable#getJavaOptionalInteger() javaOptionalInteger} attribute.
* @param value The value for javaOptionalInteger, {@code null} is accepted as {@code java.util.Optional.empty()}
* @return A modified copy of {@code this} object
*/
public final ImmutableOptionalWithNullable withJavaOptionalInteger(@Nullable Integer value) {
@Nullable Integer newValue = value;
if (Objects.equal(this.javaOptionalInteger, newValue)) return this;
return new ImmutableOptionalWithNullable(this.javaOptional, this.javaOptionalInt, newValue, this.guavaOptional);
}
/**
* Copy the current immutable object by setting an optional value for the {@link OptionalWithNullable#getJavaOptionalInteger() javaOptionalInteger} attribute.
* An equality check is used on inner nullable value to prevent copying of the same value by returning {@code this}.
* @param optional A value for javaOptionalInteger
* @return A modified copy of {@code this} object
*/
public final ImmutableOptionalWithNullable withJavaOptionalInteger(java.util.Optional optional) {
@Nullable Integer value = optional.orElse(null);
if (Objects.equal(this.javaOptionalInteger, value)) return this;
return new ImmutableOptionalWithNullable(this.javaOptional, this.javaOptionalInt, value, this.guavaOptional);
}
/**
* Copy the current immutable object by setting a present value for the optional {@link OptionalWithNullable#getGuavaOptional() guavaOptional} attribute.
* @param value The value for guavaOptional, {@code null} is accepted as {@code com.google.common.base.Optional.absent()}
* @return A modified copy of {@code this} object
*/
public final ImmutableOptionalWithNullable withGuavaOptional(@Nullable String value) {
Optional newValue = Optional.fromNullable(value);
if (this.guavaOptional.equals(newValue)) return this;
return new ImmutableOptionalWithNullable(this.javaOptional, this.javaOptionalInt, this.javaOptionalInteger, newValue);
}
/**
* Copy the current immutable object by setting an optional value for the {@link OptionalWithNullable#getGuavaOptional() guavaOptional} attribute.
* An equality check is used to prevent copying of the same value by returning {@code this}.
* @param optional A value for guavaOptional
* @return A modified copy of {@code this} object
*/
public final ImmutableOptionalWithNullable withGuavaOptional(Optional optional) {
Optional value = Preconditions.checkNotNull(optional, "guavaOptional");
if (this.guavaOptional.equals(value)) return this;
return new ImmutableOptionalWithNullable(this.javaOptional, this.javaOptionalInt, this.javaOptionalInteger, value);
}
/**
* This instance is equal to all instances of {@code ImmutableOptionalWithNullable} 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 ImmutableOptionalWithNullable
&& equalTo((ImmutableOptionalWithNullable) another);
}
private boolean equalTo(ImmutableOptionalWithNullable another) {
return Objects.equal(javaOptional, another.javaOptional)
&& Objects.equal(javaOptionalInt, another.javaOptionalInt)
&& Objects.equal(javaOptionalInteger, another.javaOptionalInteger)
&& guavaOptional.equals(another.guavaOptional);
}
/**
* Computes a hash code from attributes: {@code javaOptional}, {@code javaOptionalInt}, {@code javaOptionalInteger}, {@code guavaOptional}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + Objects.hashCode(javaOptional);
h = h * 17 + Objects.hashCode(javaOptionalInt);
h = h * 17 + Objects.hashCode(javaOptionalInteger);
h = h * 17 + guavaOptional.hashCode();
return h;
}
/**
* Prints the immutable value {@code OptionalWithNullable} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("OptionalWithNullable")
.omitNullValues()
.add("javaOptional", javaOptional)
.add("javaOptionalInt", javaOptionalInt)
.add("javaOptionalInteger", javaOptionalInteger)
.add("guavaOptional", guavaOptional.orNull())
.toString();
}
/**
* Creates an immutable copy of a {@link OptionalWithNullable} 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 OptionalWithNullable instance
*/
public static ImmutableOptionalWithNullable copyOf(OptionalWithNullable instance) {
if (instance instanceof ImmutableOptionalWithNullable) {
return (ImmutableOptionalWithNullable) instance;
}
return ImmutableOptionalWithNullable.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableOptionalWithNullable ImmutableOptionalWithNullable}.
* @return A new ImmutableOptionalWithNullable builder
*/
public static ImmutableOptionalWithNullable.Builder builder() {
return new ImmutableOptionalWithNullable.Builder();
}
/**
* Builds instances of type {@link ImmutableOptionalWithNullable ImmutableOptionalWithNullable}.
* 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 @Nullable String javaOptional;
private @Nullable Integer javaOptionalInt;
private @Nullable Integer javaOptionalInteger;
private Optional guavaOptional = Optional.absent();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code OptionalWithNullable} 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
*/
public final Builder from(OptionalWithNullable instance) {
Preconditions.checkNotNull(instance, "instance");
java.util.Optional javaOptionalOptional = instance.getJavaOptional();
if (javaOptionalOptional.isPresent()) {
javaOptional(javaOptionalOptional);
}
OptionalInt javaOptionalIntOptional = instance.getJavaOptionalInt();
if (javaOptionalIntOptional.isPresent()) {
javaOptionalInt(javaOptionalIntOptional);
}
java.util.Optional javaOptionalIntegerOptional = instance.getJavaOptionalInteger();
if (javaOptionalIntegerOptional.isPresent()) {
javaOptionalInteger(javaOptionalIntegerOptional);
}
Optional guavaOptionalOptional = instance.getGuavaOptional();
if (guavaOptionalOptional.isPresent()) {
guavaOptional(guavaOptionalOptional);
}
return this;
}
/**
* Initializes the optional value {@link OptionalWithNullable#getJavaOptional() javaOptional} to javaOptional.
* @param javaOptional The value for javaOptional, {@code null} is accepted as {@code java.util.Optional.empty()}
* @return {@code this} builder for chained invocation
*/
public final Builder javaOptional(@Nullable String javaOptional) {
this.javaOptional = javaOptional;
return this;
}
/**
* Initializes the optional value {@link OptionalWithNullable#getJavaOptional() javaOptional} to javaOptional.
* @param javaOptional The value for javaOptional
* @return {@code this} builder for use in a chained invocation
*/
public final Builder javaOptional(java.util.Optional javaOptional) {
this.javaOptional = javaOptional.orElse(null);
return this;
}
/**
* Initializes the optional value {@link OptionalWithNullable#getJavaOptionalInt() javaOptionalInt} to javaOptionalInt.
* @param javaOptionalInt The value for javaOptionalInt
* @return {@code this} builder for chained invocation
*/
public final Builder javaOptionalInt(int javaOptionalInt) {
this.javaOptionalInt = javaOptionalInt;
return this;
}
/**
* Initializes the optional value {@link OptionalWithNullable#getJavaOptionalInt() javaOptionalInt} to javaOptionalInt.
* @param javaOptionalInt The value for javaOptionalInt
* @return {@code this} builder for use in a chained invocation
*/
public final Builder javaOptionalInt(OptionalInt javaOptionalInt) {
this.javaOptionalInt = javaOptionalInt.isPresent() ? javaOptionalInt.getAsInt() : null;
return this;
}
/**
* Initializes the optional value {@link OptionalWithNullable#getJavaOptionalInteger() javaOptionalInteger} to javaOptionalInteger.
* @param javaOptionalInteger The value for javaOptionalInteger, {@code null} is accepted as {@code java.util.Optional.empty()}
* @return {@code this} builder for chained invocation
*/
public final Builder javaOptionalInteger(@Nullable Integer javaOptionalInteger) {
this.javaOptionalInteger = javaOptionalInteger;
return this;
}
/**
* Initializes the optional value {@link OptionalWithNullable#getJavaOptionalInteger() javaOptionalInteger} to javaOptionalInteger.
* @param javaOptionalInteger The value for javaOptionalInteger
* @return {@code this} builder for use in a chained invocation
*/
public final Builder javaOptionalInteger(java.util.Optional javaOptionalInteger) {
this.javaOptionalInteger = javaOptionalInteger.orElse(null);
return this;
}
/**
* Initializes the optional value {@link OptionalWithNullable#getGuavaOptional() guavaOptional} to guavaOptional.
* @param guavaOptional The value for guavaOptional, {@code null} is accepted as {@code com.google.common.base.Optional.absent()}
* @return {@code this} builder for chained invocation
*/
public final Builder guavaOptional(@Nullable String guavaOptional) {
this.guavaOptional = Optional.fromNullable(guavaOptional);
return this;
}
/**
* Initializes the optional value {@link OptionalWithNullable#getGuavaOptional() guavaOptional} to guavaOptional.
* @param guavaOptional The value for guavaOptional
* @return {@code this} builder for use in a chained invocation
*/
public final Builder guavaOptional(Optional guavaOptional) {
this.guavaOptional = Preconditions.checkNotNull(guavaOptional, "guavaOptional");
return this;
}
/**
* Builds a new {@link ImmutableOptionalWithNullable ImmutableOptionalWithNullable}.
* @return An immutable instance of OptionalWithNullable
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableOptionalWithNullable build() {
return new ImmutableOptionalWithNullable(javaOptional, javaOptionalInt, javaOptionalInteger, guavaOptional);
}
}
}