org.cloudfoundry.multiapps.controller.process.util.ImmutableProcessTime Maven / Gradle / Ivy
package org.cloudfoundry.multiapps.controller.process.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Immutable implementation of {@link ProcessTime}.
*
* Use the builder to create immutable instances:
* {@code ImmutableProcessTime.builder()}.
*/
@SuppressWarnings({"all"})
public final class ImmutableProcessTime
implements ProcessTime {
private final long processDuration;
private final long delayBetweenSteps;
private ImmutableProcessTime(long processDuration, long delayBetweenSteps) {
this.processDuration = processDuration;
this.delayBetweenSteps = delayBetweenSteps;
}
/**
* @return The value of the {@code processDuration} attribute
*/
@Override
public long getProcessDuration() {
return processDuration;
}
/**
* @return The value of the {@code delayBetweenSteps} attribute
*/
@Override
public long getDelayBetweenSteps() {
return delayBetweenSteps;
}
/**
* Copy the current immutable object by setting a value for the {@link ProcessTime#getProcessDuration() processDuration} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for processDuration
* @return A modified copy of the {@code this} object
*/
public final ImmutableProcessTime withProcessDuration(long value) {
if (this.processDuration == value) return this;
return new ImmutableProcessTime(value, this.delayBetweenSteps);
}
/**
* Copy the current immutable object by setting a value for the {@link ProcessTime#getDelayBetweenSteps() delayBetweenSteps} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for delayBetweenSteps
* @return A modified copy of the {@code this} object
*/
public final ImmutableProcessTime withDelayBetweenSteps(long value) {
if (this.delayBetweenSteps == value) return this;
return new ImmutableProcessTime(this.processDuration, value);
}
/**
* This instance is equal to all instances of {@code ImmutableProcessTime} 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 ImmutableProcessTime
&& equalTo(0, (ImmutableProcessTime) another);
}
private boolean equalTo(int synthetic, ImmutableProcessTime another) {
return processDuration == another.processDuration
&& delayBetweenSteps == another.delayBetweenSteps;
}
/**
* Computes a hash code from attributes: {@code processDuration}, {@code delayBetweenSteps}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + Long.hashCode(processDuration);
h += (h << 5) + Long.hashCode(delayBetweenSteps);
return h;
}
/**
* Prints the immutable value {@code ProcessTime} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "ProcessTime{"
+ "processDuration=" + processDuration
+ ", delayBetweenSteps=" + delayBetweenSteps
+ "}";
}
/**
* Creates an immutable copy of a {@link ProcessTime} 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 ProcessTime instance
*/
public static ImmutableProcessTime copyOf(ProcessTime instance) {
if (instance instanceof ImmutableProcessTime) {
return (ImmutableProcessTime) instance;
}
return ImmutableProcessTime.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableProcessTime ImmutableProcessTime}.
*
* ImmutableProcessTime.builder()
* .processDuration(long) // required {@link ProcessTime#getProcessDuration() processDuration}
* .delayBetweenSteps(long) // required {@link ProcessTime#getDelayBetweenSteps() delayBetweenSteps}
* .build();
*
* @return A new ImmutableProcessTime builder
*/
public static ImmutableProcessTime.Builder builder() {
return new ImmutableProcessTime.Builder();
}
/**
* Builds instances of type {@link ImmutableProcessTime ImmutableProcessTime}.
* 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 static final long INIT_BIT_PROCESS_DURATION = 0x1L;
private static final long INIT_BIT_DELAY_BETWEEN_STEPS = 0x2L;
private long initBits = 0x3L;
private long processDuration;
private long delayBetweenSteps;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code ProcessTime} 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(ProcessTime instance) {
Objects.requireNonNull(instance, "instance");
this.processDuration(instance.getProcessDuration());
this.delayBetweenSteps(instance.getDelayBetweenSteps());
return this;
}
/**
* Initializes the value for the {@link ProcessTime#getProcessDuration() processDuration} attribute.
* @param processDuration The value for processDuration
* @return {@code this} builder for use in a chained invocation
*/
public final Builder processDuration(long processDuration) {
this.processDuration = processDuration;
initBits &= ~INIT_BIT_PROCESS_DURATION;
return this;
}
/**
* Initializes the value for the {@link ProcessTime#getDelayBetweenSteps() delayBetweenSteps} attribute.
* @param delayBetweenSteps The value for delayBetweenSteps
* @return {@code this} builder for use in a chained invocation
*/
public final Builder delayBetweenSteps(long delayBetweenSteps) {
this.delayBetweenSteps = delayBetweenSteps;
initBits &= ~INIT_BIT_DELAY_BETWEEN_STEPS;
return this;
}
/**
* Builds a new {@link ImmutableProcessTime ImmutableProcessTime}.
* @return An immutable instance of ProcessTime
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableProcessTime build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableProcessTime(processDuration, delayBetweenSteps);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_PROCESS_DURATION) != 0) attributes.add("processDuration");
if ((initBits & INIT_BIT_DELAY_BETWEEN_STEPS) != 0) attributes.add("delayBetweenSteps");
return "Cannot build ProcessTime, some of required attributes are not set " + attributes;
}
}
}