io.resys.hdes.client.api.ImmutableCacheEntry Maven / Gradle / Ivy
package io.resys.hdes.client.api;
import com.google.common.base.MoreObjects;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import io.resys.hdes.client.api.ast.AstBody;
import io.resys.hdes.client.api.programs.Program;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
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 HdesCache.CacheEntry}.
*
* Use the builder to create immutable instances:
* {@code ImmutableCacheEntry.builder()}.
*/
@Generated(from = "HdesCache.CacheEntry", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableCacheEntry implements HdesCache.CacheEntry {
private final String id;
private final AstBody.AstSource source;
private final AstBody ast;
private final @Nullable Program program;
private ImmutableCacheEntry(
String id,
AstBody.AstSource source,
AstBody ast,
@Nullable Program program) {
this.id = id;
this.source = source;
this.ast = ast;
this.program = program;
}
/**
* @return The value of the {@code id} attribute
*/
@Override
public String getId() {
return id;
}
/**
* @return The value of the {@code source} attribute
*/
@Override
public AstBody.AstSource getSource() {
return source;
}
/**
* @return The value of the {@code ast} attribute
*/
@Override
public AstBody getAst() {
return ast;
}
/**
* @return The value of the {@code program} attribute
*/
@Override
public Optional getProgram() {
return Optional.ofNullable(program);
}
/**
* Copy the current immutable object by setting a value for the {@link HdesCache.CacheEntry#getId() id} attribute.
* An equals check 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 ImmutableCacheEntry withId(String value) {
String newValue = Objects.requireNonNull(value, "id");
if (this.id.equals(newValue)) return this;
return new ImmutableCacheEntry(newValue, this.source, this.ast, this.program);
}
/**
* Copy the current immutable object by setting a value for the {@link HdesCache.CacheEntry#getSource() source} 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 source
* @return A modified copy of the {@code this} object
*/
public final ImmutableCacheEntry withSource(AstBody.AstSource value) {
if (this.source == value) return this;
AstBody.AstSource newValue = Objects.requireNonNull(value, "source");
return new ImmutableCacheEntry(this.id, newValue, this.ast, this.program);
}
/**
* Copy the current immutable object by setting a value for the {@link HdesCache.CacheEntry#getAst() ast} 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 ast
* @return A modified copy of the {@code this} object
*/
public final ImmutableCacheEntry withAst(AstBody value) {
if (this.ast == value) return this;
AstBody newValue = Objects.requireNonNull(value, "ast");
return new ImmutableCacheEntry(this.id, this.source, newValue, this.program);
}
/**
* Copy the current immutable object by setting a present value for the optional {@link HdesCache.CacheEntry#getProgram() program} attribute.
* @param value The value for program
* @return A modified copy of {@code this} object
*/
public final ImmutableCacheEntry withProgram(Program value) {
@Nullable Program newValue = Objects.requireNonNull(value, "program");
if (this.program == newValue) return this;
return new ImmutableCacheEntry(this.id, this.source, this.ast, newValue);
}
/**
* Copy the current immutable object by setting an optional value for the {@link HdesCache.CacheEntry#getProgram() program} attribute.
* A shallow reference equality check is used on unboxed optional value to prevent copying of the same value by returning {@code this}.
* @param optional A value for program
* @return A modified copy of {@code this} object
*/
@SuppressWarnings("unchecked") // safe covariant cast
public final ImmutableCacheEntry withProgram(Optional extends Program> optional) {
@Nullable Program value = optional.orElse(null);
if (this.program == value) return this;
return new ImmutableCacheEntry(this.id, this.source, this.ast, value);
}
/**
* This instance is equal to all instances of {@code ImmutableCacheEntry} 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 ImmutableCacheEntry
&& equalTo((ImmutableCacheEntry) another);
}
private boolean equalTo(ImmutableCacheEntry another) {
return id.equals(another.id)
&& source.equals(another.source)
&& ast.equals(another.ast)
&& Objects.equals(program, another.program);
}
/**
* Computes a hash code from attributes: {@code id}, {@code source}, {@code ast}, {@code program}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + id.hashCode();
h += (h << 5) + source.hashCode();
h += (h << 5) + ast.hashCode();
h += (h << 5) + Objects.hashCode(program);
return h;
}
/**
* Prints the immutable value {@code CacheEntry} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("CacheEntry")
.omitNullValues()
.add("id", id)
.add("source", source)
.add("ast", ast)
.add("program", program)
.toString();
}
/**
* Creates an immutable copy of a {@link HdesCache.CacheEntry} 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 CacheEntry instance
*/
public static ImmutableCacheEntry copyOf(HdesCache.CacheEntry instance) {
if (instance instanceof ImmutableCacheEntry) {
return (ImmutableCacheEntry) instance;
}
return ImmutableCacheEntry.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableCacheEntry ImmutableCacheEntry}.
*
* ImmutableCacheEntry.builder()
* .id(String) // required {@link HdesCache.CacheEntry#getId() id}
* .source(io.resys.hdes.client.api.ast.AstBody.AstSource) // required {@link HdesCache.CacheEntry#getSource() source}
* .ast(io.resys.hdes.client.api.ast.AstBody) // required {@link HdesCache.CacheEntry#getAst() ast}
* .program(io.resys.hdes.client.api.programs.Program) // optional {@link HdesCache.CacheEntry#getProgram() program}
* .build();
*
* @return A new ImmutableCacheEntry builder
*/
public static ImmutableCacheEntry.Builder builder() {
return new ImmutableCacheEntry.Builder();
}
/**
* Builds instances of type {@link ImmutableCacheEntry ImmutableCacheEntry}.
* 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 = "HdesCache.CacheEntry", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_ID = 0x1L;
private static final long INIT_BIT_SOURCE = 0x2L;
private static final long INIT_BIT_AST = 0x4L;
private long initBits = 0x7L;
private @Nullable String id;
private @Nullable AstBody.AstSource source;
private @Nullable AstBody ast;
private @Nullable Program program;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code CacheEntry} 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
*/
@CanIgnoreReturnValue
public final Builder from(HdesCache.CacheEntry instance) {
Objects.requireNonNull(instance, "instance");
id(instance.getId());
source(instance.getSource());
ast(instance.getAst());
Optional programOptional = instance.getProgram();
if (programOptional.isPresent()) {
program(programOptional);
}
return this;
}
/**
* Initializes the value for the {@link HdesCache.CacheEntry#getId() id} attribute.
* @param id The value for id
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder id(String id) {
this.id = Objects.requireNonNull(id, "id");
initBits &= ~INIT_BIT_ID;
return this;
}
/**
* Initializes the value for the {@link HdesCache.CacheEntry#getSource() source} attribute.
* @param source The value for source
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder source(AstBody.AstSource source) {
this.source = Objects.requireNonNull(source, "source");
initBits &= ~INIT_BIT_SOURCE;
return this;
}
/**
* Initializes the value for the {@link HdesCache.CacheEntry#getAst() ast} attribute.
* @param ast The value for ast
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder ast(AstBody ast) {
this.ast = Objects.requireNonNull(ast, "ast");
initBits &= ~INIT_BIT_AST;
return this;
}
/**
* Initializes the optional value {@link HdesCache.CacheEntry#getProgram() program} to program.
* @param program The value for program
* @return {@code this} builder for chained invocation
*/
@CanIgnoreReturnValue
public final Builder program(Program program) {
this.program = Objects.requireNonNull(program, "program");
return this;
}
/**
* Initializes the optional value {@link HdesCache.CacheEntry#getProgram() program} to program.
* @param program The value for program
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder program(Optional extends Program> program) {
this.program = program.orElse(null);
return this;
}
/**
* Builds a new {@link ImmutableCacheEntry ImmutableCacheEntry}.
* @return An immutable instance of CacheEntry
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableCacheEntry build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableCacheEntry(id, source, ast, program);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_ID) != 0) attributes.add("id");
if ((initBits & INIT_BIT_SOURCE) != 0) attributes.add("source");
if ((initBits & INIT_BIT_AST) != 0) attributes.add("ast");
return "Cannot build CacheEntry, some of required attributes are not set " + attributes;
}
}
}