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

org.cloudfoundry.multiapps.controller.process.variables.ImmutableSimpleVariable 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 SimpleVariable}.
 * 

* Use the builder to create immutable instances: * {@code ImmutableSimpleVariable.builder()}. */ @SuppressWarnings({"all"}) public final class ImmutableSimpleVariable extends SimpleVariable { private final String name; private final @Nullable T defaultValue; private ImmutableSimpleVariable(String name, @Nullable T defaultValue) { this.name = name; this.defaultValue = defaultValue; } /** * @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; } /** * Copy the current immutable object by setting a value for the {@link SimpleVariable#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 ImmutableSimpleVariable withName(String value) { String newValue = Objects.requireNonNull(value, "name"); if (this.name.equals(newValue)) return this; return new ImmutableSimpleVariable<>(newValue, this.defaultValue); } /** * Copy the current immutable object by setting a value for the {@link SimpleVariable#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 ImmutableSimpleVariable withDefaultValue(@Nullable T value) { if (this.defaultValue == value) return this; return new ImmutableSimpleVariable<>(this.name, value); } /** * This instance is equal to all instances of {@code ImmutableSimpleVariable} 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 ImmutableSimpleVariable && equalTo(0, (ImmutableSimpleVariable) another); } private boolean equalTo(int synthetic, ImmutableSimpleVariable another) { return name.equals(another.name) && Objects.equals(defaultValue, another.defaultValue); } /** * Computes a hash code from attributes: {@code name}, {@code defaultValue}. * @return hashCode value */ @Override public int hashCode() { int h = 5381; h += (h << 5) + name.hashCode(); h += (h << 5) + Objects.hashCode(defaultValue); return h; } /** * Prints the immutable value {@code SimpleVariable} with attribute values. * @return A string representation of the value */ @Override public String toString() { return "SimpleVariable{" + "name=" + name + ", defaultValue=" + defaultValue + "}"; } /** * Creates an immutable copy of a {@link SimpleVariable} 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 SimpleVariable instance */ public static ImmutableSimpleVariable copyOf(SimpleVariable instance) { if (instance instanceof ImmutableSimpleVariable) { return (ImmutableSimpleVariable) instance; } return ImmutableSimpleVariable.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableSimpleVariable ImmutableSimpleVariable}. *

   * ImmutableSimpleVariable.&lt;T&gt;builder()
   *    .name(String) // required {@link SimpleVariable#getName() name}
   *    .defaultValue(T | null) // nullable {@link SimpleVariable#getDefaultValue() defaultValue}
   *    .build();
   * 
* @param generic parameter T * @return A new ImmutableSimpleVariable builder */ public static ImmutableSimpleVariable.Builder builder() { return new ImmutableSimpleVariable.Builder<>(); } /** * Builds instances of type {@link ImmutableSimpleVariable ImmutableSimpleVariable}. * 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 long initBits = 0x1L; private String name; private T defaultValue; private Builder() { } /** * Fill a builder with attribute values from the provided {@code org.cloudfoundry.multiapps.controller.process.variables.Variable} instance. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(Variable instance) { Objects.requireNonNull(instance, "instance"); from((short) 0, (Object) instance); return this; } /** * Fill a builder with attribute values from the provided {@code org.cloudfoundry.multiapps.controller.process.variables.SimpleVariable} instance. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(SimpleVariable instance) { Objects.requireNonNull(instance, "instance"); from((short) 0, (Object) instance); return this; } @SuppressWarnings("unchecked") private void from(short _unused, Object object) { if (object instanceof Variable) { Variable instance = (Variable) object; this.name(instance.getName()); @Nullable T defaultValueValue = instance.getDefaultValue(); if (defaultValueValue != null) { defaultValue(defaultValueValue); } } } /** * Initializes the value for the {@link SimpleVariable#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 SimpleVariable#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; } /** * Builds a new {@link ImmutableSimpleVariable ImmutableSimpleVariable}. * @return An immutable instance of SimpleVariable * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableSimpleVariable build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableSimpleVariable<>(name, defaultValue); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name"); return "Cannot build SimpleVariable, some of required attributes are not set " + attributes; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy