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

org.cloudfoundry.multiapps.controller.api.model.ImmutableParameterMetadata Maven / Gradle / Ivy

package org.cloudfoundry.multiapps.controller.api.model;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.cloudfoundry.multiapps.common.Nullable;
import org.cloudfoundry.multiapps.controller.api.model.parameters.ParameterConverter;

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

* Use the builder to create immutable instances: * {@code ImmutableParameterMetadata.builder()}. */ @SuppressWarnings({"all"}) public final class ImmutableParameterMetadata implements ParameterMetadata { private final String id; private final @Nullable Object defaultValue; private final boolean required; private final ParameterType type; private final @Nullable ParameterConverter customConverter; private ImmutableParameterMetadata(ImmutableParameterMetadata.Builder builder) { this.id = builder.id; this.defaultValue = builder.defaultValue; this.type = builder.type; this.customConverter = builder.customConverter; this.required = builder.requiredIsSet() ? builder.required : ParameterMetadata.super.getRequired(); } private ImmutableParameterMetadata( String id, @Nullable Object defaultValue, boolean required, ParameterType type, @Nullable ParameterConverter customConverter) { this.id = id; this.defaultValue = defaultValue; this.required = required; this.type = type; this.customConverter = customConverter; } /** * @return The value of the {@code id} attribute */ @JsonProperty("id") @Override public String getId() { return id; } /** * @return The value of the {@code defaultValue} attribute */ @JsonProperty("defaultValue") @Override public @Nullable Object getDefaultValue() { return defaultValue; } /** * @return The value of the {@code required} attribute */ @JsonProperty("required") @Override public boolean getRequired() { return required; } /** * @return The value of the {@code type} attribute */ @JsonProperty("type") @Override public ParameterType getType() { return type; } /** * @return The value of the {@code customConverter} attribute */ @JsonProperty("customConverter") @Override public @Nullable ParameterConverter getCustomConverter() { return customConverter; } /** * Copy the current immutable object by setting a value for the {@link ParameterMetadata#getId() id} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for id * @return A modified copy of the {@code this} object */ public final ImmutableParameterMetadata withId(String value) { String newValue = Objects.requireNonNull(value, "id"); if (this.id.equals(newValue)) return this; return new ImmutableParameterMetadata(newValue, this.defaultValue, this.required, this.type, this.customConverter); } /** * Copy the current immutable object by setting a value for the {@link ParameterMetadata#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 ImmutableParameterMetadata withDefaultValue(@Nullable Object value) { if (this.defaultValue == value) return this; return new ImmutableParameterMetadata(this.id, value, this.required, this.type, this.customConverter); } /** * Copy the current immutable object by setting a value for the {@link ParameterMetadata#getRequired() required} attribute. * A value equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for required * @return A modified copy of the {@code this} object */ public final ImmutableParameterMetadata withRequired(boolean value) { if (this.required == value) return this; return new ImmutableParameterMetadata(this.id, this.defaultValue, value, this.type, this.customConverter); } /** * Copy the current immutable object by setting a value for the {@link ParameterMetadata#getType() type} attribute. * A value 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 ImmutableParameterMetadata withType(ParameterType value) { ParameterType newValue = Objects.requireNonNull(value, "type"); if (this.type == newValue) return this; return new ImmutableParameterMetadata(this.id, this.defaultValue, this.required, newValue, this.customConverter); } /** * Copy the current immutable object by setting a value for the {@link ParameterMetadata#getCustomConverter() customConverter} 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 customConverter (can be {@code null}) * @return A modified copy of the {@code this} object */ public final ImmutableParameterMetadata withCustomConverter(@Nullable ParameterConverter value) { if (this.customConverter == value) return this; return new ImmutableParameterMetadata(this.id, this.defaultValue, this.required, this.type, value); } /** * This instance is equal to all instances of {@code ImmutableParameterMetadata} 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 ImmutableParameterMetadata && equalTo(0, (ImmutableParameterMetadata) another); } private boolean equalTo(int synthetic, ImmutableParameterMetadata another) { return id.equals(another.id) && Objects.equals(defaultValue, another.defaultValue) && required == another.required && type.equals(another.type) && Objects.equals(customConverter, another.customConverter); } /** * Computes a hash code from attributes: {@code id}, {@code defaultValue}, {@code required}, {@code type}, {@code customConverter}. * @return hashCode value */ @Override public int hashCode() { int h = 5381; h += (h << 5) + id.hashCode(); h += (h << 5) + Objects.hashCode(defaultValue); h += (h << 5) + Boolean.hashCode(required); h += (h << 5) + type.hashCode(); h += (h << 5) + Objects.hashCode(customConverter); return h; } /** * Prints the immutable value {@code ParameterMetadata} with attribute values. * @return A string representation of the value */ @Override public String toString() { return "ParameterMetadata{" + "id=" + id + ", defaultValue=" + defaultValue + ", required=" + required + ", type=" + type + ", customConverter=" + customConverter + "}"; } /** * Utility type used to correctly read immutable object from JSON representation. * @deprecated Do not use this type directly, it exists only for the Jackson-binding infrastructure */ @Deprecated @JsonDeserialize @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE) static final class Json implements ParameterMetadata { String id; Object defaultValue; boolean required; boolean requiredIsSet; ParameterType type; ParameterConverter customConverter; @JsonProperty("id") public void setId(String id) { this.id = id; } @JsonProperty("defaultValue") public void setDefaultValue(@Nullable Object defaultValue) { this.defaultValue = defaultValue; } @JsonProperty("required") public void setRequired(boolean required) { this.required = required; this.requiredIsSet = true; } @JsonProperty("type") public void setType(ParameterType type) { this.type = type; } @JsonProperty("customConverter") public void setCustomConverter(@Nullable ParameterConverter customConverter) { this.customConverter = customConverter; } @Override public String getId() { throw new UnsupportedOperationException(); } @Override public Object getDefaultValue() { throw new UnsupportedOperationException(); } @Override public boolean getRequired() { throw new UnsupportedOperationException(); } @Override public ParameterType getType() { throw new UnsupportedOperationException(); } @Override public ParameterConverter getCustomConverter() { throw new UnsupportedOperationException(); } } /** * @param json A JSON-bindable data structure * @return An immutable value type * @deprecated Do not use this method directly, it exists only for the Jackson-binding infrastructure */ @Deprecated @JsonCreator(mode = JsonCreator.Mode.DELEGATING) static ImmutableParameterMetadata fromJson(Json json) { ImmutableParameterMetadata.Builder builder = ImmutableParameterMetadata.builder(); if (json.id != null) { builder.id(json.id); } if (json.defaultValue != null) { builder.defaultValue(json.defaultValue); } if (json.requiredIsSet) { builder.required(json.required); } if (json.type != null) { builder.type(json.type); } if (json.customConverter != null) { builder.customConverter(json.customConverter); } return builder.build(); } /** * Creates an immutable copy of a {@link ParameterMetadata} 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 ParameterMetadata instance */ public static ImmutableParameterMetadata copyOf(ParameterMetadata instance) { if (instance instanceof ImmutableParameterMetadata) { return (ImmutableParameterMetadata) instance; } return ImmutableParameterMetadata.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableParameterMetadata ImmutableParameterMetadata}. *

   * ImmutableParameterMetadata.builder()
   *    .id(String) // required {@link ParameterMetadata#getId() id}
   *    .defaultValue(Object | null) // nullable {@link ParameterMetadata#getDefaultValue() defaultValue}
   *    .required(boolean) // optional {@link ParameterMetadata#getRequired() required}
   *    .type(org.cloudfoundry.multiapps.controller.api.model.ParameterType) // required {@link ParameterMetadata#getType() type}
   *    .customConverter(org.cloudfoundry.multiapps.controller.api.model.parameters.ParameterConverter | null) // nullable {@link ParameterMetadata#getCustomConverter() customConverter}
   *    .build();
   * 
* @return A new ImmutableParameterMetadata builder */ public static ImmutableParameterMetadata.Builder builder() { return new ImmutableParameterMetadata.Builder(); } /** * Builds instances of type {@link ImmutableParameterMetadata ImmutableParameterMetadata}. * 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_ID = 0x1L; private static final long INIT_BIT_TYPE = 0x2L; private static final long OPT_BIT_REQUIRED = 0x1L; private long initBits = 0x3L; private long optBits; private String id; private Object defaultValue; private boolean required; private ParameterType type; private ParameterConverter customConverter; private Builder() { } /** * Fill a builder with attribute values from the provided {@code ParameterMetadata} 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(ParameterMetadata instance) { Objects.requireNonNull(instance, "instance"); this.id(instance.getId()); @Nullable Object defaultValueValue = instance.getDefaultValue(); if (defaultValueValue != null) { defaultValue(defaultValueValue); } this.required(instance.getRequired()); this.type(instance.getType()); @Nullable ParameterConverter customConverterValue = instance.getCustomConverter(); if (customConverterValue != null) { customConverter(customConverterValue); } return this; } /** * Initializes the value for the {@link ParameterMetadata#getId() id} attribute. * @param id The value for id * @return {@code this} builder for use in a chained invocation */ @JsonProperty("id") public final Builder id(String id) { this.id = Objects.requireNonNull(id, "id"); initBits &= ~INIT_BIT_ID; return this; } /** * Initializes the value for the {@link ParameterMetadata#getDefaultValue() defaultValue} attribute. * @param defaultValue The value for defaultValue (can be {@code null}) * @return {@code this} builder for use in a chained invocation */ @JsonProperty("defaultValue") public final Builder defaultValue(@Nullable Object defaultValue) { this.defaultValue = defaultValue; return this; } /** * Initializes the value for the {@link ParameterMetadata#getRequired() required} attribute. *

If not set, this attribute will have a default value as returned by the initializer of {@link ParameterMetadata#getRequired() required}. * @param required The value for required * @return {@code this} builder for use in a chained invocation */ @JsonProperty("required") public final Builder required(boolean required) { this.required = required; optBits |= OPT_BIT_REQUIRED; return this; } /** * Initializes the value for the {@link ParameterMetadata#getType() type} attribute. * @param type The value for type * @return {@code this} builder for use in a chained invocation */ @JsonProperty("type") public final Builder type(ParameterType type) { this.type = Objects.requireNonNull(type, "type"); initBits &= ~INIT_BIT_TYPE; return this; } /** * Initializes the value for the {@link ParameterMetadata#getCustomConverter() customConverter} attribute. * @param customConverter The value for customConverter (can be {@code null}) * @return {@code this} builder for use in a chained invocation */ @JsonProperty("customConverter") public final Builder customConverter(@Nullable ParameterConverter customConverter) { this.customConverter = customConverter; return this; } /** * Builds a new {@link ImmutableParameterMetadata ImmutableParameterMetadata}. * @return An immutable instance of ParameterMetadata * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableParameterMetadata build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableParameterMetadata(this); } private boolean requiredIsSet() { return (optBits & OPT_BIT_REQUIRED) != 0; } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_ID) != 0) attributes.add("id"); if ((initBits & INIT_BIT_TYPE) != 0) attributes.add("type"); return "Cannot build ParameterMetadata, some of required attributes are not set " + attributes; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy