
com.mercateo.spring.security.jwt.token.claim.JWTClaims 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.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link _JWTClaims}.
*
* Use the builder to create immutable instances:
* {@code JWTClaims.builder()}.
*/
@Generated(from = "_JWTClaims", generator = "Immutables")
@SuppressWarnings({"all"})
public final class JWTClaims implements com.mercateo.spring.security.jwt.token.claim._JWTClaims {
private final DecodedJWT token;
private final ImmutableMap claims;
private final int verifiedCount;
private JWTClaims(JWTClaims.Builder builder) {
this.token = builder.token;
this.claims = builder.claims.build();
this.verifiedCount = builder.verifiedCountIsSet()
? builder.verifiedCount
: _JWTClaims.super.verifiedCount();
}
private JWTClaims(
DecodedJWT token,
ImmutableMap 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 ImmutableMap 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 JWTClaims withToken(DecodedJWT value) {
if (this.token == value) return this;
DecodedJWT newValue = Objects.requireNonNull(value, "token");
return new JWTClaims(newValue, this.claims, this.verifiedCount);
}
/**
* Copy the current immutable object by replacing the {@link _JWTClaims#claims() claims} 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 claims map
* @return A modified copy of {@code this} object
*/
public final JWTClaims withClaims(Map entries) {
if (this.claims == entries) return this;
ImmutableMap newValue = ImmutableMap.copyOf(entries);
return new JWTClaims(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 JWTClaims withVerifiedCount(int value) {
if (this.verifiedCount == value) return this;
return new JWTClaims(this.token, this.claims, value);
}
/**
* This instance is equal to all instances of {@code JWTClaims} 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 JWTClaims
&& equalTo((JWTClaims) another);
}
private boolean equalTo(JWTClaims another) {
return token.equals(another.token)
&& 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() {
@Var 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)
.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 JWTClaims copyOf(_JWTClaims instance) {
if (instance instanceof JWTClaims) {
return (JWTClaims) instance;
}
return JWTClaims.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link JWTClaims JWTClaims}.
*
* JWTClaims.builder()
* .token(com.auth0.jwt.interfaces.DecodedJWT) // required {@link _JWTClaims#token() token}
* .putClaims|putAllClaims(String => JWTClaim) // {@link _JWTClaims#claims() claims} mappings
* .verifiedCount(int) // optional {@link _JWTClaims#verifiedCount() verifiedCount}
* .build();
*
* @return A new JWTClaims builder
*/
public static JWTClaims.Builder builder() {
return new JWTClaims.Builder();
}
/**
* Builds instances of type {@link JWTClaims JWTClaims}.
* 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 = "_JWTClaims", generator = "Immutables")
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 DecodedJWT token;
private ImmutableMap.Builder claims = ImmutableMap.builder();
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.
* 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(_JWTClaims instance) {
Objects.requireNonNull(instance, "instance");
token(instance.token());
putAllClaims(instance.claims());
verifiedCount(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 token(DecodedJWT token) {
this.token = Objects.requireNonNull(token, "token");
initBits &= ~INIT_BIT_TOKEN;
return this;
}
/**
* Put one entry to the {@link _JWTClaims#claims() claims} map.
* @param key The key in the claims map
* @param value The associated value in the claims map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putClaims(String key, JWTClaim value) {
this.claims.put(key, value);
return this;
}
/**
* Put one entry to the {@link _JWTClaims#claims() claims} 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 putClaims(Map.Entry entry) {
this.claims.put(entry);
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link _JWTClaims#claims() claims} map. Nulls are not permitted
* @param entries The entries that will be added to the claims map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder claims(Map entries) {
this.claims = ImmutableMap.builder();
return putAllClaims(entries);
}
/**
* Put all mappings from the specified map as entries to {@link _JWTClaims#claims() claims} map. Nulls are not permitted
* @param entries The entries that will be added to the claims map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putAllClaims(Map entries) {
this.claims.putAll(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 verifiedCount(int verifiedCount) {
this.verifiedCount = verifiedCount;
optBits |= OPT_BIT_VERIFIED_COUNT;
return this;
}
/**
* Builds a new {@link JWTClaims JWTClaims}.
* @return An immutable instance of JWTClaims
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public JWTClaims build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new JWTClaims(this);
}
private boolean verifiedCountIsSet() {
return (optBits & OPT_BIT_VERIFIED_COUNT) != 0;
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_TOKEN) != 0) attributes.add("token");
return "Cannot build JWTClaims, some of required attributes are not set " + attributes;
}
}
}