org.mandas.docker.client.messages.ImmutableContainerUpdate 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 ContainerUpdate}.
*
* Use the builder to create immutable instances:
* {@code ImmutableContainerUpdate.builder()}.
*/
@SuppressWarnings({"all"})
final class ImmutableContainerUpdate implements ContainerUpdate {
private final @Nullable List warnings;
private ImmutableContainerUpdate(@Nullable List warnings) {
this.warnings = warnings;
}
/**
* @return The value of the {@code warnings} attribute
*/
@JsonProperty("Warnings")
@Override
public @Nullable List warnings() {
return warnings;
}
/**
* Copy the current immutable object with elements that replace the content of {@link ContainerUpdate#warnings() warnings}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableContainerUpdate withWarnings(@Nullable String... elements) {
if (elements == null) {
return new ImmutableContainerUpdate(null);
}
@Nullable List newValue = Arrays.asList(elements) == null ? null : createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new ImmutableContainerUpdate(newValue);
}
/**
* Copy the current immutable object with elements that replace the content of {@link ContainerUpdate#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 ImmutableContainerUpdate withWarnings(@Nullable Iterable elements) {
if (this.warnings == elements) return this;
@Nullable List newValue = elements == null ? null : createUnmodifiableList(false, createSafeList(elements, true, false));
return new ImmutableContainerUpdate(newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableContainerUpdate} 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 ImmutableContainerUpdate
&& equalTo(0, (ImmutableContainerUpdate) another);
}
private boolean equalTo(int synthetic, ImmutableContainerUpdate another) {
return Objects.equals(warnings, another.warnings);
}
/**
* Computes a hash code from attributes: {@code warnings}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + Objects.hashCode(warnings);
return h;
}
/**
* Prints the immutable value {@code ContainerUpdate} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "ContainerUpdate{"
+ "warnings=" + warnings
+ "}";
}
/**
* Creates an immutable copy of a {@link ContainerUpdate} 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 ContainerUpdate instance
*/
public static ImmutableContainerUpdate copyOf(ContainerUpdate instance) {
if (instance instanceof ImmutableContainerUpdate) {
return (ImmutableContainerUpdate) instance;
}
return ImmutableContainerUpdate.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableContainerUpdate ImmutableContainerUpdate}.
*
* ImmutableContainerUpdate.builder()
* .warnings(List<String> | null) // nullable {@link ContainerUpdate#warnings() warnings}
* .build();
*
* @return A new ImmutableContainerUpdate builder
*/
public static ImmutableContainerUpdate.Builder builder() {
return new ImmutableContainerUpdate.Builder();
}
/**
* Builds instances of type {@link ImmutableContainerUpdate ImmutableContainerUpdate}.
* 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 ContainerUpdate.Builder {
private List warnings = null;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code ContainerUpdate} 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(ContainerUpdate instance) {
Objects.requireNonNull(instance, "instance");
@Nullable List warningsValue = instance.warnings();
if (warningsValue != null) {
addAllWarnings(warningsValue);
}
return this;
}
/**
* Adds one element to {@link ContainerUpdate#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 ContainerUpdate#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 ContainerUpdate#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 ContainerUpdate#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 ImmutableContainerUpdate ImmutableContainerUpdate}.
* @return An immutable instance of ContainerUpdate
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableContainerUpdate build() {
return new ImmutableContainerUpdate(warnings == null ? null : createUnmodifiableList(true, warnings));
}
}
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);
}
}
}
}