org.cloudfoundry.multiapps.controller.persistence.model.ImmutableAccessToken Maven / Gradle / Ivy
package org.cloudfoundry.multiapps.controller.persistence.model;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* Immutable implementation of {@link AccessToken}.
*
* Use the builder to create immutable instances:
* {@code ImmutableAccessToken.builder()}.
*/
@SuppressWarnings({"all"})
public final class ImmutableAccessToken
implements AccessToken {
private final long id;
private final byte[] value;
private final String username;
private final LocalDateTime expiresAt;
private ImmutableAccessToken(ImmutableAccessToken.Builder builder) {
this.value = builder.value;
this.username = builder.username;
this.expiresAt = builder.expiresAt;
this.id = builder.idIsSet()
? builder.id
: AccessToken.super.getId();
}
private ImmutableAccessToken(long id, byte[] value, String username, LocalDateTime expiresAt) {
this.id = id;
this.value = value;
this.username = username;
this.expiresAt = expiresAt;
}
/**
* @return The value of the {@code id} attribute
*/
@JsonProperty("id")
@Override
public long getId() {
return id;
}
/**
* @return A cloned {@code value} array
*/
@JsonProperty("value")
@Override
public byte[] getValue() {
return value.clone();
}
/**
* @return The value of the {@code username} attribute
*/
@JsonProperty("username")
@Override
public String getUsername() {
return username;
}
/**
* @return The value of the {@code expiresAt} attribute
*/
@JsonProperty("expiresAt")
@Override
public LocalDateTime getExpiresAt() {
return expiresAt;
}
/**
* Copy the current immutable object by setting a value for the {@link AccessToken#getId() id} attribute.
* A value equality check is 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 ImmutableAccessToken withId(long value) {
if (this.id == value) return this;
return new ImmutableAccessToken(value, this.value, this.username, this.expiresAt);
}
/**
* Copy the current immutable object with elements that replace the content of {@link AccessToken#getValue() value}.
* The array is cloned before being saved as attribute values.
* @param elements The non-null elements for value
* @return A modified copy of {@code this} object
*/
public final ImmutableAccessToken withValue(byte... elements) {
byte[] newValue = elements.clone();
return new ImmutableAccessToken(this.id, newValue, this.username, this.expiresAt);
}
/**
* Copy the current immutable object by setting a value for the {@link AccessToken#getUsername() username} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for username
* @return A modified copy of the {@code this} object
*/
public final ImmutableAccessToken withUsername(String value) {
String newValue = Objects.requireNonNull(value, "username");
if (this.username.equals(newValue)) return this;
return new ImmutableAccessToken(this.id, this.value, newValue, this.expiresAt);
}
/**
* Copy the current immutable object by setting a value for the {@link AccessToken#getExpiresAt() expiresAt} 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 expiresAt
* @return A modified copy of the {@code this} object
*/
public final ImmutableAccessToken withExpiresAt(LocalDateTime value) {
if (this.expiresAt == value) return this;
LocalDateTime newValue = Objects.requireNonNull(value, "expiresAt");
return new ImmutableAccessToken(this.id, this.value, this.username, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableAccessToken} 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 ImmutableAccessToken
&& equalTo(0, (ImmutableAccessToken) another);
}
private boolean equalTo(int synthetic, ImmutableAccessToken another) {
return id == another.id
&& Arrays.equals(value, another.value)
&& username.equals(another.username)
&& expiresAt.equals(another.expiresAt);
}
/**
* Computes a hash code from attributes: {@code id}, {@code value}, {@code username}, {@code expiresAt}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + Long.hashCode(id);
h += (h << 5) + Arrays.hashCode(value);
h += (h << 5) + username.hashCode();
h += (h << 5) + expiresAt.hashCode();
return h;
}
/**
* Prints the immutable value {@code AccessToken} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "AccessToken{"
+ "id=" + id
+ ", value=" + Arrays.toString(value)
+ ", username=" + username
+ ", expiresAt=" + expiresAt
+ "}";
}
/**
* Utility type used to correctly read immutable object from JSON representation.
* @deprecated Do not use this type directly, it exists only for the Jackson-binding infrastructure
*/
@Deprecated
@JsonDeserialize
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static final class Json implements AccessToken {
long id;
boolean idIsSet;
byte[] value;
String username;
LocalDateTime expiresAt;
@JsonProperty("id")
public void setId(long id) {
this.id = id;
this.idIsSet = true;
}
@JsonProperty("value")
public void setValue(byte[] value) {
this.value = value;
}
@JsonProperty("username")
public void setUsername(String username) {
this.username = username;
}
@JsonProperty("expiresAt")
public void setExpiresAt(LocalDateTime expiresAt) {
this.expiresAt = expiresAt;
}
@Override
public long getId() { throw new UnsupportedOperationException(); }
@Override
public byte[] getValue() { throw new UnsupportedOperationException(); }
@Override
public String getUsername() { throw new UnsupportedOperationException(); }
@Override
public LocalDateTime getExpiresAt() { throw new UnsupportedOperationException(); }
}
/**
* @param json A JSON-bindable data structure
* @return An immutable value type
* @deprecated Do not use this method directly, it exists only for the Jackson-binding infrastructure
*/
@Deprecated
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
static ImmutableAccessToken fromJson(Json json) {
ImmutableAccessToken.Builder builder = ImmutableAccessToken.builder();
if (json.idIsSet) {
builder.id(json.id);
}
if (json.value != null) {
builder.value(json.value);
}
if (json.username != null) {
builder.username(json.username);
}
if (json.expiresAt != null) {
builder.expiresAt(json.expiresAt);
}
return builder.build();
}
/**
* Creates an immutable copy of a {@link AccessToken} 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 AccessToken instance
*/
public static ImmutableAccessToken copyOf(AccessToken instance) {
if (instance instanceof ImmutableAccessToken) {
return (ImmutableAccessToken) instance;
}
return ImmutableAccessToken.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableAccessToken ImmutableAccessToken}.
*
* ImmutableAccessToken.builder()
* .id(long) // optional {@link AccessToken#getId() id}
* .value(byte) // required {@link AccessToken#getValue() value}
* .username(String) // required {@link AccessToken#getUsername() username}
* .expiresAt(java.time.LocalDateTime) // required {@link AccessToken#getExpiresAt() expiresAt}
* .build();
*
* @return A new ImmutableAccessToken builder
*/
public static ImmutableAccessToken.Builder builder() {
return new ImmutableAccessToken.Builder();
}
/**
* Builds instances of type {@link ImmutableAccessToken ImmutableAccessToken}.
* 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.
*/
public static final class Builder {
private static final long INIT_BIT_VALUE = 0x1L;
private static final long INIT_BIT_USERNAME = 0x2L;
private static final long INIT_BIT_EXPIRES_AT = 0x4L;
private static final long OPT_BIT_ID = 0x1L;
private long initBits = 0x7L;
private long optBits;
private long id;
private byte[] value;
private String username;
private LocalDateTime expiresAt;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code AccessToken} 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
*/
public final Builder from(AccessToken instance) {
Objects.requireNonNull(instance, "instance");
this.id(instance.getId());
this.value(instance.getValue());
this.username(instance.getUsername());
this.expiresAt(instance.getExpiresAt());
return this;
}
/**
* Initializes the value for the {@link AccessToken#getId() id} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link AccessToken#getId() id}.
* @param id The value for id
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("id")
public final Builder id(long id) {
this.id = id;
optBits |= OPT_BIT_ID;
return this;
}
/**
* Initializes the value for the {@link AccessToken#getValue() value} attribute.
* @param value The elements for value
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("value")
public final Builder value(byte... value) {
this.value = value.clone();
initBits &= ~INIT_BIT_VALUE;
return this;
}
/**
* Initializes the value for the {@link AccessToken#getUsername() username} attribute.
* @param username The value for username
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("username")
public final Builder username(String username) {
this.username = Objects.requireNonNull(username, "username");
initBits &= ~INIT_BIT_USERNAME;
return this;
}
/**
* Initializes the value for the {@link AccessToken#getExpiresAt() expiresAt} attribute.
* @param expiresAt The value for expiresAt
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("expiresAt")
public final Builder expiresAt(LocalDateTime expiresAt) {
this.expiresAt = Objects.requireNonNull(expiresAt, "expiresAt");
initBits &= ~INIT_BIT_EXPIRES_AT;
return this;
}
/**
* Builds a new {@link ImmutableAccessToken ImmutableAccessToken}.
* @return An immutable instance of AccessToken
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableAccessToken build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableAccessToken(this);
}
private boolean idIsSet() {
return (optBits & OPT_BIT_ID) != 0;
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_VALUE) != 0) attributes.add("value");
if ((initBits & INIT_BIT_USERNAME) != 0) attributes.add("username");
if ((initBits & INIT_BIT_EXPIRES_AT) != 0) attributes.add("expiresAt");
return "Cannot build AccessToken, some of required attributes are not set " + attributes;
}
}
}