org.cloudfoundry.multiapps.controller.api.model.ImmutableMetadata Maven / Gradle / Ivy
package org.cloudfoundry.multiapps.controller.api.model;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.Objects;
import org.cloudfoundry.multiapps.common.Nullable;
/**
* Immutable implementation of {@link Metadata}.
*
* Use the builder to create immutable instances:
* {@code ImmutableMetadata.builder()}.
*/
@SuppressWarnings({"all"})
public final class ImmutableMetadata implements Metadata {
private final @Nullable String id;
private final @Nullable String version;
private final @Nullable String namespace;
private ImmutableMetadata(
@Nullable String id,
@Nullable String version,
@Nullable String namespace) {
this.id = id;
this.version = version;
this.namespace = namespace;
}
/**
* @return The value of the {@code id} attribute
*/
@JsonProperty("id")
@Override
public @Nullable String getId() {
return id;
}
/**
* @return The value of the {@code version} attribute
*/
@JsonProperty("version")
@Override
public @Nullable String getVersion() {
return version;
}
/**
* @return The value of the {@code namespace} attribute
*/
@JsonProperty("namespace")
@Override
public @Nullable String getNamespace() {
return namespace;
}
/**
* Copy the current immutable object by setting a value for the {@link Metadata#getId() id} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for id (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableMetadata withId(@Nullable String value) {
if (Objects.equals(this.id, value)) return this;
return new ImmutableMetadata(value, this.version, this.namespace);
}
/**
* Copy the current immutable object by setting a value for the {@link Metadata#getVersion() version} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for version (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableMetadata withVersion(@Nullable String value) {
if (Objects.equals(this.version, value)) return this;
return new ImmutableMetadata(this.id, value, this.namespace);
}
/**
* Copy the current immutable object by setting a value for the {@link Metadata#getNamespace() namespace} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for namespace (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableMetadata withNamespace(@Nullable String value) {
if (Objects.equals(this.namespace, value)) return this;
return new ImmutableMetadata(this.id, this.version, value);
}
/**
* This instance is equal to all instances of {@code ImmutableMetadata} 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 ImmutableMetadata
&& equalTo(0, (ImmutableMetadata) another);
}
private boolean equalTo(int synthetic, ImmutableMetadata another) {
return Objects.equals(id, another.id)
&& Objects.equals(version, another.version)
&& Objects.equals(namespace, another.namespace);
}
/**
* Computes a hash code from attributes: {@code id}, {@code version}, {@code namespace}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + Objects.hashCode(id);
h += (h << 5) + Objects.hashCode(version);
h += (h << 5) + Objects.hashCode(namespace);
return h;
}
/**
* Prints the immutable value {@code Metadata} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "Metadata{"
+ "id=" + id
+ ", version=" + version
+ ", namespace=" + namespace
+ "}";
}
/**
* 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
@JsonDeserialize
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static final class Json implements Metadata {
String id;
String version;
String namespace;
@JsonProperty("id")
public void setId(@Nullable String id) {
this.id = id;
}
@JsonProperty("version")
public void setVersion(@Nullable String version) {
this.version = version;
}
@JsonProperty("namespace")
public void setNamespace(@Nullable String namespace) {
this.namespace = namespace;
}
@Override
public String getId() { throw new UnsupportedOperationException(); }
@Override
public String getVersion() { throw new UnsupportedOperationException(); }
@Override
public String getNamespace() { 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 ImmutableMetadata fromJson(Json json) {
ImmutableMetadata.Builder builder = ImmutableMetadata.builder();
if (json.id != null) {
builder.id(json.id);
}
if (json.version != null) {
builder.version(json.version);
}
if (json.namespace != null) {
builder.namespace(json.namespace);
}
return builder.build();
}
/**
* Creates an immutable copy of a {@link Metadata} 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 Metadata instance
*/
public static ImmutableMetadata copyOf(Metadata instance) {
if (instance instanceof ImmutableMetadata) {
return (ImmutableMetadata) instance;
}
return ImmutableMetadata.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableMetadata ImmutableMetadata}.
*
* ImmutableMetadata.builder()
* .id(String | null) // nullable {@link Metadata#getId() id}
* .version(String | null) // nullable {@link Metadata#getVersion() version}
* .namespace(String | null) // nullable {@link Metadata#getNamespace() namespace}
* .build();
*
* @return A new ImmutableMetadata builder
*/
public static ImmutableMetadata.Builder builder() {
return new ImmutableMetadata.Builder();
}
/**
* Builds instances of type {@link ImmutableMetadata ImmutableMetadata}.
* 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.
*/
public static final class Builder {
private String id;
private String version;
private String namespace;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Metadata} 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(Metadata instance) {
Objects.requireNonNull(instance, "instance");
@Nullable String idValue = instance.getId();
if (idValue != null) {
id(idValue);
}
@Nullable String versionValue = instance.getVersion();
if (versionValue != null) {
version(versionValue);
}
@Nullable String namespaceValue = instance.getNamespace();
if (namespaceValue != null) {
namespace(namespaceValue);
}
return this;
}
/**
* Initializes the value for the {@link Metadata#getId() id} attribute.
* @param id The value for id (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("id")
public final Builder id(@Nullable String id) {
this.id = id;
return this;
}
/**
* Initializes the value for the {@link Metadata#getVersion() version} attribute.
* @param version The value for version (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("version")
public final Builder version(@Nullable String version) {
this.version = version;
return this;
}
/**
* Initializes the value for the {@link Metadata#getNamespace() namespace} attribute.
* @param namespace The value for namespace (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("namespace")
public final Builder namespace(@Nullable String namespace) {
this.namespace = namespace;
return this;
}
/**
* Builds a new {@link ImmutableMetadata ImmutableMetadata}.
* @return An immutable instance of Metadata
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableMetadata build() {
return new ImmutableMetadata(id, version, namespace);
}
}
}