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

org.cloudfoundry.multiapps.controller.process.variables.ImmutableEnumVariable Maven / Gradle / Ivy

There is a newer version: 1.183.0
Show newest version
package org.cloudfoundry.multiapps.controller.process.variables;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.cloudfoundry.multiapps.common.Nullable;

/**
 * Immutable implementation of {@link EnumVariable}.
 * 

* Use the builder to create immutable instances: * {@code ImmutableEnumVariable.builder()}. */ @SuppressWarnings({"all"}) public final class ImmutableEnumVariable> extends EnumVariable { private final String name; private final @Nullable T defaultValue; private final Class type; private ImmutableEnumVariable( String name, @Nullable T defaultValue, Class type) { this.name = name; this.defaultValue = defaultValue; this.type = type; } /** * @return The value of the {@code name} attribute */ @Override public String getName() { return name; } /** * @return The value of the {@code defaultValue} attribute */ @Override public @Nullable T getDefaultValue() { return defaultValue; } /** * @return The value of the {@code type} attribute */ @Override public Class getType() { return type; } /** * Copy the current immutable object by setting a value for the {@link EnumVariable#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 ImmutableEnumVariable withName(String value) { String newValue = Objects.requireNonNull(value, "name"); if (this.name.equals(newValue)) return this; return new ImmutableEnumVariable<>(newValue, this.defaultValue, this.type); } /** * Copy the current immutable object by setting a value for the {@link EnumVariable#getDefaultValue() defaultValue} attribute. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for defaultValue (can be {@code null}) * @return A modified copy of the {@code this} object */ public final ImmutableEnumVariable withDefaultValue(@Nullable T value) { if (this.defaultValue == value) return this; return new ImmutableEnumVariable<>(this.name, value, this.type); } /** * Copy the current immutable object by setting a value for the {@link EnumVariable#getType() type} attribute. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for type * @return A modified copy of the {@code this} object */ public final ImmutableEnumVariable withType(Class value) { if (this.type == value) return this; Class newValue = Objects.requireNonNull(value, "type"); return new ImmutableEnumVariable<>(this.name, this.defaultValue, newValue); } /** * This instance is equal to all instances of {@code ImmutableEnumVariable} that have equal attribute values. * @return {@code true} if {@code this} is equal to {@code another} instance */ @Override public boolean equals(Object another) { if (this == another) return true; return another instanceof ImmutableEnumVariable && equalTo(0, (ImmutableEnumVariable) another); } private boolean equalTo(int synthetic, ImmutableEnumVariable another) { return name.equals(another.name) && Objects.equals(defaultValue, another.defaultValue) && type.equals(another.type); } /** * Computes a hash code from attributes: {@code name}, {@code defaultValue}, {@code type}. * @return hashCode value */ @Override public int hashCode() { int h = 5381; h += (h << 5) + name.hashCode(); h += (h << 5) + Objects.hashCode(defaultValue); h += (h << 5) + type.hashCode(); return h; } /** * Prints the immutable value {@code EnumVariable} with attribute values. * @return A string representation of the value */ @Override public String toString() { return "EnumVariable{" + "name=" + name + ", defaultValue=" + defaultValue + ", type=" + type + "}"; } /** * Creates an immutable copy of a {@link EnumVariable} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param generic parameter T * @param instance The instance to copy * @return A copied immutable EnumVariable instance */ public static > ImmutableEnumVariable copyOf(EnumVariable instance) { if (instance instanceof ImmutableEnumVariable) { return (ImmutableEnumVariable) instance; } return ImmutableEnumVariable.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableEnumVariable ImmutableEnumVariable}. *

   * ImmutableEnumVariable.&lt;T&gt;builder()
   *    .name(String) // required {@link EnumVariable#getName() name}
   *    .defaultValue(T | null) // nullable {@link EnumVariable#getDefaultValue() defaultValue}
   *    .type(Class&lt;T&gt;) // required {@link EnumVariable#getType() type}
   *    .build();
   * 
* @param generic parameter T * @return A new ImmutableEnumVariable builder */ public static > ImmutableEnumVariable.Builder builder() { return new ImmutableEnumVariable.Builder<>(); } /** * Builds instances of type {@link ImmutableEnumVariable ImmutableEnumVariable}. * 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. */ public static final class Builder> { private static final long INIT_BIT_NAME = 0x1L; private static final long INIT_BIT_TYPE = 0x2L; private long initBits = 0x3L; private String name; private T defaultValue; private Class type; private Builder() { } /** * Fill a builder with attribute values from the provided {@code EnumVariable} 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(EnumVariable instance) { Objects.requireNonNull(instance, "instance"); this.name(instance.getName()); @Nullable T defaultValueValue = instance.getDefaultValue(); if (defaultValueValue != null) { defaultValue(defaultValueValue); } this.type(instance.getType()); return this; } /** * Initializes the value for the {@link EnumVariable#getName() name} attribute. * @param name The value for name * @return {@code this} builder for use in a chained invocation */ public final Builder name(String name) { this.name = Objects.requireNonNull(name, "name"); initBits &= ~INIT_BIT_NAME; return this; } /** * Initializes the value for the {@link EnumVariable#getDefaultValue() defaultValue} attribute. * @param defaultValue The value for defaultValue (can be {@code null}) * @return {@code this} builder for use in a chained invocation */ public final Builder defaultValue(@Nullable T defaultValue) { this.defaultValue = defaultValue; return this; } /** * Initializes the value for the {@link EnumVariable#getType() type} attribute. * @param type The value for type * @return {@code this} builder for use in a chained invocation */ public final Builder type(Class type) { this.type = Objects.requireNonNull(type, "type"); initBits &= ~INIT_BIT_TYPE; return this; } /** * Builds a new {@link ImmutableEnumVariable ImmutableEnumVariable}. * @return An immutable instance of EnumVariable * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableEnumVariable build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableEnumVariable<>(name, defaultValue, type); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name"); if ((initBits & INIT_BIT_TYPE) != 0) attributes.add("type"); return "Cannot build EnumVariable, some of required attributes are not set " + attributes; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy