org.immutables.fixture.jackson.ImmutableLazyAttributesSafe Maven / Gradle / Ivy
package org.immutables.fixture.jackson;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.ArrayList;
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 LazyAttributesSafe}.
*
* Use the builder to create immutable instances:
* {@code ImmutableLazyAttributesSafe.builder()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "LazyAttributesSafe"})
@Immutable
public final class ImmutableLazyAttributesSafe extends LazyAttributesSafe {
private final int a;
private final String derived;
private final int defaults;
private ImmutableLazyAttributesSafe(ImmutableLazyAttributesSafe.Builder builder) {
this.a = builder.a;
if (builder.defaultsIsSet()) {
initShim.defaults(builder.defaults);
}
this.defaults = initShim.getDefaults();
this.derived = initShim.getDerived();
this.initShim = null;
}
private ImmutableLazyAttributesSafe(int a, int defaults) {
this.a = a;
this.defaults = defaults;
initShim.defaults(this.defaults);
this.derived = initShim.getDerived();
this.initShim = null;
}
private static final int STAGE_INITIALIZING = -1;
private static final int STAGE_UNINITIALIZED = 0;
private static final int STAGE_INITIALIZED = 1;
private transient volatile InitShim initShim = new InitShim();
private final class InitShim {
private String derived;
private int derivedStage;
String getDerived() {
if (derivedStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (derivedStage == STAGE_UNINITIALIZED) {
derivedStage = STAGE_INITIALIZING;
this.derived = Preconditions.checkNotNull(ImmutableLazyAttributesSafe.super.getDerived(), "derived");
derivedStage = STAGE_INITIALIZED;
}
return this.derived;
}
private int defaults;
private int defaultsStage;
int getDefaults() {
if (defaultsStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (defaultsStage == STAGE_UNINITIALIZED) {
defaultsStage = STAGE_INITIALIZING;
this.defaults = ImmutableLazyAttributesSafe.super.getDefaults();
defaultsStage = STAGE_INITIALIZED;
}
return this.defaults;
}
void defaults(int defaults) {
this.defaults = defaults;
defaultsStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
ArrayList attributes = Lists.newArrayList();
if (derivedStage == STAGE_INITIALIZING) attributes.add("derived");
if (defaultsStage == STAGE_INITIALIZING) attributes.add("defaults");
return "Cannot build LazyAttributesSafe, attribute initializers form cycle" + attributes;
}
}
/**
* @return The value of the {@code a} attribute
*/
@JsonProperty
@Override
public int getA() {
return a;
}
/**
* @return The computed-at-construction value of the {@code derived} attribute
*/
@JsonProperty
@Override
public String getDerived() {
InitShim shim = this.initShim;
return shim != null
? shim.getDerived()
: this.derived;
}
/**
* @return The value of the {@code defaults} attribute
*/
@JsonProperty
@Override
public int getDefaults() {
InitShim shim = this.initShim;
return shim != null
? shim.getDefaults()
: this.defaults;
}
/**
* Copy the current immutable object by setting a value for the {@link LazyAttributesSafe#getA() 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 ImmutableLazyAttributesSafe withA(int a) {
if (this.a == a) return this;
return new ImmutableLazyAttributesSafe(a, this.defaults);
}
/**
* Copy the current immutable object by setting a value for the {@link LazyAttributesSafe#getDefaults() defaults} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param defaults A new value for defaults
* @return A modified copy of the {@code this} object
*/
public final ImmutableLazyAttributesSafe withDefaults(int defaults) {
if (this.defaults == defaults) return this;
return new ImmutableLazyAttributesSafe(this.a, defaults);
}
/**
* This instance is equal to all instances of {@code ImmutableLazyAttributesSafe} 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 ImmutableLazyAttributesSafe
&& equalTo((ImmutableLazyAttributesSafe) another);
}
private boolean equalTo(ImmutableLazyAttributesSafe another) {
return a == another.a
&& derived.equals(another.derived)
&& defaults == another.defaults;
}
/**
* Computes a hash code from attributes: {@code a}, {@code derived}, {@code defaults}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + a;
h = h * 17 + derived.hashCode();
h = h * 17 + defaults;
return h;
}
/**
* Prints the immutable value {@code LazyAttributesSafe} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("LazyAttributesSafe")
.omitNullValues()
.add("a", a)
.add("derived", derived)
.add("defaults", defaults)
.toString();
}
/**
* Utility type used to correctly read immutable object from JSON representation.
* @deprecated Do not use this type directly, it exists only for the Jackson-binding infrastructure
*/
@Deprecated
@JsonDeserialize
static final class Json extends LazyAttributesSafe {
@Nullable Integer a;
@Nullable Integer defaults;
boolean defaultsIsSet;
@JsonProperty
public void setA(int a) {
this.a = a;
}
@JsonProperty
public void setDefaults(int defaults) {
this.defaults = defaults;
this.defaultsIsSet = true;
}
@Override
public int getA() { throw new UnsupportedOperationException(); }
@Override
public String getDerived() { throw new UnsupportedOperationException(); }
@Override
public int getDefaults() { throw new UnsupportedOperationException(); }
@Override
public String getLazy() { throw new UnsupportedOperationException(); }
}
/**
* @param json A JSON-bindable data structure
* @return An immutable value type
* @deprecated Do not use this method directly, it exists only for the Jackson-binding infrastructure
*/
@Deprecated
@JsonCreator
static ImmutableLazyAttributesSafe fromJson(Json json) {
ImmutableLazyAttributesSafe.Builder builder = ImmutableLazyAttributesSafe.builder();
if (json.a != null) {
builder.a(json.a);
}
if (json.defaults != null) {
builder.defaults(json.defaults);
}
return builder.build();
}
private volatile long lazyInitBitmap;
private static final long LAZY_LAZY_INIT_BIT = 0x1L;
private String lazy;
/**
* {@inheritDoc}
*
* Returns a lazily initialized value of the {@link LazyAttributesSafe#getLazy() lazy} attribute.
* Initialized once and only once and stored for subsequent access with proper synchronization.
* @return A lazily initialized value of the {@code l.name} attribute
*/
@Override
public String getLazy() {
if ((lazyInitBitmap & LAZY_LAZY_INIT_BIT) == 0) {
synchronized (this) {
if ((lazyInitBitmap & LAZY_LAZY_INIT_BIT) == 0) {
this.lazy = Preconditions.checkNotNull(super.getLazy(), "lazy");
lazyInitBitmap |= LAZY_LAZY_INIT_BIT;
}
}
}
return lazy;
}
/**
* Creates an immutable copy of a {@link LazyAttributesSafe} 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 LazyAttributesSafe instance
*/
public static ImmutableLazyAttributesSafe copyOf(LazyAttributesSafe instance) {
if (instance instanceof ImmutableLazyAttributesSafe) {
return (ImmutableLazyAttributesSafe) instance;
}
return ImmutableLazyAttributesSafe.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableLazyAttributesSafe ImmutableLazyAttributesSafe}.
* @return A new ImmutableLazyAttributesSafe builder
*/
public static ImmutableLazyAttributesSafe.Builder builder() {
return new ImmutableLazyAttributesSafe.Builder();
}
/**
* Builds instances of type {@link ImmutableLazyAttributesSafe ImmutableLazyAttributesSafe}.
* 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_A = 0x1L;
private static final long OPT_BIT_DEFAULTS = 0x1L;
private long initBits = 0x1L;
private long optBits;
private int a;
private int defaults;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code LazyAttributesSafe} 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(LazyAttributesSafe instance) {
Preconditions.checkNotNull(instance, "instance");
a(instance.getA());
defaults(instance.getDefaults());
return this;
}
/**
* Initializes the value for the {@link LazyAttributesSafe#getA() 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;
}
/**
* Initializes the value for the {@link LazyAttributesSafe#getDefaults() defaults} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link LazyAttributesSafe#getDefaults() defaults}.
* @param defaults The value for defaults
* @return {@code this} builder for use in a chained invocation
*/
public final Builder defaults(int defaults) {
this.defaults = defaults;
optBits |= OPT_BIT_DEFAULTS;
return this;
}
/**
* Builds a new {@link ImmutableLazyAttributesSafe ImmutableLazyAttributesSafe}.
* @return An immutable instance of LazyAttributesSafe
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableLazyAttributesSafe build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableLazyAttributesSafe(this);
}
private boolean defaultsIsSet() {
return (optBits & OPT_BIT_DEFAULTS) != 0;
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if ((initBits & INIT_BIT_A) != 0) attributes.add("a");
return "Cannot build LazyAttributesSafe, some of required attributes are not set " + attributes;
}
}
}