All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.immutables.fixture.ImmutableSillyLazy Maven / Gradle / Ivy

package org.immutables.fixture;

import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
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 SillyLazy}.
 * 

* Use the builder to create immutable instances: * {@code ImmutableSillyLazy.builder()}. * Use the static factory method to create immutable instances: * {@code ImmutableSillyLazy.of()}. */ @SuppressWarnings("all") @ParametersAreNonnullByDefault @Generated({"Immutables.generator", "SillyLazy"}) @Immutable public final class ImmutableSillyLazy extends SillyLazy { private final AtomicInteger counter; private ImmutableSillyLazy(AtomicInteger counter) { this.counter = Preconditions.checkNotNull(counter, "counter"); } private ImmutableSillyLazy(ImmutableSillyLazy original, AtomicInteger counter) { this.counter = counter; } /** * @return The value of the {@code counter} attribute */ @Override public AtomicInteger counter() { return counter; } /** * Copy the current immutable object by setting a value for the {@link SillyLazy#counter() counter} attribute. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param counter A new value for counter * @return A modified copy of the {@code this} object */ public final ImmutableSillyLazy withCounter(AtomicInteger counter) { if (this.counter == counter) return this; AtomicInteger newValue = Preconditions.checkNotNull(counter, "counter"); return new ImmutableSillyLazy(this, newValue); } /** * This instance is equal to all instances of {@code ImmutableSillyLazy} 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 ImmutableSillyLazy && equalTo((ImmutableSillyLazy) another); } private boolean equalTo(ImmutableSillyLazy another) { return counter.equals(another.counter); } /** * Computes a hash code from attributes: {@code counter}. * @return hashCode value */ @Override public int hashCode() { int h = 31; h = h * 17 + counter.hashCode(); return h; } /** * Prints the immutable value {@code SillyLazy} with attribute values. * @return A string representation of the value */ @Override public String toString() { return MoreObjects.toStringHelper("SillyLazy") .omitNullValues() .add("counter", counter) .toString(); } private volatile long lazyInitBitmap; private static final long VAL1_LAZY_INIT_BIT = 0x1L; private int val1; /** * {@inheritDoc} *

* Returns a lazily initialized value of the {@link SillyLazy#val1() val1} 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 int val1() { if ((lazyInitBitmap & VAL1_LAZY_INIT_BIT) == 0) { synchronized (this) { if ((lazyInitBitmap & VAL1_LAZY_INIT_BIT) == 0) { this.val1 = super.val1(); lazyInitBitmap |= VAL1_LAZY_INIT_BIT; } } } return val1; } private static final long VAL2_LAZY_INIT_BIT = 0x2L; private int val2; /** * {@inheritDoc} *

* Returns a lazily initialized value of the {@link SillyLazy#val2() val2} 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 int val2() { if ((lazyInitBitmap & VAL2_LAZY_INIT_BIT) == 0) { synchronized (this) { if ((lazyInitBitmap & VAL2_LAZY_INIT_BIT) == 0) { this.val2 = super.val2(); lazyInitBitmap |= VAL2_LAZY_INIT_BIT; } } } return val2; } /** * Construct a new immutable {@code SillyLazy} instance. * @param counter The value for the {@code counter} attribute * @return An immutable SillyLazy instance */ public static ImmutableSillyLazy of(AtomicInteger counter) { return new ImmutableSillyLazy(counter); } /** * Creates an immutable copy of a {@link SillyLazy} 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 SillyLazy instance */ public static ImmutableSillyLazy copyOf(SillyLazy instance) { if (instance instanceof ImmutableSillyLazy) { return (ImmutableSillyLazy) instance; } return ImmutableSillyLazy.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableSillyLazy ImmutableSillyLazy}. * @return A new ImmutableSillyLazy builder */ public static ImmutableSillyLazy.Builder builder() { return new ImmutableSillyLazy.Builder(); } /** * Builds instances of type {@link ImmutableSillyLazy ImmutableSillyLazy}. * 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_COUNTER = 0x1L; private long initBits = 0x1L; private @Nullable AtomicInteger counter; private Builder() { } /** * Fill a builder with attribute values from the provided {@code SillyLazy} 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(SillyLazy instance) { Preconditions.checkNotNull(instance, "instance"); counter(instance.counter()); return this; } /** * Initializes the value for the {@link SillyLazy#counter() counter} attribute. * @param counter The value for counter * @return {@code this} builder for use in a chained invocation */ public final Builder counter(AtomicInteger counter) { this.counter = Preconditions.checkNotNull(counter, "counter"); initBits &= ~INIT_BIT_COUNTER; return this; } /** * Builds a new {@link ImmutableSillyLazy ImmutableSillyLazy}. * @return An immutable instance of SillyLazy * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableSillyLazy build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableSillyLazy(null, counter); } private String formatRequiredAttributesMessage() { List attributes = Lists.newArrayList(); if ((initBits & INIT_BIT_COUNTER) != 0) attributes.add("counter"); return "Cannot build SillyLazy, some of required attributes are not set " + attributes; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy