com.mercateo.spring.security.jwt.token.claim.ImmutableJWTClaims Maven / Gradle / Ivy
Show all versions of spring-security-jwt Show documentation
package com.mercateo.spring.security.jwt.token.claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.vavr.Tuple2;
import io.vavr.collection.HashMap;
import io.vavr.collection.Map;
import java.util.List;
import java.util.Objects;
import javax.annotation.CheckReturnValue;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link JWTClaims}.
*
* Use the builder to create immutable instances:
* {@code ImmutableJWTClaims.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "JWTClaims"})
@Immutable
@CheckReturnValue
final class ImmutableJWTClaims implements JWTClaims {
private final DecodedJWT token;
private final Map claims;
private final int verifiedCount;
private ImmutableJWTClaims(ImmutableJWTClaims.Builder builder) {
this.token = builder.token;
this.claims = builder.claims_build();
this.verifiedCount = builder.verifiedCountIsSet()
? builder.verifiedCount
: JWTClaims.super.verifiedCount();
}
private ImmutableJWTClaims(
DecodedJWT token,
Map claims,
int verifiedCount) {
this.token = token;
this.claims = claims;
this.verifiedCount = verifiedCount;
}
/**
* @return The value of the {@code token} attribute
*/
@Override
public DecodedJWT token() {
return token;
}
/**
* @return The value of the {@code claims} attribute
*/
@Override
public Map claims() {
return claims;
}
/**
* @return The value of the {@code verifiedCount} attribute
*/
@Override
public int verifiedCount() {
return verifiedCount;
}
/**
* Copy the current immutable object by setting a value for the {@link JWTClaims#token() token} 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 token
* @return A modified copy of the {@code this} object
*/
public final ImmutableJWTClaims withToken(DecodedJWT value) {
if (this.token == value) return this;
DecodedJWT newValue = Objects.requireNonNull(value, "token");
return new ImmutableJWTClaims(newValue, this.claims, this.verifiedCount);
}
public ImmutableJWTClaims withClaims(Map value) {
Map newValue = claims_from(value);
if (this.claims == newValue) return this;
return new ImmutableJWTClaims(this.token, newValue, this.verifiedCount);
}
/**
* Copy the current immutable object by setting a value for the {@link JWTClaims#verifiedCount() verifiedCount} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for verifiedCount
* @return A modified copy of the {@code this} object
*/
public final ImmutableJWTClaims withVerifiedCount(int value) {
if (this.verifiedCount == value) return this;
return new ImmutableJWTClaims(this.token, this.claims, value);
}
/**
* This instance is equal to all instances of {@code ImmutableJWTClaims} 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 ImmutableJWTClaims
&& equalTo((ImmutableJWTClaims) another);
}
private boolean equalTo(ImmutableJWTClaims another) {
return token.equals(another.token)
&& this.claims().equals(another.claims())
&& verifiedCount == another.verifiedCount;
}
/**
* Computes a hash code from attributes: {@code token}, {@code claims}, {@code verifiedCount}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + token.hashCode();
h += (h << 5) + (claims().hashCode());
h += (h << 5) + verifiedCount;
return h;
}
/**
* Prints the immutable value {@code JWTClaims} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("JWTClaims")
.omitNullValues()
.add("token", token)
.add("claims", claims().toString())
.add("verifiedCount", verifiedCount)
.toString();
}
/**
* Creates an immutable copy of a {@link JWTClaims} 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 JWTClaims instance
*/
public static ImmutableJWTClaims copyOf(JWTClaims instance) {
if (instance instanceof ImmutableJWTClaims) {
return (ImmutableJWTClaims) instance;
}
return ImmutableJWTClaims.builder()
.from(instance)
.build();
}
private static Map claims_from(Map value) {
return value;
}
/**
* Creates a builder for {@link ImmutableJWTClaims ImmutableJWTClaims}.
* @return A new ImmutableJWTClaims builder
*/
public static ImmutableJWTClaims.Builder builder() {
return new ImmutableJWTClaims.Builder();
}
/**
* Builds instances of type {@link ImmutableJWTClaims ImmutableJWTClaims}.
* 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.
*/
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_TOKEN = 0x1L;
private static final long OPT_BIT_VERIFIED_COUNT = 0x1L;
private long initBits = 0x1L;
private long optBits;
private Map claims_map = HashMap.empty();
private @Nullable DecodedJWT token;
private int verifiedCount;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code JWTClaims} 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(JWTClaims instance) {
Objects.requireNonNull(instance, "instance");
withToken(instance.token());
withClaims(instance.claims());
withVerifiedCount(instance.verifiedCount());
return this;
}
/**
* Initializes the value for the {@link JWTClaims#token() token} attribute.
* @param token The value for token
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder withToken(DecodedJWT token) {
this.token = Objects.requireNonNull(token, "token");
initBits &= ~INIT_BIT_TOKEN;
return this;
}
public Builder putClaims(String key, JWTClaim value) {
this.claims_map = this.claims_map.put(key, value);
return this;
}
public Builder putEntryClaims(Tuple2 entry) {
this.claims_map = this.claims_map.put(entry);
return this;
}
public Builder withClaims(Map elements) {
this.claims_map = elements;
return this;
}
public Builder setJavaMapClaims(java.util.Map in_map) {
this.claims_map = HashMap.ofAll(in_map);
return this;
}
public Builder setEntriesClaims(Iterable> entries) {
this.claims_map = HashMap.ofEntries(entries);
return this;
}
/**
* Initializes the value for the {@link JWTClaims#verifiedCount() verifiedCount} attribute.
* If not set, this attribute will have a default value as returned by the initializer of {@link JWTClaims#verifiedCount() verifiedCount}.
* @param verifiedCount The value for verifiedCount
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder withVerifiedCount(int verifiedCount) {
this.verifiedCount = verifiedCount;
optBits |= OPT_BIT_VERIFIED_COUNT;
return this;
}
/**
* Builds a new {@link ImmutableJWTClaims ImmutableJWTClaims}.
* @return An immutable instance of JWTClaims
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableJWTClaims build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableJWTClaims(this);
}
private boolean verifiedCountIsSet() {
return (optBits & OPT_BIT_VERIFIED_COUNT) != 0;
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if ((initBits & INIT_BIT_TOKEN) != 0) attributes.add("token");
return "Cannot build JWTClaims, some of required attributes are not set " + attributes;
}
private Map claims_build() {
return this.claims_map;
}
}
}