com.mercateo.eventstore.domain.ImmutableEventInitiator Maven / Gradle / Ivy
Show all versions of client-common Show documentation
package com.mercateo.eventstore.domain;
import com.mercateo.eventstore.data.ReferenceData;
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 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 EventInitiator}.
*
* Use the builder to create immutable instances:
* {@code ImmutableEventInitiator.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "EventInitiator"})
@Immutable
public final class ImmutableEventInitiator implements EventInitiator {
private final List agent;
private final UUID id;
private final String type;
private ImmutableEventInitiator(
List agent,
UUID id,
String type) {
this.agent = agent;
this.id = id;
this.type = type;
}
/**
* @return The value of the {@code agent} attribute
*/
@Override
public List agent() {
return agent;
}
/**
* @return The value of the {@code id} attribute
*/
@Override
public UUID id() {
return id;
}
/**
* @return The value of the {@code type} attribute
*/
@Override
public String type() {
return type;
}
/**
* Copy the current immutable object with elements that replace the content of {@link EventInitiator#agent() agent}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableEventInitiator withAgent(ReferenceData... elements) {
List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new ImmutableEventInitiator(newValue, this.id, this.type);
}
/**
* Copy the current immutable object with elements that replace the content of {@link EventInitiator#agent() agent}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of agent elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableEventInitiator withAgent(Iterable extends ReferenceData> elements) {
if (this.agent == elements) return this;
List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new ImmutableEventInitiator(newValue, this.id, this.type);
}
/**
* Copy the current immutable object by setting a value for the {@link EventInitiator#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 ImmutableEventInitiator withId(UUID value) {
if (this.id == value) return this;
UUID newValue = Objects.requireNonNull(value, "id");
return new ImmutableEventInitiator(this.agent, newValue, this.type);
}
/**
* Copy the current immutable object by setting a value for the {@link EventInitiator#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 ImmutableEventInitiator withType(String value) {
if (this.type.equals(value)) return this;
String newValue = Objects.requireNonNull(value, "type");
return new ImmutableEventInitiator(this.agent, this.id, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableEventInitiator} 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 ImmutableEventInitiator
&& equalTo((ImmutableEventInitiator) another);
}
private boolean equalTo(ImmutableEventInitiator another) {
return agent.equals(another.agent)
&& id.equals(another.id)
&& type.equals(another.type);
}
/**
* Computes a hash code from attributes: {@code agent}, {@code id}, {@code type}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + agent.hashCode();
h += (h << 5) + id.hashCode();
h += (h << 5) + type.hashCode();
return h;
}
/**
* Prints the immutable value {@code EventInitiator} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "EventInitiator{"
+ "agent=" + agent
+ ", id=" + id
+ ", type=" + type
+ "}";
}
/**
* Creates an immutable copy of a {@link EventInitiator} 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 EventInitiator instance
*/
public static ImmutableEventInitiator copyOf(EventInitiator instance) {
if (instance instanceof ImmutableEventInitiator) {
return (ImmutableEventInitiator) instance;
}
return ImmutableEventInitiator.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableEventInitiator ImmutableEventInitiator}.
* @return A new ImmutableEventInitiator builder
*/
public static ImmutableEventInitiator.Builder builder() {
return new ImmutableEventInitiator.Builder();
}
/**
* Builds instances of type {@link ImmutableEventInitiator ImmutableEventInitiator}.
* 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 List agent = new ArrayList();
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.domain.EventInitiator} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(EventInitiator instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
if (object instanceof Reference) {
Reference instance = (Reference) object;
type(instance.type());
id(instance.id());
}
if (object instanceof EventInitiator) {
EventInitiator instance = (EventInitiator) object;
addAllAgent(instance.agent());
}
}
/**
* Adds one element to {@link EventInitiator#agent() agent} list.
* @param element A agent element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAgent(ReferenceData element) {
this.agent.add(Objects.requireNonNull(element, "agent element"));
return this;
}
/**
* Adds elements to {@link EventInitiator#agent() agent} list.
* @param elements An array of agent elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAgent(ReferenceData... elements) {
for (ReferenceData element : elements) {
this.agent.add(Objects.requireNonNull(element, "agent element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link EventInitiator#agent() agent} list.
* @param elements An iterable of agent elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder agent(Iterable extends ReferenceData> elements) {
this.agent.clear();
return addAllAgent(elements);
}
/**
* Adds elements to {@link EventInitiator#agent() agent} list.
* @param elements An iterable of agent elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAllAgent(Iterable extends ReferenceData> elements) {
for (ReferenceData element : elements) {
this.agent.add(Objects.requireNonNull(element, "agent element"));
}
return this;
}
/**
* Initializes the value for the {@link EventInitiator#id() id} attribute.
* @param id The value for id
* @return {@code this} builder for use in a chained invocation
*/
public final Builder id(UUID id) {
this.id = Objects.requireNonNull(id, "id");
initBits &= ~INIT_BIT_ID;
return this;
}
/**
* Initializes the value for the {@link EventInitiator#type() type} attribute.
* @param type The value for type
* @return {@code this} builder for use in a chained invocation
*/
public final Builder type(String type) {
this.type = Objects.requireNonNull(type, "type");
initBits &= ~INIT_BIT_TYPE;
return this;
}
/**
* Builds a new {@link ImmutableEventInitiator ImmutableEventInitiator}.
* @return An immutable instance of EventInitiator
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableEventInitiator build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableEventInitiator(createUnmodifiableList(true, agent), 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 EventInitiator, some of required attributes are not set " + attributes;
}
}
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();
} 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);
}
}
}
}