
io.github.emm035.openapi.immutables.v3.servers.ServerVariable Maven / Gradle / Ivy
Show all versions of openapi-immutables Show documentation
package io.github.emm035.openapi.immutables.v3.servers;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.github.emm035.openapi.immutables.v3.shared.Extensible;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link AbstractServerVariable}.
*
* Use the builder to create immutable instances:
* {@code ServerVariable.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Immutable
@CheckReturnValue
public final class ServerVariable
extends AbstractServerVariable {
private final ImmutableList getEnum;
private final String getDefault;
private final @Nullable String description;
private final ImmutableMap extensions;
private ServerVariable(
ImmutableList getEnum,
String getDefault,
@Nullable String description,
ImmutableMap extensions) {
this.getEnum = getEnum;
this.getDefault = getDefault;
this.description = description;
this.extensions = extensions;
}
/**
* @return The value of the {@code getEnum} attribute
*/
@JsonProperty("enum")
@Override
public ImmutableList getEnum() {
return getEnum;
}
/**
* @return The value of the {@code getDefault} attribute
*/
@JsonProperty("default")
@Override
public String getDefault() {
return getDefault;
}
/**
* @return The value of the {@code description} attribute
*/
@JsonProperty("description")
@Override
public Optional getDescription() {
return Optional.ofNullable(description);
}
/**
* @return The value of the {@code extensions} attribute
*/
@JsonProperty("extensions")
@JsonAnyGetter
@Override
public ImmutableMap getExtensions() {
return extensions;
}
/**
* Copy the current immutable object with elements that replace the content of {@link AbstractServerVariable#getEnum() enum}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ServerVariable withEnum(String... elements) {
ImmutableList newValue = ImmutableList.copyOf(elements);
return validate(new ServerVariable(newValue, this.getDefault, this.description, this.extensions));
}
/**
* Copy the current immutable object with elements that replace the content of {@link AbstractServerVariable#getEnum() enum}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of getEnum elements to set
* @return A modified copy of {@code this} object
*/
public final ServerVariable withEnum(Iterable elements) {
if (this.getEnum == elements) return this;
ImmutableList newValue = ImmutableList.copyOf(elements);
return validate(new ServerVariable(newValue, this.getDefault, this.description, this.extensions));
}
/**
* Copy the current immutable object by setting a value for the {@link AbstractServerVariable#getDefault() default} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for getDefault
* @return A modified copy of the {@code this} object
*/
public final ServerVariable withDefault(String value) {
if (this.getDefault.equals(value)) return this;
String newValue = Objects.requireNonNull(value, "getDefault");
return validate(new ServerVariable(this.getEnum, newValue, this.description, this.extensions));
}
/**
* Copy the current immutable object by setting a present value for the optional {@link AbstractServerVariable#getDescription() description} attribute.
* @param value The value for description, {@code null} is accepted as {@code java.util.Optional.empty()}
* @return A modified copy of {@code this} object
*/
public final ServerVariable withDescription(@Nullable String value) {
@Nullable String newValue = value;
if (Objects.equals(this.description, newValue)) return this;
return validate(new ServerVariable(this.getEnum, this.getDefault, newValue, this.extensions));
}
/**
* Copy the current immutable object by setting an optional value for the {@link AbstractServerVariable#getDescription() description} attribute.
* An equality check is used on inner nullable value to prevent copying of the same value by returning {@code this}.
* @param optional A value for description
* @return A modified copy of {@code this} object
*/
public final ServerVariable withDescription(Optional optional) {
@Nullable String value = optional.orElse(null);
if (Objects.equals(this.description, value)) return this;
return validate(new ServerVariable(this.getEnum, this.getDefault, value, this.extensions));
}
/**
* Copy the current immutable object by replacing the {@link AbstractServerVariable#getExtensions() extensions} map with the specified map.
* Nulls are not permitted as keys or values.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param entries The entries to be added to the extensions map
* @return A modified copy of {@code this} object
*/
public final ServerVariable withExtensions(Map entries) {
if (this.extensions == entries) return this;
ImmutableMap newValue = ImmutableMap.copyOf(entries);
return validate(new ServerVariable(this.getEnum, this.getDefault, this.description, newValue));
}
/**
* This instance is equal to all instances of {@code ServerVariable} 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 ServerVariable
&& equalTo((ServerVariable) another);
}
private boolean equalTo(ServerVariable another) {
return getEnum.equals(another.getEnum)
&& getDefault.equals(another.getDefault)
&& Objects.equals(description, another.description)
&& extensions.equals(another.extensions);
}
/**
* Computes a hash code from attributes: {@code getEnum}, {@code getDefault}, {@code description}, {@code extensions}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + getEnum.hashCode();
h += (h << 5) + getDefault.hashCode();
h += (h << 5) + Objects.hashCode(description);
h += (h << 5) + extensions.hashCode();
return h;
}
/**
* Prints the immutable value {@code ServerVariable} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("ServerVariable")
.omitNullValues()
.add("enum", getEnum)
.add("default", getDefault)
.add("description", description)
.add("extensions", extensions)
.toString();
}
/**
* 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
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static final class Json extends AbstractServerVariable {
@Nullable List getEnum = ImmutableList.of();
@Nullable String getDefault;
@Nullable Optional description = Optional.empty();
final Map extensions = new HashMap();
@JsonProperty("enum")
public void setEnum(List getEnum) {
this.getEnum = getEnum;
}
@JsonProperty("default")
public void setDefault(String getDefault) {
this.getDefault = getDefault;
}
@JsonProperty("description")
public void setDescription(Optional description) {
this.description = description;
}
@JsonAnySetter
public void setExtensions(String key, Object value) {
this.extensions.put(key, value);
}
@Override
public List getEnum() { throw new UnsupportedOperationException(); }
@Override
public String getDefault() { throw new UnsupportedOperationException(); }
@Override
public Optional getDescription() { throw new UnsupportedOperationException(); }
@Override
public Map getExtensions() { 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 ServerVariable fromJson(Json json) {
ServerVariable.Builder builder = ServerVariable.builder();
if (json.getEnum != null) {
builder.addAllEnum(json.getEnum);
}
if (json.getDefault != null) {
builder.setDefault(json.getDefault);
}
if (json.description != null) {
builder.setDescription(json.description);
}
if (json.extensions != null) {
builder.putAllExtensions(json.extensions);
}
return builder.build();
}
private static ServerVariable validate(ServerVariable instance) {
instance = (ServerVariable) instance.normalizeExtensions();
return instance;
}
/**
* Creates an immutable copy of a {@link AbstractServerVariable} 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 ServerVariable instance
*/
public static ServerVariable copyOf(AbstractServerVariable instance) {
if (instance instanceof ServerVariable) {
return (ServerVariable) instance;
}
return ServerVariable.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ServerVariable ServerVariable}.
* @return A new ServerVariable builder
*/
public static ServerVariable.Builder builder() {
return new ServerVariable.Builder();
}
/**
* Builds instances of type {@link ServerVariable ServerVariable}.
* 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.
*/
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_GET_DEFAULT = 0x1L;
private long initBits = 0x1L;
private ImmutableList.Builder getEnum = ImmutableList.builder();
private @Nullable String getDefault;
private @Nullable String description;
private ImmutableMap.Builder extensions = ImmutableMap.builder();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code io.github.emm035.openapi.immutables.v3.servers.AbstractServerVariable} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(AbstractServerVariable instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code io.github.emm035.openapi.immutables.v3.shared.Extensible} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(Extensible instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
if (object instanceof AbstractServerVariable) {
AbstractServerVariable instance = (AbstractServerVariable) object;
addAllEnum(instance.getEnum());
Optional descriptionOptional = instance.getDescription();
if (descriptionOptional.isPresent()) {
setDescription(descriptionOptional);
}
setDefault(instance.getDefault());
}
if (object instanceof Extensible) {
Extensible instance = (Extensible) object;
putAllExtensions(instance.getExtensions());
}
}
/**
* Adds one element to {@link AbstractServerVariable#getEnum() enum} list.
* @param element A getEnum element
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addEnum(String element) {
this.getEnum.add(element);
return this;
}
/**
* Adds elements to {@link AbstractServerVariable#getEnum() enum} list.
* @param elements An array of getEnum elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addEnum(String... elements) {
this.getEnum.add(elements);
return this;
}
/**
* Sets or replaces all elements for {@link AbstractServerVariable#getEnum() enum} list.
* @param elements An iterable of getEnum elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder setEnum(Iterable elements) {
this.getEnum = ImmutableList.builder();
return addAllEnum(elements);
}
/**
* Adds elements to {@link AbstractServerVariable#getEnum() enum} list.
* @param elements An iterable of getEnum elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addAllEnum(Iterable elements) {
this.getEnum.addAll(elements);
return this;
}
/**
* Initializes the value for the {@link AbstractServerVariable#getDefault() default} attribute.
* @param getDefault The value for getDefault
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder setDefault(String getDefault) {
this.getDefault = Objects.requireNonNull(getDefault, "getDefault");
initBits &= ~INIT_BIT_GET_DEFAULT;
return this;
}
/**
* Initializes the optional value {@link AbstractServerVariable#getDescription() description} to description.
* @param description The value for description, {@code null} is accepted as {@code java.util.Optional.empty()}
* @return {@code this} builder for chained invocation
*/
@CanIgnoreReturnValue
public final Builder setDescription(@Nullable String description) {
this.description = description;
return this;
}
/**
* Initializes the optional value {@link AbstractServerVariable#getDescription() description} to description.
* @param description The value for description
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder setDescription(Optional description) {
this.description = description.orElse(null);
return this;
}
/**
* Put one entry to the {@link AbstractServerVariable#getExtensions() extensions} map.
* @param key The key in the extensions map
* @param value The associated value in the extensions map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonAnySetter
public final Builder putExtensions(String key, Object value) {
this.extensions.put(key, value);
return this;
}
/**
* Put one entry to the {@link AbstractServerVariable#getExtensions() extensions} map. Nulls are not permitted
* @param entry The key and value entry
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putExtensions(Map.Entry entry) {
this.extensions.put(entry);
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link AbstractServerVariable#getExtensions() extensions} map. Nulls are not permitted
* @param extensions The entries that will be added to the extensions map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder setExtensions(Map extensions) {
this.extensions = ImmutableMap.builder();
return putAllExtensions(extensions);
}
/**
* Put all mappings from the specified map as entries to {@link AbstractServerVariable#getExtensions() extensions} map. Nulls are not permitted
* @param extensions The entries that will be added to the extensions map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putAllExtensions(Map extensions) {
this.extensions.putAll(extensions);
return this;
}
/**
* Builds a new {@link ServerVariable ServerVariable}.
* @return An immutable instance of ServerVariable
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ServerVariable build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return ServerVariable.validate(new ServerVariable(getEnum.build(), getDefault, description, extensions.build()));
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if ((initBits & INIT_BIT_GET_DEFAULT) != 0) attributes.add("default");
return "Cannot build ServerVariable, some of required attributes are not set " + attributes;
}
}
}