org.mandas.docker.client.messages.swarm.ImmutableSwarmCluster Maven / Gradle / Ivy
package org.mandas.docker.client.messages.swarm;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
/**
* Immutable implementation of {@link SwarmCluster}.
*
* Use the builder to create immutable instances:
* {@code ImmutableSwarmCluster.builder()}.
*/
@SuppressWarnings({"all"})
final class ImmutableSwarmCluster implements SwarmCluster {
private final String id;
private final Version version;
private final Date createdAt;
private final Date updatedAt;
private final SwarmSpec swarmSpec;
private ImmutableSwarmCluster(
String id,
Version version,
Date createdAt,
Date updatedAt,
SwarmSpec swarmSpec) {
this.id = id;
this.version = version;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.swarmSpec = swarmSpec;
}
/**
* @return The value of the {@code id} attribute
*/
@JsonProperty("ID")
@Override
public String id() {
return id;
}
/**
* @return The value of the {@code version} attribute
*/
@JsonProperty("Version")
@Override
public Version version() {
return version;
}
/**
* @return The value of the {@code createdAt} attribute
*/
@JsonProperty("CreatedAt")
@Override
public Date createdAt() {
return createdAt;
}
/**
* @return The value of the {@code updatedAt} attribute
*/
@JsonProperty("UpdatedAt")
@Override
public Date updatedAt() {
return updatedAt;
}
/**
* @return The value of the {@code swarmSpec} attribute
*/
@JsonProperty("Spec")
@Override
public SwarmSpec swarmSpec() {
return swarmSpec;
}
/**
* Copy the current immutable object by setting a value for the {@link SwarmCluster#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 ImmutableSwarmCluster withId(String value) {
String newValue = Objects.requireNonNull(value, "id");
if (this.id.equals(newValue)) return this;
return new ImmutableSwarmCluster(newValue, this.version, this.createdAt, this.updatedAt, this.swarmSpec);
}
/**
* Copy the current immutable object by setting a value for the {@link SwarmCluster#version() version} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for version
* @return A modified copy of the {@code this} object
*/
public final ImmutableSwarmCluster withVersion(Version value) {
if (this.version == value) return this;
Version newValue = Objects.requireNonNull(value, "version");
return new ImmutableSwarmCluster(this.id, newValue, this.createdAt, this.updatedAt, this.swarmSpec);
}
/**
* Copy the current immutable object by setting a value for the {@link SwarmCluster#createdAt() createdAt} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for createdAt
* @return A modified copy of the {@code this} object
*/
public final ImmutableSwarmCluster withCreatedAt(Date value) {
if (this.createdAt == value) return this;
Date newValue = Objects.requireNonNull(value, "createdAt");
return new ImmutableSwarmCluster(this.id, this.version, newValue, this.updatedAt, this.swarmSpec);
}
/**
* Copy the current immutable object by setting a value for the {@link SwarmCluster#updatedAt() updatedAt} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for updatedAt
* @return A modified copy of the {@code this} object
*/
public final ImmutableSwarmCluster withUpdatedAt(Date value) {
if (this.updatedAt == value) return this;
Date newValue = Objects.requireNonNull(value, "updatedAt");
return new ImmutableSwarmCluster(this.id, this.version, this.createdAt, newValue, this.swarmSpec);
}
/**
* Copy the current immutable object by setting a value for the {@link SwarmCluster#swarmSpec() swarmSpec} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for swarmSpec
* @return A modified copy of the {@code this} object
*/
public final ImmutableSwarmCluster withSwarmSpec(SwarmSpec value) {
if (this.swarmSpec == value) return this;
SwarmSpec newValue = Objects.requireNonNull(value, "swarmSpec");
return new ImmutableSwarmCluster(this.id, this.version, this.createdAt, this.updatedAt, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableSwarmCluster} 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 ImmutableSwarmCluster
&& equalTo(0, (ImmutableSwarmCluster) another);
}
private boolean equalTo(int synthetic, ImmutableSwarmCluster another) {
return id.equals(another.id)
&& version.equals(another.version)
&& createdAt.equals(another.createdAt)
&& updatedAt.equals(another.updatedAt)
&& swarmSpec.equals(another.swarmSpec);
}
/**
* Computes a hash code from attributes: {@code id}, {@code version}, {@code createdAt}, {@code updatedAt}, {@code swarmSpec}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + id.hashCode();
h += (h << 5) + version.hashCode();
h += (h << 5) + createdAt.hashCode();
h += (h << 5) + updatedAt.hashCode();
h += (h << 5) + swarmSpec.hashCode();
return h;
}
/**
* Prints the immutable value {@code SwarmCluster} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "SwarmCluster{"
+ "id=" + id
+ ", version=" + version
+ ", createdAt=" + createdAt
+ ", updatedAt=" + updatedAt
+ ", swarmSpec=" + swarmSpec
+ "}";
}
/**
* Creates an immutable copy of a {@link SwarmCluster} 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 SwarmCluster instance
*/
public static ImmutableSwarmCluster copyOf(SwarmCluster instance) {
if (instance instanceof ImmutableSwarmCluster) {
return (ImmutableSwarmCluster) instance;
}
return ImmutableSwarmCluster.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableSwarmCluster ImmutableSwarmCluster}.
*
* ImmutableSwarmCluster.builder()
* .id(String) // required {@link SwarmCluster#id() id}
* .version(org.mandas.docker.client.messages.swarm.Version) // required {@link SwarmCluster#version() version}
* .createdAt(Date) // required {@link SwarmCluster#createdAt() createdAt}
* .updatedAt(Date) // required {@link SwarmCluster#updatedAt() updatedAt}
* .swarmSpec(org.mandas.docker.client.messages.swarm.SwarmSpec) // required {@link SwarmCluster#swarmSpec() swarmSpec}
* .build();
*
* @return A new ImmutableSwarmCluster builder
*/
public static ImmutableSwarmCluster.Builder builder() {
return new ImmutableSwarmCluster.Builder();
}
/**
* Builds instances of type {@link ImmutableSwarmCluster ImmutableSwarmCluster}.
* 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 static final long INIT_BIT_VERSION = 0x2L;
private static final long INIT_BIT_CREATED_AT = 0x4L;
private static final long INIT_BIT_UPDATED_AT = 0x8L;
private static final long INIT_BIT_SWARM_SPEC = 0x10L;
private long initBits = 0x1fL;
private String id;
private Version version;
private Date createdAt;
private Date updatedAt;
private SwarmSpec swarmSpec;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code SwarmCluster} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(SwarmCluster instance) {
Objects.requireNonNull(instance, "instance");
id(instance.id());
version(instance.version());
createdAt(instance.createdAt());
updatedAt(instance.updatedAt());
swarmSpec(instance.swarmSpec());
return this;
}
/**
* Initializes the value for the {@link SwarmCluster#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;
}
/**
* Initializes the value for the {@link SwarmCluster#version() version} attribute.
* @param version The value for version
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("Version")
public final Builder version(Version version) {
this.version = Objects.requireNonNull(version, "version");
initBits &= ~INIT_BIT_VERSION;
return this;
}
/**
* Initializes the value for the {@link SwarmCluster#createdAt() createdAt} attribute.
* @param createdAt The value for createdAt
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("CreatedAt")
public final Builder createdAt(Date createdAt) {
this.createdAt = Objects.requireNonNull(createdAt, "createdAt");
initBits &= ~INIT_BIT_CREATED_AT;
return this;
}
/**
* Initializes the value for the {@link SwarmCluster#updatedAt() updatedAt} attribute.
* @param updatedAt The value for updatedAt
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("UpdatedAt")
public final Builder updatedAt(Date updatedAt) {
this.updatedAt = Objects.requireNonNull(updatedAt, "updatedAt");
initBits &= ~INIT_BIT_UPDATED_AT;
return this;
}
/**
* Initializes the value for the {@link SwarmCluster#swarmSpec() swarmSpec} attribute.
* @param swarmSpec The value for swarmSpec
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("Spec")
public final Builder swarmSpec(SwarmSpec swarmSpec) {
this.swarmSpec = Objects.requireNonNull(swarmSpec, "swarmSpec");
initBits &= ~INIT_BIT_SWARM_SPEC;
return this;
}
/**
* Builds a new {@link ImmutableSwarmCluster ImmutableSwarmCluster}.
* @return An immutable instance of SwarmCluster
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableSwarmCluster build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableSwarmCluster(id, version, createdAt, updatedAt, swarmSpec);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_ID) != 0) attributes.add("id");
if ((initBits & INIT_BIT_VERSION) != 0) attributes.add("version");
if ((initBits & INIT_BIT_CREATED_AT) != 0) attributes.add("createdAt");
if ((initBits & INIT_BIT_UPDATED_AT) != 0) attributes.add("updatedAt");
if ((initBits & INIT_BIT_SWARM_SPEC) != 0) attributes.add("swarmSpec");
return "Cannot build SwarmCluster, some of required attributes are not set " + attributes;
}
}
}