org.cloudfoundry.multiapps.controller.process.util.ImmutableModuleDeterminer Maven / Gradle / Ivy
package org.cloudfoundry.multiapps.controller.process.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.cloudfoundry.multiapps.controller.core.cf.metadata.processor.MtaMetadataParser;
import org.cloudfoundry.multiapps.controller.process.steps.ProcessContext;
/**
* Immutable implementation of {@link ModuleDeterminer}.
*
* Use the builder to create immutable instances:
* {@code ImmutableModuleDeterminer.builder()}.
*/
@SuppressWarnings({"all"})
public final class ImmutableModuleDeterminer
extends ModuleDeterminer {
private final MtaMetadataParser mtaMetadataParser;
private final ProcessContext context;
private ImmutableModuleDeterminer(
MtaMetadataParser mtaMetadataParser,
ProcessContext context) {
this.mtaMetadataParser = mtaMetadataParser;
this.context = context;
}
/**
* @return The value of the {@code mtaMetadataParser} attribute
*/
@Override
public MtaMetadataParser getMtaMetadataParser() {
return mtaMetadataParser;
}
/**
* @return The value of the {@code context} attribute
*/
@Override
public ProcessContext getContext() {
return context;
}
/**
* Copy the current immutable object by setting a value for the {@link ModuleDeterminer#getMtaMetadataParser() mtaMetadataParser} 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 mtaMetadataParser
* @return A modified copy of the {@code this} object
*/
public final ImmutableModuleDeterminer withMtaMetadataParser(MtaMetadataParser value) {
if (this.mtaMetadataParser == value) return this;
MtaMetadataParser newValue = Objects.requireNonNull(value, "mtaMetadataParser");
return new ImmutableModuleDeterminer(newValue, this.context);
}
/**
* Copy the current immutable object by setting a value for the {@link ModuleDeterminer#getContext() context} 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 context
* @return A modified copy of the {@code this} object
*/
public final ImmutableModuleDeterminer withContext(ProcessContext value) {
if (this.context == value) return this;
ProcessContext newValue = Objects.requireNonNull(value, "context");
return new ImmutableModuleDeterminer(this.mtaMetadataParser, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableModuleDeterminer} 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 ImmutableModuleDeterminer
&& equalTo(0, (ImmutableModuleDeterminer) another);
}
private boolean equalTo(int synthetic, ImmutableModuleDeterminer another) {
return mtaMetadataParser.equals(another.mtaMetadataParser)
&& context.equals(another.context);
}
/**
* Computes a hash code from attributes: {@code mtaMetadataParser}, {@code context}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + mtaMetadataParser.hashCode();
h += (h << 5) + context.hashCode();
return h;
}
/**
* Prints the immutable value {@code ModuleDeterminer} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "ModuleDeterminer{"
+ "mtaMetadataParser=" + mtaMetadataParser
+ ", context=" + context
+ "}";
}
/**
* Creates an immutable copy of a {@link ModuleDeterminer} 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 ModuleDeterminer instance
*/
public static ImmutableModuleDeterminer copyOf(ModuleDeterminer instance) {
if (instance instanceof ImmutableModuleDeterminer) {
return (ImmutableModuleDeterminer) instance;
}
return ImmutableModuleDeterminer.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableModuleDeterminer ImmutableModuleDeterminer}.
*
* ImmutableModuleDeterminer.builder()
* .mtaMetadataParser(org.cloudfoundry.multiapps.controller.core.cf.metadata.processor.MtaMetadataParser) // required {@link ModuleDeterminer#getMtaMetadataParser() mtaMetadataParser}
* .context(org.cloudfoundry.multiapps.controller.process.steps.ProcessContext) // required {@link ModuleDeterminer#getContext() context}
* .build();
*
* @return A new ImmutableModuleDeterminer builder
*/
public static ImmutableModuleDeterminer.Builder builder() {
return new ImmutableModuleDeterminer.Builder();
}
/**
* Builds instances of type {@link ImmutableModuleDeterminer ImmutableModuleDeterminer}.
* 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_MTA_METADATA_PARSER = 0x1L;
private static final long INIT_BIT_CONTEXT = 0x2L;
private long initBits = 0x3L;
private MtaMetadataParser mtaMetadataParser;
private ProcessContext context;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code ModuleDeterminer} 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(ModuleDeterminer instance) {
Objects.requireNonNull(instance, "instance");
this.mtaMetadataParser(instance.getMtaMetadataParser());
this.context(instance.getContext());
return this;
}
/**
* Initializes the value for the {@link ModuleDeterminer#getMtaMetadataParser() mtaMetadataParser} attribute.
* @param mtaMetadataParser The value for mtaMetadataParser
* @return {@code this} builder for use in a chained invocation
*/
public final Builder mtaMetadataParser(MtaMetadataParser mtaMetadataParser) {
this.mtaMetadataParser = Objects.requireNonNull(mtaMetadataParser, "mtaMetadataParser");
initBits &= ~INIT_BIT_MTA_METADATA_PARSER;
return this;
}
/**
* Initializes the value for the {@link ModuleDeterminer#getContext() context} attribute.
* @param context The value for context
* @return {@code this} builder for use in a chained invocation
*/
public final Builder context(ProcessContext context) {
this.context = Objects.requireNonNull(context, "context");
initBits &= ~INIT_BIT_CONTEXT;
return this;
}
/**
* Builds a new {@link ImmutableModuleDeterminer ImmutableModuleDeterminer}.
* @return An immutable instance of ModuleDeterminer
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableModuleDeterminer build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableModuleDeterminer(mtaMetadataParser, context);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_MTA_METADATA_PARSER) != 0) attributes.add("mtaMetadataParser");
if ((initBits & INIT_BIT_CONTEXT) != 0) attributes.add("context");
return "Cannot build ModuleDeterminer, some of required attributes are not set " + attributes;
}
}
}