io.resys.hdes.client.api.programs.ImmutableFlowProgram Maven / Gradle / Ivy
package io.resys.hdes.client.api.programs;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import io.resys.hdes.client.api.ast.TypeDef;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link FlowProgram}.
*
* Use the builder to create immutable instances:
* {@code ImmutableFlowProgram.builder()}.
*/
@Generated(from = "FlowProgram", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableFlowProgram implements FlowProgram {
private final Collection acceptDefs;
private final String startStepId;
private final ImmutableMap steps;
private ImmutableFlowProgram(
Collection acceptDefs,
String startStepId,
ImmutableMap steps) {
this.acceptDefs = acceptDefs;
this.startStepId = startStepId;
this.steps = steps;
}
/**
* @return The value of the {@code acceptDefs} attribute
*/
@Override
public Collection getAcceptDefs() {
return acceptDefs;
}
/**
* @return The value of the {@code startStepId} attribute
*/
@Override
public String getStartStepId() {
return startStepId;
}
/**
* @return The value of the {@code steps} attribute
*/
@Override
public ImmutableMap getSteps() {
return steps;
}
/**
* Copy the current immutable object by setting a value for the {@link FlowProgram#getAcceptDefs() acceptDefs} 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 acceptDefs
* @return A modified copy of the {@code this} object
*/
public final ImmutableFlowProgram withAcceptDefs(Collection value) {
if (this.acceptDefs == value) return this;
Collection newValue = Objects.requireNonNull(value, "acceptDefs");
return new ImmutableFlowProgram(newValue, this.startStepId, this.steps);
}
/**
* Copy the current immutable object by setting a value for the {@link FlowProgram#getStartStepId() startStepId} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for startStepId
* @return A modified copy of the {@code this} object
*/
public final ImmutableFlowProgram withStartStepId(String value) {
String newValue = Objects.requireNonNull(value, "startStepId");
if (this.startStepId.equals(newValue)) return this;
return new ImmutableFlowProgram(this.acceptDefs, newValue, this.steps);
}
/**
* Copy the current immutable object by replacing the {@link FlowProgram#getSteps() steps} map with the specified map.
* Nulls are not permitted as keys or values.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param entries The entries to be added to the steps map
* @return A modified copy of {@code this} object
*/
public final ImmutableFlowProgram withSteps(Map entries) {
if (this.steps == entries) return this;
ImmutableMap newValue = ImmutableMap.copyOf(entries);
return new ImmutableFlowProgram(this.acceptDefs, this.startStepId, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableFlowProgram} 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 ImmutableFlowProgram
&& equalTo((ImmutableFlowProgram) another);
}
private boolean equalTo(ImmutableFlowProgram another) {
return acceptDefs.equals(another.acceptDefs)
&& startStepId.equals(another.startStepId)
&& steps.equals(another.steps);
}
/**
* Computes a hash code from attributes: {@code acceptDefs}, {@code startStepId}, {@code steps}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + acceptDefs.hashCode();
h += (h << 5) + startStepId.hashCode();
h += (h << 5) + steps.hashCode();
return h;
}
/**
* Prints the immutable value {@code FlowProgram} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("FlowProgram")
.omitNullValues()
.add("acceptDefs", acceptDefs)
.add("startStepId", startStepId)
.add("steps", steps)
.toString();
}
/**
* Creates an immutable copy of a {@link FlowProgram} 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 FlowProgram instance
*/
public static ImmutableFlowProgram copyOf(FlowProgram instance) {
if (instance instanceof ImmutableFlowProgram) {
return (ImmutableFlowProgram) instance;
}
return ImmutableFlowProgram.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableFlowProgram ImmutableFlowProgram}.
*
* ImmutableFlowProgram.builder()
* .acceptDefs(Collection<io.resys.hdes.client.api.ast.TypeDef>) // required {@link FlowProgram#getAcceptDefs() acceptDefs}
* .startStepId(String) // required {@link FlowProgram#getStartStepId() startStepId}
* .putSteps|putAllSteps(String => io.resys.hdes.client.api.programs.FlowProgram.FlowProgramStep) // {@link FlowProgram#getSteps() steps} mappings
* .build();
*
* @return A new ImmutableFlowProgram builder
*/
public static ImmutableFlowProgram.Builder builder() {
return new ImmutableFlowProgram.Builder();
}
/**
* Builds instances of type {@link ImmutableFlowProgram ImmutableFlowProgram}.
* 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 = "FlowProgram", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_ACCEPT_DEFS = 0x1L;
private static final long INIT_BIT_START_STEP_ID = 0x2L;
private long initBits = 0x3L;
private @Nullable Collection acceptDefs;
private @Nullable String startStepId;
private ImmutableMap.Builder steps = ImmutableMap.builder();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code FlowProgram} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* Collection elements and entries will be added, not replaced.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(FlowProgram instance) {
Objects.requireNonNull(instance, "instance");
acceptDefs(instance.getAcceptDefs());
startStepId(instance.getStartStepId());
putAllSteps(instance.getSteps());
return this;
}
/**
* Initializes the value for the {@link FlowProgram#getAcceptDefs() acceptDefs} attribute.
* @param acceptDefs The value for acceptDefs
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder acceptDefs(Collection acceptDefs) {
this.acceptDefs = Objects.requireNonNull(acceptDefs, "acceptDefs");
initBits &= ~INIT_BIT_ACCEPT_DEFS;
return this;
}
/**
* Initializes the value for the {@link FlowProgram#getStartStepId() startStepId} attribute.
* @param startStepId The value for startStepId
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder startStepId(String startStepId) {
this.startStepId = Objects.requireNonNull(startStepId, "startStepId");
initBits &= ~INIT_BIT_START_STEP_ID;
return this;
}
/**
* Put one entry to the {@link FlowProgram#getSteps() steps} map.
* @param key The key in the steps map
* @param value The associated value in the steps map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putSteps(String key, FlowProgram.FlowProgramStep value) {
this.steps.put(key, value);
return this;
}
/**
* Put one entry to the {@link FlowProgram#getSteps() steps} map. Nulls are not permitted
* @param entry The key and value entry
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putSteps(Map.Entry entry) {
this.steps.put(entry);
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link FlowProgram#getSteps() steps} map. Nulls are not permitted
* @param entries The entries that will be added to the steps map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder steps(Map entries) {
this.steps = ImmutableMap.builder();
return putAllSteps(entries);
}
/**
* Put all mappings from the specified map as entries to {@link FlowProgram#getSteps() steps} map. Nulls are not permitted
* @param entries The entries that will be added to the steps map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putAllSteps(Map entries) {
this.steps.putAll(entries);
return this;
}
/**
* Builds a new {@link ImmutableFlowProgram ImmutableFlowProgram}.
* @return An immutable instance of FlowProgram
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableFlowProgram build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableFlowProgram(acceptDefs, startStepId, steps.build());
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_ACCEPT_DEFS) != 0) attributes.add("acceptDefs");
if ((initBits & INIT_BIT_START_STEP_ID) != 0) attributes.add("startStepId");
return "Cannot build FlowProgram, some of required attributes are not set " + attributes;
}
}
}