
io.stargate.grpc.service.interceptors.ImmutableRequestInfo Maven / Gradle / Ivy
package io.stargate.grpc.service.interceptors;
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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.SocketAddress;
import java.util.ArrayList;
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 NewConnectionInterceptor.RequestInfo}.
*
* Use the builder to create immutable instances:
* {@code ImmutableRequestInfo.builder()}.
*/
@Generated(from = "NewConnectionInterceptor.RequestInfo", generator = "Immutables")
@SuppressWarnings({"all"})
@SuppressFBWarnings
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
final class ImmutableRequestInfo
implements NewConnectionInterceptor.RequestInfo {
private final String token;
private final ImmutableMap headers;
private final @Nullable SocketAddress remoteAddress;
private ImmutableRequestInfo(
String token,
ImmutableMap headers,
@Nullable SocketAddress remoteAddress) {
this.token = token;
this.headers = headers;
this.remoteAddress = remoteAddress;
}
/**
* @return The value of the {@code token} attribute
*/
@Override
public String token() {
return token;
}
/**
* @return The value of the {@code headers} attribute
*/
@Override
public ImmutableMap headers() {
return headers;
}
/**
* @return The value of the {@code remoteAddress} attribute
*/
@Override
public @Nullable SocketAddress remoteAddress() {
return remoteAddress;
}
/**
* Copy the current immutable object by setting a value for the {@link NewConnectionInterceptor.RequestInfo#token() token} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for token
* @return A modified copy of the {@code this} object
*/
public final ImmutableRequestInfo withToken(String value) {
String newValue = Objects.requireNonNull(value, "token");
if (this.token.equals(newValue)) return this;
return new ImmutableRequestInfo(newValue, this.headers, this.remoteAddress);
}
/**
* Copy the current immutable object by replacing the {@link NewConnectionInterceptor.RequestInfo#headers() headers} 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 headers map
* @return A modified copy of {@code this} object
*/
public final ImmutableRequestInfo withHeaders(Map entries) {
if (this.headers == entries) return this;
ImmutableMap newValue = ImmutableMap.copyOf(entries);
return new ImmutableRequestInfo(this.token, newValue, this.remoteAddress);
}
/**
* Copy the current immutable object by setting a value for the {@link NewConnectionInterceptor.RequestInfo#remoteAddress() remoteAddress} 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 remoteAddress (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableRequestInfo withRemoteAddress(@Nullable SocketAddress value) {
if (this.remoteAddress == value) return this;
return new ImmutableRequestInfo(this.token, this.headers, value);
}
/**
* This instance is equal to all instances of {@code ImmutableRequestInfo} 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 ImmutableRequestInfo
&& equalTo((ImmutableRequestInfo) another);
}
private boolean equalTo(ImmutableRequestInfo another) {
return token.equals(another.token)
&& headers.equals(another.headers)
&& Objects.equals(remoteAddress, another.remoteAddress);
}
/**
* Computes a hash code from attributes: {@code token}, {@code headers}, {@code remoteAddress}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + token.hashCode();
h += (h << 5) + headers.hashCode();
h += (h << 5) + Objects.hashCode(remoteAddress);
return h;
}
/**
* Prints the immutable value {@code RequestInfo} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("RequestInfo")
.omitNullValues()
.add("token", token)
.add("headers", headers)
.add("remoteAddress", remoteAddress)
.toString();
}
/**
* Creates an immutable copy of a {@link NewConnectionInterceptor.RequestInfo} 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 RequestInfo instance
*/
public static ImmutableRequestInfo copyOf(NewConnectionInterceptor.RequestInfo instance) {
if (instance instanceof ImmutableRequestInfo) {
return (ImmutableRequestInfo) instance;
}
return ImmutableRequestInfo.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableRequestInfo ImmutableRequestInfo}.
*
* ImmutableRequestInfo.builder()
* .token(String) // required {@link NewConnectionInterceptor.RequestInfo#token() token}
* .putHeaders|putAllHeaders(String => String) // {@link NewConnectionInterceptor.RequestInfo#headers() headers} mappings
* .remoteAddress(java.net.SocketAddress | null) // nullable {@link NewConnectionInterceptor.RequestInfo#remoteAddress() remoteAddress}
* .build();
*
* @return A new ImmutableRequestInfo builder
*/
public static ImmutableRequestInfo.Builder builder() {
return new ImmutableRequestInfo.Builder();
}
/**
* Builds instances of type {@link ImmutableRequestInfo ImmutableRequestInfo}.
* 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 = "NewConnectionInterceptor.RequestInfo", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_TOKEN = 0x1L;
private long initBits = 0x1L;
private @Nullable String token;
private ImmutableMap.Builder headers = ImmutableMap.builder();
private @Nullable SocketAddress remoteAddress;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code RequestInfo} 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(NewConnectionInterceptor.RequestInfo instance) {
Objects.requireNonNull(instance, "instance");
token(instance.token());
putAllHeaders(instance.headers());
@Nullable SocketAddress remoteAddressValue = instance.remoteAddress();
if (remoteAddressValue != null) {
remoteAddress(remoteAddressValue);
}
return this;
}
/**
* Initializes the value for the {@link NewConnectionInterceptor.RequestInfo#token() token} attribute.
* @param token The value for token
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder token(String token) {
this.token = Objects.requireNonNull(token, "token");
initBits &= ~INIT_BIT_TOKEN;
return this;
}
/**
* Put one entry to the {@link NewConnectionInterceptor.RequestInfo#headers() headers} map.
* @param key The key in the headers map
* @param value The associated value in the headers map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putHeaders(String key, String value) {
this.headers.put(key, value);
return this;
}
/**
* Put one entry to the {@link NewConnectionInterceptor.RequestInfo#headers() headers} 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 putHeaders(Map.Entry entry) {
this.headers.put(entry);
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link NewConnectionInterceptor.RequestInfo#headers() headers} map. Nulls are not permitted
* @param entries The entries that will be added to the headers map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder headers(Map entries) {
this.headers = ImmutableMap.builder();
return putAllHeaders(entries);
}
/**
* Put all mappings from the specified map as entries to {@link NewConnectionInterceptor.RequestInfo#headers() headers} map. Nulls are not permitted
* @param entries The entries that will be added to the headers map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putAllHeaders(Map entries) {
this.headers.putAll(entries);
return this;
}
/**
* Initializes the value for the {@link NewConnectionInterceptor.RequestInfo#remoteAddress() remoteAddress} attribute.
* @param remoteAddress The value for remoteAddress (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder remoteAddress(@Nullable SocketAddress remoteAddress) {
this.remoteAddress = remoteAddress;
return this;
}
/**
* Builds a new {@link ImmutableRequestInfo ImmutableRequestInfo}.
* @return An immutable instance of RequestInfo
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableRequestInfo build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableRequestInfo(token, headers.build(), remoteAddress);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_TOKEN) != 0) attributes.add("token");
return "Cannot build RequestInfo, some of required attributes are not set " + attributes;
}
}
}