org.projectnessie.versioned.persist.adapter.ImmutableContentIdAndBytes Maven / Gradle / Ivy
package org.projectnessie.versioned.persist.adapter;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;
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 ContentIdAndBytes}.
*
* Use the builder to create immutable instances:
* {@code ImmutableContentIdAndBytes.builder()}.
*/
@Generated(from = "ContentIdAndBytes", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableContentIdAndBytes
implements ContentIdAndBytes {
private final ContentId contentId;
private final byte type;
private final ByteString value;
private ImmutableContentIdAndBytes(
ContentId contentId,
byte type,
ByteString value) {
this.contentId = contentId;
this.type = type;
this.value = value;
}
/**
* @return The value of the {@code contentId} attribute
*/
@Override
public ContentId getContentId() {
return contentId;
}
/**
* @return The value of the {@code type} attribute
*/
@Override
public byte getType() {
return type;
}
/**
* @return The value of the {@code value} attribute
*/
@Override
public ByteString getValue() {
return value;
}
/**
* Copy the current immutable object by setting a value for the {@link ContentIdAndBytes#getContentId() contentId} 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 contentId
* @return A modified copy of the {@code this} object
*/
public final ImmutableContentIdAndBytes withContentId(ContentId value) {
if (this.contentId == value) return this;
ContentId newValue = Objects.requireNonNull(value, "contentId");
return new ImmutableContentIdAndBytes(newValue, this.type, this.value);
}
/**
* Copy the current immutable object by setting a value for the {@link ContentIdAndBytes#getType() type} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for type
* @return A modified copy of the {@code this} object
*/
public final ImmutableContentIdAndBytes withType(byte value) {
if (this.type == value) return this;
return new ImmutableContentIdAndBytes(this.contentId, value, this.value);
}
/**
* Copy the current immutable object by setting a value for the {@link ContentIdAndBytes#getValue() value} 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 value
* @return A modified copy of the {@code this} object
*/
public final ImmutableContentIdAndBytes withValue(ByteString value) {
if (this.value == value) return this;
ByteString newValue = Objects.requireNonNull(value, "value");
return new ImmutableContentIdAndBytes(this.contentId, this.type, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableContentIdAndBytes} 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 ImmutableContentIdAndBytes
&& equalTo((ImmutableContentIdAndBytes) another);
}
private boolean equalTo(ImmutableContentIdAndBytes another) {
return contentId.equals(another.contentId)
&& type == another.type
&& value.equals(another.value);
}
/**
* Computes a hash code from attributes: {@code contentId}, {@code type}, {@code value}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + contentId.hashCode();
h += (h << 5) + Bytes.hashCode(type);
h += (h << 5) + value.hashCode();
return h;
}
/**
* Prints the immutable value {@code ContentIdAndBytes} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("ContentIdAndBytes")
.omitNullValues()
.add("contentId", contentId)
.add("type", type)
.add("value", value)
.toString();
}
/**
* Creates an immutable copy of a {@link ContentIdAndBytes} 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 ContentIdAndBytes instance
*/
public static ImmutableContentIdAndBytes copyOf(ContentIdAndBytes instance) {
if (instance instanceof ImmutableContentIdAndBytes) {
return (ImmutableContentIdAndBytes) instance;
}
return ImmutableContentIdAndBytes.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableContentIdAndBytes ImmutableContentIdAndBytes}.
*
* ImmutableContentIdAndBytes.builder()
* .contentId(org.projectnessie.versioned.persist.adapter.ContentId) // required {@link ContentIdAndBytes#getContentId() contentId}
* .type(byte) // required {@link ContentIdAndBytes#getType() type}
* .value(com.google.protobuf.ByteString) // required {@link ContentIdAndBytes#getValue() value}
* .build();
*
* @return A new ImmutableContentIdAndBytes builder
*/
public static ImmutableContentIdAndBytes.Builder builder() {
return new ImmutableContentIdAndBytes.Builder();
}
/**
* Builds instances of type {@link ImmutableContentIdAndBytes ImmutableContentIdAndBytes}.
* 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 = "ContentIdAndBytes", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_CONTENT_ID = 0x1L;
private static final long INIT_BIT_TYPE = 0x2L;
private static final long INIT_BIT_VALUE = 0x4L;
private long initBits = 0x7L;
private @Nullable ContentId contentId;
private byte type;
private @Nullable ByteString value;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code ContentIdAndBytes} 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(ContentIdAndBytes instance) {
Objects.requireNonNull(instance, "instance");
contentId(instance.getContentId());
type(instance.getType());
value(instance.getValue());
return this;
}
/**
* Initializes the value for the {@link ContentIdAndBytes#getContentId() contentId} attribute.
* @param contentId The value for contentId
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder contentId(ContentId contentId) {
this.contentId = Objects.requireNonNull(contentId, "contentId");
initBits &= ~INIT_BIT_CONTENT_ID;
return this;
}
/**
* Initializes the value for the {@link ContentIdAndBytes#getType() type} attribute.
* @param type The value for type
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder type(byte type) {
this.type = type;
initBits &= ~INIT_BIT_TYPE;
return this;
}
/**
* Initializes the value for the {@link ContentIdAndBytes#getValue() value} attribute.
* @param value The value for value
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder value(ByteString value) {
this.value = Objects.requireNonNull(value, "value");
initBits &= ~INIT_BIT_VALUE;
return this;
}
/**
* Builds a new {@link ImmutableContentIdAndBytes ImmutableContentIdAndBytes}.
* @return An immutable instance of ContentIdAndBytes
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableContentIdAndBytes build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableContentIdAndBytes(contentId, type, value);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_CONTENT_ID) != 0) attributes.add("contentId");
if ((initBits & INIT_BIT_TYPE) != 0) attributes.add("type");
if ((initBits & INIT_BIT_VALUE) != 0) attributes.add("value");
return "Cannot build ContentIdAndBytes, some of required attributes are not set " + attributes;
}
}
}