org.mandas.docker.client.messages.swarm.ImmutableSecretSpec Maven / Gradle / Ivy
package org.mandas.docker.client.messages.swarm;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.mandas.docker.Nullable;
/**
* Immutable implementation of {@link SecretSpec}.
*
* Use the builder to create immutable instances:
* {@code ImmutableSecretSpec.builder()}.
*/
@SuppressWarnings({"all"})
final class ImmutableSecretSpec implements SecretSpec {
private final String name;
private final @Nullable Map labels;
private final @Nullable String data;
private ImmutableSecretSpec(
String name,
@Nullable Map labels,
@Nullable String data) {
this.name = name;
this.labels = labels;
this.data = data;
}
/**
* @return The value of the {@code name} attribute
*/
@JsonProperty("Name")
@Override
public String name() {
return name;
}
/**
* @return The value of the {@code labels} attribute
*/
@JsonProperty("Labels")
@Override
public @Nullable Map labels() {
return labels;
}
/**
* @return The value of the {@code data} attribute
*/
@JsonProperty("Data")
@Override
public @Nullable String data() {
return data;
}
/**
* Copy the current immutable object by setting a value for the {@link SecretSpec#name() 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 ImmutableSecretSpec withName(String value) {
String newValue = Objects.requireNonNull(value, "name");
if (this.name.equals(newValue)) return this;
return new ImmutableSecretSpec(newValue, this.labels, this.data);
}
/**
* Copy the current immutable object by replacing the {@link SecretSpec#labels() labels} 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 labels map
* @return A modified copy of {@code this} object
*/
public final ImmutableSecretSpec withLabels(@Nullable Map entries) {
if (this.labels == entries) return this;
@Nullable Map newValue = entries == null ? null : createUnmodifiableMap(true, false, entries);
return new ImmutableSecretSpec(this.name, newValue, this.data);
}
/**
* Copy the current immutable object by setting a value for the {@link SecretSpec#data() data} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for data (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableSecretSpec withData(@Nullable String value) {
if (Objects.equals(this.data, value)) return this;
return new ImmutableSecretSpec(this.name, this.labels, value);
}
/**
* This instance is equal to all instances of {@code ImmutableSecretSpec} 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 ImmutableSecretSpec
&& equalTo(0, (ImmutableSecretSpec) another);
}
private boolean equalTo(int synthetic, ImmutableSecretSpec another) {
return name.equals(another.name)
&& Objects.equals(labels, another.labels)
&& Objects.equals(data, another.data);
}
/**
* Computes a hash code from attributes: {@code name}, {@code labels}, {@code data}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + name.hashCode();
h += (h << 5) + Objects.hashCode(labels);
h += (h << 5) + Objects.hashCode(data);
return h;
}
/**
* Prints the immutable value {@code SecretSpec} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "SecretSpec{"
+ "name=" + name
+ ", labels=" + labels
+ ", data=" + data
+ "}";
}
/**
* Creates an immutable copy of a {@link SecretSpec} 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 SecretSpec instance
*/
public static ImmutableSecretSpec copyOf(SecretSpec instance) {
if (instance instanceof ImmutableSecretSpec) {
return (ImmutableSecretSpec) instance;
}
return ImmutableSecretSpec.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableSecretSpec ImmutableSecretSpec}.
*
* ImmutableSecretSpec.builder()
* .name(String) // required {@link SecretSpec#name() name}
* .labels(Map<String, String> | null) // nullable {@link SecretSpec#labels() labels}
* .data(String | null) // nullable {@link SecretSpec#data() data}
* .build();
*
* @return A new ImmutableSecretSpec builder
*/
public static ImmutableSecretSpec.Builder builder() {
return new ImmutableSecretSpec.Builder();
}
/**
* Builds instances of type {@link ImmutableSecretSpec ImmutableSecretSpec}.
* 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.
*/
static final class Builder implements SecretSpec.Builder {
private static final long INIT_BIT_NAME = 0x1L;
private long initBits = 0x1L;
private String name;
private Map labels = null;
private String data;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code SecretSpec} 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(SecretSpec instance) {
Objects.requireNonNull(instance, "instance");
name(instance.name());
@Nullable Map labelsValue = instance.labels();
if (labelsValue != null) {
putAllLabels(labelsValue);
}
@Nullable String dataValue = instance.data();
if (dataValue != null) {
data(dataValue);
}
return this;
}
/**
* Initializes the value for the {@link SecretSpec#name() name} attribute.
* @param name The value for name
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("Name")
public final Builder name(String name) {
this.name = Objects.requireNonNull(name, "name");
initBits &= ~INIT_BIT_NAME;
return this;
}
/**
* Put one entry to the {@link SecretSpec#labels() labels} map.
* @param key The key in the labels map
* @param value The associated value in the labels map
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addLabel(String key, String value) {
if (this.labels == null) {
this.labels = new LinkedHashMap();
}
this.labels.put(
Objects.requireNonNull(key, "labels key"),
Objects.requireNonNull(value, value == null ? "labels value for key: " + key : null));
return this;
}
/**
* Put one entry to the {@link SecretSpec#labels() labels} map. Nulls are not permitted
* @param entry The key and value entry
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addLabel(Map.Entry entry) {
if (this.labels == null) {
this.labels = new LinkedHashMap();
}
String k = entry.getKey();
String v = entry.getValue();
this.labels.put(
Objects.requireNonNull(k, "labels key"),
Objects.requireNonNull(v, v == null ? "labels value for key: " + k : null));
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link SecretSpec#labels() labels} map. Nulls are not permitted as keys or values, but parameter itself can be null
* @param entries The entries that will be added to the labels map
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("Labels")
public final Builder labels(@Nullable Map entries) {
if (entries == null) {
this.labels = null;
return this;
}
this.labels = new LinkedHashMap();
return putAllLabels(entries);
}
/**
* Put all mappings from the specified map as entries to {@link SecretSpec#labels() labels} map. Nulls are not permitted
* @param entries The entries that will be added to the labels map
* @return {@code this} builder for use in a chained invocation
*/
public final Builder putAllLabels(Map entries) {
if (this.labels == null) {
this.labels = new LinkedHashMap();
}
for (Map.Entry e : entries.entrySet()) {
String k = e.getKey();
String v = e.getValue();
this.labels.put(
Objects.requireNonNull(k, "labels key"),
Objects.requireNonNull(v, v == null ? "labels value for key: " + k : null));
}
return this;
}
/**
* Initializes the value for the {@link SecretSpec#data() data} attribute.
* @param data The value for data (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("Data")
public final Builder data(@Nullable String data) {
this.data = data;
return this;
}
/**
* Builds a new {@link ImmutableSecretSpec ImmutableSecretSpec}.
* @return An immutable instance of SecretSpec
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableSecretSpec build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableSecretSpec(name, labels == null ? null : createUnmodifiableMap(false, false, labels), data);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name");
return "Cannot build SecretSpec, some of required attributes are not set " + attributes;
}
}
private static Map createUnmodifiableMap(boolean checkNulls, boolean skipNulls, Map extends K, ? extends V> map) {
switch (map.size()) {
case 0: return Collections.emptyMap();
case 1: {
Map.Entry extends K, ? extends V> e = map.entrySet().iterator().next();
K k = e.getKey();
V v = e.getValue();
if (checkNulls) {
Objects.requireNonNull(k, "key");
Objects.requireNonNull(v, v == null ? "value for key: " + k : null);
}
if (skipNulls && (k == null || v == null)) {
return Collections.emptyMap();
}
return Collections.singletonMap(k, v);
}
default: {
Map linkedMap = new LinkedHashMap<>(map.size() * 4 / 3 + 1);
if (skipNulls || checkNulls) {
for (Map.Entry extends K, ? extends V> e : map.entrySet()) {
K k = e.getKey();
V v = e.getValue();
if (skipNulls) {
if (k == null || v == null) continue;
} else if (checkNulls) {
Objects.requireNonNull(k, "key");
Objects.requireNonNull(v, v == null ? "value for key: " + k : null);
}
linkedMap.put(k, v);
}
} else {
linkedMap.putAll(map);
}
return Collections.unmodifiableMap(linkedMap);
}
}
}
}