de.hilling.junit.cdi.lifecycle.ImmutableTestEvent Maven / Gradle / Ivy
package de.hilling.junit.cdi.lifecycle;
import de.hilling.junit.cdi.scope.TestState;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link TestEvent}.
*
* Use the builder to create immutable instances:
* {@code ImmutableTestEvent.builder()}.
* Use the static factory method to create immutable instances:
* {@code ImmutableTestEvent.of()}.
*/
@Generated(from = "TestEvent", generator = "Immutables")
@SuppressWarnings({"all"})
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
public final class ImmutableTestEvent implements TestEvent {
private final TestState value;
private ImmutableTestEvent(TestState value) {
this.value = Objects.requireNonNull(value, "value");
}
private ImmutableTestEvent(ImmutableTestEvent original, TestState value) {
this.value = value;
}
/** {@inheritDoc} */
@Override
public Class extends Annotation> annotationType() {
return TestEvent.class;
}
/**
* @return current {@link TestState}
*/
@Override
public TestState value() {
return value;
}
/**
* Copy the current immutable object by setting a value for the {@link TestEvent#value() value} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for value
* @return A modified copy of the {@code this} object
*/
public final ImmutableTestEvent withValue(TestState value) {
TestState newValue = Objects.requireNonNull(value, "value");
if (this.value == newValue) return this;
return new ImmutableTestEvent(this, newValue);
}
/**
* This instance is equal to any implementation of the {@link ImmutableTestEvent} type with 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 TestEvent
&& equalTo(0, (TestEvent) another);
}
private boolean equalTo(int synthetic, TestEvent another) {
return value.equals(another.value());
}
/**
* Computes a hash code from attributes: {@code value}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 0;
h += 127 * "value".hashCode() ^ value.hashCode();
return h;
}
/**
* Prints the immutable value {@code TestEvent} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "@TestEvent("
+ "value=" + value
+ ")";
}
/**
* Construct a new immutable {@code TestEvent} instance.
* @param value The value for the {@code value} attribute
* @return An immutable TestEvent instance
*/
public static ImmutableTestEvent of(TestState value) {
return new ImmutableTestEvent(value);
}
/**
* Creates an immutable copy of a {@link TestEvent} 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 TestEvent instance
*/
public static ImmutableTestEvent copyOf(TestEvent instance) {
if (instance instanceof ImmutableTestEvent) {
return (ImmutableTestEvent) instance;
}
return ImmutableTestEvent.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableTestEvent ImmutableTestEvent}.
*
* ImmutableTestEvent.builder()
* .value(de.hilling.junit.cdi.scope.TestState) // required {@link TestEvent#value() value}
* .build();
*
* @return A new ImmutableTestEvent builder
*/
public static ImmutableTestEvent.Builder builder() {
return new ImmutableTestEvent.Builder();
}
/**
* Builds instances of type {@link ImmutableTestEvent ImmutableTestEvent}.
* 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.
*/
@Generated(from = "TestEvent", generator = "Immutables")
public static final class Builder {
private static final long INIT_BIT_VALUE = 0x1L;
private long initBits = 0x1L;
private TestState value;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code TestEvent} 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(TestEvent instance) {
Objects.requireNonNull(instance, "instance");
value(instance.value());
return this;
}
/**
* Initializes the value for the {@link TestEvent#value() value} attribute.
* @param value The value for value
* @return {@code this} builder for use in a chained invocation
*/
public final Builder value(TestState value) {
this.value = Objects.requireNonNull(value, "value");
initBits &= ~INIT_BIT_VALUE;
return this;
}
/**
* Builds a new {@link ImmutableTestEvent ImmutableTestEvent}.
* @return An immutable instance of TestEvent
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableTestEvent build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableTestEvent(null, value);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_VALUE) != 0) attributes.add("value");
return "Cannot build TestEvent, some of required attributes are not set " + attributes;
}
}
}