org.immutables.fixture.jackson.ImmutableJacksonNullable Maven / Gradle / Ivy
Show all versions of value-fixture Show documentation
package org.immutables.fixture.jackson;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
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 JacksonNullable}.
*
* Use the builder to create immutable instances:
* {@code ImmutableJacksonNullable.builder()}.
*/
@Generated(from = "JacksonNullable", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableJacksonNullable implements JacksonNullable {
private final String notNullable;
private final @Nullable String nullable;
private ImmutableJacksonNullable(String notNullable, @Nullable String nullable) {
this.notNullable = notNullable;
this.nullable = nullable;
}
/**
* @return The value of the {@code notNullable} attribute
*/
@JsonProperty(value = "notNullable", required = true)
@Override
public String notNullable() {
return notNullable;
}
/**
* @return The value of the {@code nullable} attribute
*/
@JsonProperty(value = "nullable")
@Override
public @Nullable String nullable() {
return nullable;
}
/**
* Copy the current immutable object by setting a value for the {@link JacksonNullable#notNullable() notNullable} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for notNullable
* @return A modified copy or the {@code this} object
*/
public final ImmutableJacksonNullable withNotNullable(String value) {
String newValue = Objects.requireNonNull(value, "notNullable");
if (this.notNullable.equals(newValue)) return this;
return new ImmutableJacksonNullable(newValue, this.nullable);
}
/**
* Copy the current immutable object by setting a value for the {@link JacksonNullable#nullable() nullable} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for nullable (can be {@code null})
* @return A modified copy or the {@code this} object
*/
public final ImmutableJacksonNullable withNullable(@Nullable String value) {
if (Objects.equals(this.nullable, value)) return this;
return new ImmutableJacksonNullable(this.notNullable, value);
}
/**
* This instance is equal to all instances of {@code ImmutableJacksonNullable} 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 ImmutableJacksonNullable
&& equalTo(0, (ImmutableJacksonNullable) another);
}
private boolean equalTo(int synthetic, ImmutableJacksonNullable another) {
return notNullable.equals(another.notNullable)
&& Objects.equals(nullable, another.nullable);
}
/**
* Computes a hash code from attributes: {@code notNullable}, {@code nullable}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + notNullable.hashCode();
h += (h << 5) + Objects.hashCode(nullable);
return h;
}
/**
* Prints the immutable value {@code JacksonNullable} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("JacksonNullable")
.omitNullValues()
.add("notNullable", notNullable)
.add("nullable", nullable)
.toString();
}
/**
* Creates an immutable copy of a {@link JacksonNullable} 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 JacksonNullable instance
*/
public static ImmutableJacksonNullable copyOf(JacksonNullable instance) {
if (instance instanceof ImmutableJacksonNullable) {
return (ImmutableJacksonNullable) instance;
}
return ImmutableJacksonNullable.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableJacksonNullable ImmutableJacksonNullable}.
*
* ImmutableJacksonNullable.builder()
* .notNullable(String) // required {@link JacksonNullable#notNullable() notNullable}
* .nullable(String | null) // nullable {@link JacksonNullable#nullable() nullable}
* .build();
*
* @return A new ImmutableJacksonNullable builder
*/
public static ImmutableJacksonNullable.Builder builder() {
return new ImmutableJacksonNullable.Builder();
}
/**
* Builds instances of type {@link ImmutableJacksonNullable ImmutableJacksonNullable}.
* 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 = "JacksonNullable", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_NOT_NULLABLE = 0x1L;
private long initBits = 0x1L;
private @Nullable String notNullable;
private @Nullable String nullable;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code JacksonNullable} 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
*/
@CanIgnoreReturnValue
public final Builder from(JacksonNullable instance) {
Objects.requireNonNull(instance, "instance");
this.notNullable(instance.notNullable());
@Nullable String nullableValue = instance.nullable();
if (nullableValue != null) {
nullable(nullableValue);
}
return this;
}
/**
* Initializes the value for the {@link JacksonNullable#notNullable() notNullable} attribute.
* @param notNullable The value for notNullable
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty(value = "notNullable", required = true)
public final Builder notNullable(String notNullable) {
this.notNullable = Objects.requireNonNull(notNullable, "notNullable");
initBits &= ~INIT_BIT_NOT_NULLABLE;
return this;
}
/**
* Initializes the value for the {@link JacksonNullable#nullable() nullable} attribute.
* @param nullable The value for nullable (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty(value = "nullable")
public final Builder nullable(@Nullable String nullable) {
this.nullable = nullable;
return this;
}
/**
* Builds a new {@link ImmutableJacksonNullable ImmutableJacksonNullable}.
* @return An immutable instance of JacksonNullable
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableJacksonNullable build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableJacksonNullable(notNullable, nullable);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_NOT_NULLABLE) != 0) attributes.add("notNullable");
return "Cannot build JacksonNullable, some of required attributes are not set " + attributes;
}
}
}