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

nonimmutables.bravo.ImmutableFoo Maven / Gradle / Ivy

There is a newer version: 2.10.1
Show newest version
package nonimmutables.bravo;

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 Foo}.
 * 

* Use the builder to create immutable instances: * {@code ImmutableFoo.builder()}. */ @Generated(from = "Foo", generator = "Immutables") @SuppressWarnings({"all"}) @ParametersAreNonnullByDefault @javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor") @Immutable @CheckReturnValue public final class ImmutableFoo implements Foo { private final String name; private ImmutableFoo(String name) { this.name = name; } /** * @return The value of the {@code name} attribute */ @Override public String getName() { return name; } /** * Copy the current immutable object by setting a value for the {@link Foo#getName() name} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for name * @return A modified copy of the {@code this} object */ public final ImmutableFoo withName(String value) { String newValue = Objects.requireNonNull(value, "name"); if (this.name.equals(newValue)) return this; return new ImmutableFoo(newValue); } /** * This instance is equal to all instances of {@code ImmutableFoo} 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 ImmutableFoo && equalTo(0, (ImmutableFoo) another); } private boolean equalTo(int synthetic, ImmutableFoo another) { return name.equals(another.name); } /** * Computes a hash code from attributes: {@code name}. * @return hashCode value */ @Override public int hashCode() { @Var int h = 5381; h += (h << 5) + name.hashCode(); return h; } /** * Prints the immutable value {@code Foo} with attribute values. * @return A string representation of the value */ @Override public String toString() { return MoreObjects.toStringHelper("Foo") .omitNullValues() .add("name", name) .toString(); } /** * Creates an immutable copy of a {@link Foo} 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 Foo instance */ public static ImmutableFoo copyOf(Foo instance) { if (instance instanceof ImmutableFoo) { return (ImmutableFoo) instance; } return ImmutableFoo.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableFoo ImmutableFoo}. *

   * ImmutableFoo.builder()
   *    .name(String) // required {@link Foo#getName() name}
   *    .build();
   * 
* @return A new ImmutableFoo builder */ public static ImmutableFoo.Builder builder() { return new ImmutableFoo.Builder(); } /** * Builds instances of type {@link ImmutableFoo ImmutableFoo}. * 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 = "Foo", generator = "Immutables") @NotThreadSafe public static final class Builder { private static final long INIT_BIT_NAME = 0x1L; private long initBits = 0x1L; private @Nullable String name; private Builder() { } /** * Fill a builder with attribute values from the provided {@code Foo} 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(Foo instance) { Objects.requireNonNull(instance, "instance"); name(instance.getName()); return this; } /** * Initializes the value for the {@link Foo#getName() name} attribute. * @param name The value for name * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder name(String name) { this.name = Objects.requireNonNull(name, "name"); initBits &= ~INIT_BIT_NAME; return this; } /** * Builds a new {@link ImmutableFoo ImmutableFoo}. * @return An immutable instance of Foo * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableFoo build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableFoo(name); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name"); return "Cannot build Foo, some of required attributes are not set " + attributes; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy