
io.github.emm035.openapi.immutables.v3.security.ApiKeyScheme Maven / Gradle / Ivy
Show all versions of openapi-immutables Show documentation
package io.github.emm035.openapi.immutables.v3.security;
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.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.primitives.Booleans;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.github.emm035.openapi.immutables.v3.shared.Describable;
import io.github.emm035.openapi.immutables.v3.shared.Extensible;
import java.util.ArrayList;
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 AbstractApiKeyScheme}.
*
* Use the builder to create immutable instances:
* {@code ApiKeyScheme.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Immutable
@CheckReturnValue
public final class ApiKeyScheme extends AbstractApiKeyScheme {
private final String name;
private final AbstractApiKeyScheme.Location in;
private final SecurityScheme.Type type;
private final ImmutableMap extensions;
private final @Nullable String description;
private final boolean referential;
private ApiKeyScheme(
String name,
AbstractApiKeyScheme.Location in,
ImmutableMap extensions,
@Nullable String description) {
this.name = name;
this.in = in;
this.extensions = extensions;
this.description = description;
this.type = initShim.getType();
this.referential = initShim.isReferential();
this.initShim = null;
}
private static final int STAGE_INITIALIZING = -1;
private static final int STAGE_UNINITIALIZED = 0;
private static final int STAGE_INITIALIZED = 1;
private transient volatile InitShim initShim = new InitShim();
private final class InitShim {
private SecurityScheme.Type type;
private int typeBuildStage;
SecurityScheme.Type getType() {
if (typeBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (typeBuildStage == STAGE_UNINITIALIZED) {
typeBuildStage = STAGE_INITIALIZING;
this.type = Objects.requireNonNull(ApiKeyScheme.super.getType(), "type");
typeBuildStage = STAGE_INITIALIZED;
}
return this.type;
}
private boolean referential;
private int referentialBuildStage;
boolean isReferential() {
if (referentialBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (referentialBuildStage == STAGE_UNINITIALIZED) {
referentialBuildStage = STAGE_INITIALIZING;
this.referential = ApiKeyScheme.super.isReferential();
referentialBuildStage = STAGE_INITIALIZED;
}
return this.referential;
}
private String formatInitCycleMessage() {
ArrayList attributes = Lists.newArrayList();
if (typeBuildStage == STAGE_INITIALIZING) attributes.add("type");
if (referentialBuildStage == STAGE_INITIALIZING) attributes.add("referential");
return "Cannot build ApiKeyScheme, attribute initializers form cycle" + attributes;
}
}
/**
* @return The value of the {@code name} attribute
*/
@JsonProperty("name")
@Override
public String getName() {
return name;
}
/**
* @return The value of the {@code in} attribute
*/
@JsonProperty("in")
@Override
public AbstractApiKeyScheme.Location getIn() {
return in;
}
/**
* @return The computed-at-construction value of the {@code type} attribute
*/
@JsonProperty("type")
@Override
public SecurityScheme.Type getType() {
InitShim shim = this.initShim;
return shim != null
? shim.getType()
: this.type;
}
/**
* @return The value of the {@code extensions} attribute
*/
@JsonProperty("extensions")
@JsonAnyGetter
@Override
public ImmutableMap getExtensions() {
return extensions;
}
/**
* @return The value of the {@code description} attribute
*/
@JsonProperty("description")
@Override
public Optional getDescription() {
return Optional.ofNullable(description);
}
/**
* @return The computed-at-construction value of the {@code referential} attribute
*/
@JsonProperty("referential")
@JsonIgnore
@Override
public boolean isReferential() {
InitShim shim = this.initShim;
return shim != null
? shim.isReferential()
: this.referential;
}
/**
* Copy the current immutable object by setting a value for the {@link AbstractApiKeyScheme#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 ApiKeyScheme withName(String value) {
if (this.name.equals(value)) return this;
String newValue = Objects.requireNonNull(value, "name");
return validate(new ApiKeyScheme(newValue, this.in, this.extensions, this.description));
}
/**
* Copy the current immutable object by setting a value for the {@link AbstractApiKeyScheme#getIn() in} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for in
* @return A modified copy of the {@code this} object
*/
public final ApiKeyScheme withIn(AbstractApiKeyScheme.Location value) {
if (this.in == value) return this;
AbstractApiKeyScheme.Location newValue = Objects.requireNonNull(value, "in");
return validate(new ApiKeyScheme(this.name, newValue, this.extensions, this.description));
}
/**
* Copy the current immutable object by replacing the {@link AbstractApiKeyScheme#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 ApiKeyScheme withExtensions(Map entries) {
if (this.extensions == entries) return this;
ImmutableMap newValue = ImmutableMap.copyOf(entries);
return validate(new ApiKeyScheme(this.name, this.in, newValue, this.description));
}
/**
* Copy the current immutable object by setting a present value for the optional {@link AbstractApiKeyScheme#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 ApiKeyScheme withDescription(@Nullable String value) {
@Nullable String newValue = value;
if (Objects.equals(this.description, newValue)) return this;
return validate(new ApiKeyScheme(this.name, this.in, this.extensions, newValue));
}
/**
* Copy the current immutable object by setting an optional value for the {@link AbstractApiKeyScheme#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 ApiKeyScheme withDescription(Optional optional) {
@Nullable String value = optional.orElse(null);
if (Objects.equals(this.description, value)) return this;
return validate(new ApiKeyScheme(this.name, this.in, this.extensions, value));
}
/**
* This instance is equal to all instances of {@code ApiKeyScheme} 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 ApiKeyScheme
&& equalTo((ApiKeyScheme) another);
}
private boolean equalTo(ApiKeyScheme another) {
return name.equals(another.name)
&& in.equals(another.in)
&& type.equals(another.type)
&& extensions.equals(another.extensions)
&& Objects.equals(description, another.description)
&& referential == another.referential;
}
/**
* Computes a hash code from attributes: {@code name}, {@code in}, {@code type}, {@code extensions}, {@code description}, {@code referential}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + name.hashCode();
h += (h << 5) + in.hashCode();
h += (h << 5) + type.hashCode();
h += (h << 5) + extensions.hashCode();
h += (h << 5) + Objects.hashCode(description);
h += (h << 5) + Booleans.hashCode(referential);
return h;
}
/**
* Prints the immutable value {@code ApiKeyScheme} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("ApiKeyScheme")
.omitNullValues()
.add("name", name)
.add("in", in)
.add("type", type)
.add("extensions", extensions)
.add("description", description)
.add("referential", referential)
.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
@JsonTypeInfo(use=JsonTypeInfo.Id.NONE)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static final class Json extends AbstractApiKeyScheme {
@Nullable String name;
@Nullable AbstractApiKeyScheme.Location in;
@Nullable Optional description = Optional.empty();
final Map extensions = new HashMap();
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
@JsonProperty("in")
public void setIn(AbstractApiKeyScheme.Location in) {
this.in = in;
}
@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 String getName() { throw new UnsupportedOperationException(); }
@Override
public AbstractApiKeyScheme.Location getIn() { throw new UnsupportedOperationException(); }
@Override
public SecurityScheme.Type getType() { throw new UnsupportedOperationException(); }
@Override
public Map getExtensions() { throw new UnsupportedOperationException(); }
@Override
public Optional getDescription() { throw new UnsupportedOperationException(); }
@Override
public boolean isReferential() { 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 ApiKeyScheme fromJson(Json json) {
ApiKeyScheme.Builder builder = ApiKeyScheme.builder();
if (json.name != null) {
builder.setName(json.name);
}
if (json.in != null) {
builder.setIn(json.in);
}
if (json.extensions != null) {
builder.putAllExtensions(json.extensions);
}
if (json.description != null) {
builder.setDescription(json.description);
}
return builder.build();
}
private static ApiKeyScheme validate(ApiKeyScheme instance) {
instance = (ApiKeyScheme) instance.normalizeExtensions();
return instance;
}
/**
* Creates an immutable copy of a {@link AbstractApiKeyScheme} 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 ApiKeyScheme instance
*/
public static ApiKeyScheme copyOf(AbstractApiKeyScheme instance) {
if (instance instanceof ApiKeyScheme) {
return (ApiKeyScheme) instance;
}
return ApiKeyScheme.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ApiKeyScheme ApiKeyScheme}.
* @return A new ApiKeyScheme builder
*/
public static ApiKeyScheme.Builder builder() {
return new ApiKeyScheme.Builder();
}
/**
* Builds instances of type {@link ApiKeyScheme ApiKeyScheme}.
* 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_NAME = 0x1L;
private static final long INIT_BIT_IN = 0x2L;
private long initBits = 0x3L;
private @Nullable String name;
private @Nullable AbstractApiKeyScheme.Location in;
private ImmutableMap.Builder extensions = ImmutableMap.builder();
private @Nullable String description;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code io.github.emm035.openapi.immutables.v3.security.AbstractApiKeyScheme} 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(AbstractApiKeyScheme 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;
}
/**
* Fill a builder with attribute values from the provided {@code io.github.emm035.openapi.immutables.v3.shared.Describable} 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(Describable instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
if (object instanceof AbstractApiKeyScheme) {
AbstractApiKeyScheme instance = (AbstractApiKeyScheme) object;
setName(instance.getName());
setIn(instance.getIn());
}
if (object instanceof Extensible) {
Extensible instance = (Extensible) object;
putAllExtensions(instance.getExtensions());
}
if (object instanceof Describable) {
Describable instance = (Describable) object;
Optional descriptionOptional = instance.getDescription();
if (descriptionOptional.isPresent()) {
setDescription(descriptionOptional);
}
}
}
/**
* Initializes the value for the {@link AbstractApiKeyScheme#getName() name} attribute.
* @param name The value for name
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder setName(String name) {
this.name = Objects.requireNonNull(name, "name");
initBits &= ~INIT_BIT_NAME;
return this;
}
/**
* Initializes the value for the {@link AbstractApiKeyScheme#getIn() in} attribute.
* @param in The value for in
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder setIn(AbstractApiKeyScheme.Location in) {
this.in = Objects.requireNonNull(in, "in");
initBits &= ~INIT_BIT_IN;
return this;
}
/**
* Put one entry to the {@link AbstractApiKeyScheme#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 AbstractApiKeyScheme#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 AbstractApiKeyScheme#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 AbstractApiKeyScheme#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;
}
/**
* Initializes the optional value {@link AbstractApiKeyScheme#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 AbstractApiKeyScheme#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;
}
/**
* Builds a new {@link ApiKeyScheme ApiKeyScheme}.
* @return An immutable instance of ApiKeyScheme
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ApiKeyScheme build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return ApiKeyScheme.validate(new ApiKeyScheme(name, in, extensions.build(), description));
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name");
if ((initBits & INIT_BIT_IN) != 0) attributes.add("in");
return "Cannot build ApiKeyScheme, some of required attributes are not set " + attributes;
}
}
}