com.mercateo.eventstore.domain.ImmutableCausality Maven / Gradle / Ivy
Show all versions of client-common Show documentation
package com.mercateo.eventstore.domain;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
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 Causality}.
*
* Use the builder to create immutable instances:
* {@code ImmutableCausality.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "Causality"})
@Immutable
public final class ImmutableCausality implements Causality {
private final EventId eventId;
private final EventType eventType;
private ImmutableCausality(EventId eventId, EventType eventType) {
this.eventId = eventId;
this.eventType = eventType;
}
/**
* @return The value of the {@code eventId} attribute
*/
@Override
public EventId eventId() {
return eventId;
}
/**
* @return The value of the {@code eventType} attribute
*/
@Override
public EventType eventType() {
return eventType;
}
/**
* Copy the current immutable object by setting a value for the {@link Causality#eventId() eventId} 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 eventId
* @return A modified copy of the {@code this} object
*/
public final ImmutableCausality withEventId(EventId value) {
if (this.eventId == value) return this;
EventId newValue = Objects.requireNonNull(value, "eventId");
return new ImmutableCausality(newValue, this.eventType);
}
/**
* Copy the current immutable object by setting a value for the {@link Causality#eventType() eventType} 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 eventType
* @return A modified copy of the {@code this} object
*/
public final ImmutableCausality withEventType(EventType value) {
if (this.eventType == value) return this;
EventType newValue = Objects.requireNonNull(value, "eventType");
return new ImmutableCausality(this.eventId, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableCausality} 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 ImmutableCausality
&& equalTo((ImmutableCausality) another);
}
private boolean equalTo(ImmutableCausality another) {
return eventId.equals(another.eventId)
&& eventType.equals(another.eventType);
}
/**
* Computes a hash code from attributes: {@code eventId}, {@code eventType}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + eventId.hashCode();
h += (h << 5) + eventType.hashCode();
return h;
}
/**
* Prints the immutable value {@code Causality} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "Causality{"
+ "eventId=" + eventId
+ ", eventType=" + eventType
+ "}";
}
/**
* Creates an immutable copy of a {@link Causality} 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 Causality instance
*/
public static ImmutableCausality copyOf(Causality instance) {
if (instance instanceof ImmutableCausality) {
return (ImmutableCausality) instance;
}
return ImmutableCausality.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableCausality ImmutableCausality}.
* @return A new ImmutableCausality builder
*/
public static ImmutableCausality.Builder builder() {
return new ImmutableCausality.Builder();
}
/**
* Builds instances of type {@link ImmutableCausality ImmutableCausality}.
* 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_EVENT_ID = 0x1L;
private static final long INIT_BIT_EVENT_TYPE = 0x2L;
private long initBits = 0x3L;
private @Nullable EventId eventId;
private @Nullable EventType eventType;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Causality} 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(Causality instance) {
Objects.requireNonNull(instance, "instance");
eventId(instance.eventId());
eventType(instance.eventType());
return this;
}
/**
* Initializes the value for the {@link Causality#eventId() eventId} attribute.
* @param eventId The value for eventId
* @return {@code this} builder for use in a chained invocation
*/
public final Builder eventId(EventId eventId) {
this.eventId = Objects.requireNonNull(eventId, "eventId");
initBits &= ~INIT_BIT_EVENT_ID;
return this;
}
/**
* Initializes the value for the {@link Causality#eventType() eventType} attribute.
* @param eventType The value for eventType
* @return {@code this} builder for use in a chained invocation
*/
public final Builder eventType(EventType eventType) {
this.eventType = Objects.requireNonNull(eventType, "eventType");
initBits &= ~INIT_BIT_EVENT_TYPE;
return this;
}
/**
* Builds a new {@link ImmutableCausality ImmutableCausality}.
* @return An immutable instance of Causality
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableCausality build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableCausality(eventId, eventType);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList();
if ((initBits & INIT_BIT_EVENT_ID) != 0) attributes.add("eventId");
if ((initBits & INIT_BIT_EVENT_TYPE) != 0) attributes.add("eventType");
return "Cannot build Causality, some of required attributes are not set " + attributes;
}
}
}