com.mercateo.eventstore.data.ImmutableReferenceData Maven / Gradle / Ivy
Show all versions of client-common Show documentation
package com.mercateo.eventstore.data;
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 com.mercateo.eventstore.domain.Reference;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link ReferenceData}.
*
* Use the builder to create immutable instances:
* {@code ImmutableReferenceData.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "ReferenceData"})
@Immutable
public final class ImmutableReferenceData implements ReferenceData {
private final UUID id;
private final String type;
private ImmutableReferenceData(UUID id, String type) {
this.id = id;
this.type = type;
}
/**
* @return The value of the {@code id} attribute
*/
@JsonProperty("id")
@Override
public UUID id() {
return id;
}
/**
* @return The value of the {@code type} attribute
*/
@JsonProperty("type")
@Override
public String type() {
return type;
}
/**
* Copy the current immutable object by setting a value for the {@link ReferenceData#id() id} 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 id
* @return A modified copy of the {@code this} object
*/
public final ImmutableReferenceData withId(UUID value) {
if (this.id == value) return this;
UUID newValue = Objects.requireNonNull(value, "id");
return new ImmutableReferenceData(newValue, this.type);
}
/**
* Copy the current immutable object by setting a value for the {@link ReferenceData#type() type} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for type
* @return A modified copy of the {@code this} object
*/
public final ImmutableReferenceData withType(String value) {
if (this.type.equals(value)) return this;
String newValue = Objects.requireNonNull(value, "type");
return new ImmutableReferenceData(this.id, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableReferenceData} that have equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(@Nullable Object another) {
if (this == another) return true;
return another instanceof ImmutableReferenceData
&& equalTo((ImmutableReferenceData) another);
}
private boolean equalTo(ImmutableReferenceData another) {
return id.equals(another.id)
&& type.equals(another.type);
}
/**
* Computes a hash code from attributes: {@code id}, {@code type}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + id.hashCode();
h += (h << 5) + type.hashCode();
return h;
}
/**
* Prints the immutable value {@code ReferenceData} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "ReferenceData{"
+ "id=" + id
+ ", type=" + type
+ "}";
}
/**
* 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 ReferenceData {
@Nullable UUID id;
@Nullable String type;
@JsonProperty("id")
public void setId(UUID id) {
this.id = id;
}
@JsonProperty("type")
public void setType(String type) {
this.type = type;
}
@Override
public UUID id() { throw new UnsupportedOperationException(); }
@Override
public String type() { 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 ImmutableReferenceData fromJson(Json json) {
ImmutableReferenceData.Builder builder = ImmutableReferenceData.builder();
if (json.id != null) {
builder.id(json.id);
}
if (json.type != null) {
builder.type(json.type);
}
return builder.build();
}
/**
* Creates an immutable copy of a {@link ReferenceData} 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 ReferenceData instance
*/
public static ImmutableReferenceData copyOf(ReferenceData instance) {
if (instance instanceof ImmutableReferenceData) {
return (ImmutableReferenceData) instance;
}
return ImmutableReferenceData.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableReferenceData ImmutableReferenceData}.
* @return A new ImmutableReferenceData builder
*/
public static ImmutableReferenceData.Builder builder() {
return new ImmutableReferenceData.Builder();
}
/**
* Builds instances of type {@link ImmutableReferenceData ImmutableReferenceData}.
* 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.
*/
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_ID = 0x1L;
private static final long INIT_BIT_TYPE = 0x2L;
private long initBits = 0x3L;
private @Nullable UUID id;
private @Nullable String type;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code com.mercateo.eventstore.domain.Reference} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(Reference instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code com.mercateo.eventstore.data.ReferenceData} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(ReferenceData instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
long bits = 0;
if (object instanceof Reference) {
Reference instance = (Reference) object;
if ((bits & 0x2L) == 0) {
type(instance.type());
bits |= 0x2L;
}
if ((bits & 0x1L) == 0) {
id(instance.id());
bits |= 0x1L;
}
}
if (object instanceof ReferenceData) {
ReferenceData instance = (ReferenceData) object;
if ((bits & 0x2L) == 0) {
type(instance.type());
bits |= 0x2L;
}
if ((bits & 0x1L) == 0) {
id(instance.id());
bits |= 0x1L;
}
}
}
/**
* Initializes the value for the {@link ReferenceData#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(UUID id) {
this.id = Objects.requireNonNull(id, "id");
initBits &= ~INIT_BIT_ID;
return this;
}
/**
* Initializes the value for the {@link ReferenceData#type() type} attribute.
* @param type The value for type
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("type")
public final Builder type(String type) {
this.type = Objects.requireNonNull(type, "type");
initBits &= ~INIT_BIT_TYPE;
return this;
}
/**
* Builds a new {@link ImmutableReferenceData ImmutableReferenceData}.
* @return An immutable instance of ReferenceData
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableReferenceData build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableReferenceData(id, type);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList();
if ((initBits & INIT_BIT_ID) != 0) attributes.add("id");
if ((initBits & INIT_BIT_TYPE) != 0) attributes.add("type");
return "Cannot build ReferenceData, some of required attributes are not set " + attributes;
}
}
}