org.mandas.docker.client.messages.ImmutableServiceCreateResponse Maven / Gradle / Ivy
package org.mandas.docker.client.messages;
import com.fasterxml.jackson.annotation.JsonProperty;
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.mandas.docker.Nullable;
/**
* Immutable implementation of {@link ServiceCreateResponse}.
*
* Use the builder to create immutable instances:
* {@code ImmutableServiceCreateResponse.builder()}.
*/
@SuppressWarnings({"all"})
final class ImmutableServiceCreateResponse
implements ServiceCreateResponse {
private final String id;
private final @Nullable List warnings;
private ImmutableServiceCreateResponse(String id, @Nullable List warnings) {
this.id = id;
this.warnings = warnings;
}
/**
* @return The value of the {@code id} attribute
*/
@JsonProperty("ID")
@Override
public String id() {
return id;
}
/**
* @return The value of the {@code warnings} attribute
*/
@JsonProperty("Warnings")
@Override
public @Nullable List warnings() {
return warnings;
}
/**
* Copy the current immutable object by setting a value for the {@link ServiceCreateResponse#id() 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 ImmutableServiceCreateResponse withId(String value) {
String newValue = Objects.requireNonNull(value, "id");
if (this.id.equals(newValue)) return this;
return new ImmutableServiceCreateResponse(newValue, this.warnings);
}
/**
* Copy the current immutable object with elements that replace the content of {@link ServiceCreateResponse#warnings() warnings}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableServiceCreateResponse withWarnings(@Nullable String... elements) {
if (elements == null) {
return new ImmutableServiceCreateResponse(this.id, null);
}
@Nullable List newValue = Arrays.asList(elements) == null ? null : createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new ImmutableServiceCreateResponse(this.id, newValue);
}
/**
* Copy the current immutable object with elements that replace the content of {@link ServiceCreateResponse#warnings() warnings}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of warnings elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableServiceCreateResponse withWarnings(@Nullable Iterable elements) {
if (this.warnings == elements) return this;
@Nullable List newValue = elements == null ? null : createUnmodifiableList(false, createSafeList(elements, true, false));
return new ImmutableServiceCreateResponse(this.id, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableServiceCreateResponse} 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 ImmutableServiceCreateResponse
&& equalTo(0, (ImmutableServiceCreateResponse) another);
}
private boolean equalTo(int synthetic, ImmutableServiceCreateResponse another) {
return id.equals(another.id)
&& Objects.equals(warnings, another.warnings);
}
/**
* Computes a hash code from attributes: {@code id}, {@code warnings}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + id.hashCode();
h += (h << 5) + Objects.hashCode(warnings);
return h;
}
/**
* Prints the immutable value {@code ServiceCreateResponse} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "ServiceCreateResponse{"
+ "id=" + id
+ ", warnings=" + warnings
+ "}";
}
/**
* Creates an immutable copy of a {@link ServiceCreateResponse} 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 ServiceCreateResponse instance
*/
public static ImmutableServiceCreateResponse copyOf(ServiceCreateResponse instance) {
if (instance instanceof ImmutableServiceCreateResponse) {
return (ImmutableServiceCreateResponse) instance;
}
return ImmutableServiceCreateResponse.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableServiceCreateResponse ImmutableServiceCreateResponse}.
*
* ImmutableServiceCreateResponse.builder()
* .id(String) // required {@link ServiceCreateResponse#id() id}
* .warnings(List<String> | null) // nullable {@link ServiceCreateResponse#warnings() warnings}
* .build();
*
* @return A new ImmutableServiceCreateResponse builder
*/
public static ImmutableServiceCreateResponse.Builder builder() {
return new ImmutableServiceCreateResponse.Builder();
}
/**
* Builds instances of type {@link ImmutableServiceCreateResponse ImmutableServiceCreateResponse}.
* 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 {
private static final long INIT_BIT_ID = 0x1L;
private long initBits = 0x1L;
private String id;
private List warnings = null;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code ServiceCreateResponse} 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(ServiceCreateResponse instance) {
Objects.requireNonNull(instance, "instance");
id(instance.id());
@Nullable List warningsValue = instance.warnings();
if (warningsValue != null) {
addAllWarnings(warningsValue);
}
return this;
}
/**
* Initializes the value for the {@link ServiceCreateResponse#id() 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;
}
/**
* Adds one element to {@link ServiceCreateResponse#warnings() warnings} list.
* @param element A warnings element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder warning(String element) {
if (this.warnings == null) {
this.warnings = new ArrayList();
}
this.warnings.add(Objects.requireNonNull(element, "warnings element"));
return this;
}
/**
* Adds elements to {@link ServiceCreateResponse#warnings() warnings} list.
* @param elements An array of warnings elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder warnings(String... elements) {
if (this.warnings == null) {
this.warnings = new ArrayList();
}
for (String element : elements) {
this.warnings.add(Objects.requireNonNull(element, "warnings element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link ServiceCreateResponse#warnings() warnings} list.
* @param elements An iterable of warnings elements
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("Warnings")
public final Builder warnings(@Nullable Iterable elements) {
if (elements == null) {
this.warnings = null;
return this;
}
this.warnings = new ArrayList();
return addAllWarnings(elements);
}
/**
* Adds elements to {@link ServiceCreateResponse#warnings() warnings} list.
* @param elements An iterable of warnings elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAllWarnings(Iterable elements) {
Objects.requireNonNull(elements, "warnings element");
if (this.warnings == null) {
this.warnings = new ArrayList();
}
for (String element : elements) {
this.warnings.add(Objects.requireNonNull(element, "warnings element"));
}
return this;
}
/**
* Builds a new {@link ImmutableServiceCreateResponse ImmutableServiceCreateResponse}.
* @return An immutable instance of ServiceCreateResponse
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableServiceCreateResponse build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableServiceCreateResponse(id, warnings == null ? null : createUnmodifiableList(true, warnings));
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_ID) != 0) attributes.add("id");
return "Cannot build ServiceCreateResponse, some of required attributes are not set " + attributes;
}
}
private static List createSafeList(Iterable extends T> 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);
}
}
}
}