org.immutables.fixture.style.ImmutableOptionalWithoutNullable 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 OptionalWithoutNullable}.
*
* Use the builder to create immutable instances:
* {@code ImmutableOptionalWithoutNullable.builder()}.
*/
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "OptionalWithoutNullable"})
@Immutable
public final class ImmutableOptionalWithoutNullable
implements OptionalWithoutNullable {
private final @Nullable String javaOptional;
private final @Nullable Integer javaOptionalInt;
private final Optional guavaOptional;
private ImmutableOptionalWithoutNullable(
@Nullable String javaOptional,
@Nullable Integer javaOptionalInt,
Optional guavaOptional) {
this.javaOptional = javaOptional;
this.javaOptionalInt = javaOptionalInt;
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 guavaOptional} attribute
*/
@Override
public Optional getGuavaOptional() {
return guavaOptional;
}
/**
* Copy the current immutable object by setting a present value for the optional {@link OptionalWithoutNullable#getJavaOptional() javaOptional} attribute.
* @param value The value for javaOptional
* @return A modified copy of {@code this} object
*/
public final ImmutableOptionalWithoutNullable withJavaOptional(String value) {
@Nullable String newValue = Preconditions.checkNotNull(value, "javaOptional");
if (Objects.equal(this.javaOptional, newValue)) return this;
return new ImmutableOptionalWithoutNullable(newValue, this.javaOptionalInt, this.guavaOptional);
}
/**
* Copy the current immutable object by setting an optional value for the {@link OptionalWithoutNullable#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 ImmutableOptionalWithoutNullable withJavaOptional(java.util.Optional optional) {
@Nullable String value = optional.orElse(null);
if (Objects.equal(this.javaOptional, value)) return this;
return new ImmutableOptionalWithoutNullable(value, this.javaOptionalInt, this.guavaOptional);
}
/**
* Copy the current immutable object by setting a present value for the optional {@link OptionalWithoutNullable#getJavaOptionalInt() javaOptionalInt} attribute.
* @param value The value for javaOptionalInt
* @return A modified copy of {@code this} object
*/
public final ImmutableOptionalWithoutNullable withJavaOptionalInt(int value) {
@Nullable Integer newValue = value;
if (Objects.equal(this.javaOptionalInt, newValue)) return this;
return new ImmutableOptionalWithoutNullable(this.javaOptional, newValue, this.guavaOptional);
}
/**
* Copy the current immutable object by setting an optional value for the {@link OptionalWithoutNullable#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 ImmutableOptionalWithoutNullable withJavaOptionalInt(OptionalInt optional) {
@Nullable Integer value = optional.isPresent() ? optional.getAsInt() : null;
if (Objects.equal(this.javaOptionalInt, value)) return this;
return new ImmutableOptionalWithoutNullable(this.javaOptional, value, this.guavaOptional);
}
/**
* Copy the current immutable object by setting a present value for the optional {@link OptionalWithoutNullable#getGuavaOptional() guavaOptional} attribute.
* @param value The value for guavaOptional
* @return A modified copy of {@code this} object
*/
public final ImmutableOptionalWithoutNullable withGuavaOptional(String value) {
Optional newValue = Optional.of(value);
if (this.guavaOptional.equals(newValue)) return this;
return new ImmutableOptionalWithoutNullable(this.javaOptional, this.javaOptionalInt, newValue);
}
/**
* Copy the current immutable object by setting an optional value for the {@link OptionalWithoutNullable#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 ImmutableOptionalWithoutNullable withGuavaOptional(Optional optional) {
Optional value = Preconditions.checkNotNull(optional, "guavaOptional");
if (this.guavaOptional.equals(value)) return this;
return new ImmutableOptionalWithoutNullable(this.javaOptional, this.javaOptionalInt, value);
}
/**
* This instance is equal to all instances of {@code ImmutableOptionalWithoutNullable} 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 ImmutableOptionalWithoutNullable
&& equalTo((ImmutableOptionalWithoutNullable) another);
}
private boolean equalTo(ImmutableOptionalWithoutNullable another) {
return Objects.equal(javaOptional, another.javaOptional)
&& Objects.equal(javaOptionalInt, another.javaOptionalInt)
&& guavaOptional.equals(another.guavaOptional);
}
/**
* Computes a hash code from attributes: {@code javaOptional}, {@code javaOptionalInt}, {@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 + guavaOptional.hashCode();
return h;
}
/**
* Prints the immutable value {@code OptionalWithoutNullable} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("OptionalWithoutNullable")
.omitNullValues()
.add("javaOptional", javaOptional)
.add("javaOptionalInt", javaOptionalInt)
.add("guavaOptional", guavaOptional.orNull())
.toString();
}
/**
* Creates an immutable copy of a {@link OptionalWithoutNullable} 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 OptionalWithoutNullable instance
*/
public static ImmutableOptionalWithoutNullable copyOf(OptionalWithoutNullable instance) {
if (instance instanceof ImmutableOptionalWithoutNullable) {
return (ImmutableOptionalWithoutNullable) instance;
}
return ImmutableOptionalWithoutNullable.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableOptionalWithoutNullable ImmutableOptionalWithoutNullable}.
* @return A new ImmutableOptionalWithoutNullable builder
*/
public static ImmutableOptionalWithoutNullable.Builder builder() {
return new ImmutableOptionalWithoutNullable.Builder();
}
/**
* Builds instances of type {@link ImmutableOptionalWithoutNullable ImmutableOptionalWithoutNullable}.
* 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 Optional guavaOptional = Optional.absent();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code OptionalWithoutNullable} 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(OptionalWithoutNullable 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);
}
Optional guavaOptionalOptional = instance.getGuavaOptional();
if (guavaOptionalOptional.isPresent()) {
guavaOptional(guavaOptionalOptional);
}
return this;
}
/**
* Initializes the optional value {@link OptionalWithoutNullable#getJavaOptional() javaOptional} to javaOptional.
* @param javaOptional The value for javaOptional
* @return {@code this} builder for chained invocation
*/
public final Builder javaOptional(String javaOptional) {
this.javaOptional = Preconditions.checkNotNull(javaOptional, "javaOptional");
return this;
}
/**
* Initializes the optional value {@link OptionalWithoutNullable#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 OptionalWithoutNullable#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 OptionalWithoutNullable#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 OptionalWithoutNullable#getGuavaOptional() guavaOptional} to guavaOptional.
* @param guavaOptional The value for guavaOptional
* @return {@code this} builder for chained invocation
*/
public final Builder guavaOptional(String guavaOptional) {
this.guavaOptional = Optional.of(guavaOptional);
return this;
}
/**
* Initializes the optional value {@link OptionalWithoutNullable#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 ImmutableOptionalWithoutNullable ImmutableOptionalWithoutNullable}.
* @return An immutable instance of OptionalWithoutNullable
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableOptionalWithoutNullable build() {
return new ImmutableOptionalWithoutNullable(javaOptional, javaOptionalInt, guavaOptional);
}
}
}