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

org.cloudfoundry.multiapps.controller.process.variables.ImmutableCommaSeparatedValuesVariable 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.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.cloudfoundry.multiapps.common.Nullable;

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

* Use the builder to create immutable instances: * {@code ImmutableCommaSeparatedValuesVariable.builder()}. */ @SuppressWarnings({"all"}) public final class ImmutableCommaSeparatedValuesVariable extends CommaSeparatedValuesVariable { private final String name; private final @Nullable List defaultValue; private ImmutableCommaSeparatedValuesVariable( String name, @Nullable List 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 List getDefaultValue() { return defaultValue; } /** * Copy the current immutable object by setting a value for the {@link CommaSeparatedValuesVariable#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 ImmutableCommaSeparatedValuesVariable withName(String value) { String newValue = Objects.requireNonNull(value, "name"); if (this.name.equals(newValue)) return this; return new ImmutableCommaSeparatedValuesVariable(newValue, this.defaultValue); } /** * Copy the current immutable object with elements that replace the content of {@link CommaSeparatedValuesVariable#getDefaultValue() defaultValue}. * @param elements The elements to set * @return A modified copy of {@code this} object */ public final ImmutableCommaSeparatedValuesVariable withDefaultValue(@Nullable String... elements) { if (elements == null) { return new ImmutableCommaSeparatedValuesVariable(this.name, null); } @Nullable List newValue = Arrays.asList(elements) == null ? null : createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false)); return new ImmutableCommaSeparatedValuesVariable(this.name, newValue); } /** * Copy the current immutable object with elements that replace the content of {@link CommaSeparatedValuesVariable#getDefaultValue() defaultValue}. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param elements An iterable of defaultValue elements to set * @return A modified copy of {@code this} object */ public final ImmutableCommaSeparatedValuesVariable withDefaultValue(@Nullable Iterable elements) { if (this.defaultValue == elements) return this; @Nullable List newValue = elements == null ? null : createUnmodifiableList(false, createSafeList(elements, true, false)); return new ImmutableCommaSeparatedValuesVariable(this.name, newValue); } /** * This instance is equal to all instances of {@code ImmutableCommaSeparatedValuesVariable} 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 ImmutableCommaSeparatedValuesVariable && equalTo(0, (ImmutableCommaSeparatedValuesVariable) another); } private boolean equalTo(int synthetic, ImmutableCommaSeparatedValuesVariable 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 CommaSeparatedValuesVariable} with attribute values. * @return A string representation of the value */ @Override public String toString() { return "CommaSeparatedValuesVariable{" + "name=" + name + ", defaultValue=" + defaultValue + "}"; } /** * Creates an immutable copy of a {@link CommaSeparatedValuesVariable} 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 CommaSeparatedValuesVariable instance */ public static ImmutableCommaSeparatedValuesVariable copyOf(CommaSeparatedValuesVariable instance) { if (instance instanceof ImmutableCommaSeparatedValuesVariable) { return (ImmutableCommaSeparatedValuesVariable) instance; } return ImmutableCommaSeparatedValuesVariable.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableCommaSeparatedValuesVariable ImmutableCommaSeparatedValuesVariable}. *

   * ImmutableCommaSeparatedValuesVariable.builder()
   *    .name(String) // required {@link CommaSeparatedValuesVariable#getName() name}
   *    .defaultValue(List&lt;String&gt; | null) // nullable {@link CommaSeparatedValuesVariable#getDefaultValue() defaultValue}
   *    .build();
   * 
* @return A new ImmutableCommaSeparatedValuesVariable builder */ public static ImmutableCommaSeparatedValuesVariable.Builder builder() { return new ImmutableCommaSeparatedValuesVariable.Builder(); } /** * Builds instances of type {@link ImmutableCommaSeparatedValuesVariable ImmutableCommaSeparatedValuesVariable}. * 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 List defaultValue = null; private Builder() { } /** * Fill a builder with attribute values from the provided {@code CommaSeparatedValuesVariable} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * Collection elements and entries will be added, not replaced. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(CommaSeparatedValuesVariable instance) { Objects.requireNonNull(instance, "instance"); this.name(instance.getName()); @Nullable List defaultValueValue = instance.getDefaultValue(); if (defaultValueValue != null) { addAllDefaultValue(defaultValueValue); } return this; } /** * Initializes the value for the {@link CommaSeparatedValuesVariable#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; } /** * Adds one element to {@link CommaSeparatedValuesVariable#getDefaultValue() defaultValue} list. * @param element A defaultValue element * @return {@code this} builder for use in a chained invocation */ public final Builder addDefaultValue(String element) { if (this.defaultValue == null) { this.defaultValue = new ArrayList(); } this.defaultValue.add(Objects.requireNonNull(element, "defaultValue element")); return this; } /** * Adds elements to {@link CommaSeparatedValuesVariable#getDefaultValue() defaultValue} list. * @param elements An array of defaultValue elements * @return {@code this} builder for use in a chained invocation */ public final Builder addDefaultValue(String... elements) { if (this.defaultValue == null) { this.defaultValue = new ArrayList(); } for (String element : elements) { this.defaultValue.add(Objects.requireNonNull(element, "defaultValue element")); } return this; } /** * Sets or replaces all elements for {@link CommaSeparatedValuesVariable#getDefaultValue() defaultValue} list. * @param elements An iterable of defaultValue elements * @return {@code this} builder for use in a chained invocation */ public final Builder defaultValue(@Nullable Iterable elements) { if (elements == null) { this.defaultValue = null; return this; } this.defaultValue = new ArrayList(); return addAllDefaultValue(elements); } /** * Adds elements to {@link CommaSeparatedValuesVariable#getDefaultValue() defaultValue} list. * @param elements An iterable of defaultValue elements * @return {@code this} builder for use in a chained invocation */ public final Builder addAllDefaultValue(Iterable elements) { Objects.requireNonNull(elements, "defaultValue element"); if (this.defaultValue == null) { this.defaultValue = new ArrayList(); } for (String element : elements) { this.defaultValue.add(Objects.requireNonNull(element, "defaultValue element")); } return this; } /** * Builds a new {@link ImmutableCommaSeparatedValuesVariable ImmutableCommaSeparatedValuesVariable}. * @return An immutable instance of CommaSeparatedValuesVariable * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableCommaSeparatedValuesVariable build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableCommaSeparatedValuesVariable(name, defaultValue == null ? null : createUnmodifiableList(true, defaultValue)); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name"); return "Cannot build CommaSeparatedValuesVariable, some of required attributes are not set " + attributes; } } private static List createSafeList(Iterable iterable, boolean checkNulls, boolean skipNulls) { ArrayList list; if (iterable instanceof Collection) { int size = ((Collection) iterable).size(); if (size == 0) return Collections.emptyList(); list = new ArrayList<>(size); } else { list = new ArrayList<>(); } for (T element : iterable) { if (skipNulls && element == null) continue; if (checkNulls) Objects.requireNonNull(element, "element"); list.add(element); } return list; } private static List createUnmodifiableList(boolean clone, List list) { switch(list.size()) { case 0: return Collections.emptyList(); case 1: return Collections.singletonList(list.get(0)); default: if (clone) { return Collections.unmodifiableList(new ArrayList<>(list)); } else { if (list instanceof ArrayList) { ((ArrayList) list).trimToSize(); } return Collections.unmodifiableList(list); } } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy