org.xrpl.xrpl4j.keypairs.ImmutableEcDsaSignature Maven / Gradle / Ivy
package org.xrpl.xrpl4j.keypairs;
import com.google.common.base.MoreObjects;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.math.BigInteger;
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;
import org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray;
/**
* Immutable implementation of {@link EcDsaSignature}.
*
* Use the builder to create immutable instances:
* {@code ImmutableEcDsaSignature.builder()}.
*/
@Generated(from = "EcDsaSignature", generator = "Immutables")
@SuppressWarnings({"LocalVariableName", "MethodName", "all"})
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableEcDsaSignature implements EcDsaSignature {
private final BigInteger r;
private final BigInteger s;
private transient final UnsignedByteArray der;
private ImmutableEcDsaSignature(BigInteger r, BigInteger s) {
this.r = r;
this.s = s;
this.der = Objects.requireNonNull(EcDsaSignature.super.der(), "der");
}
/**
* The r component of this {@link EcDsaSignature}.
* @return A {@link BigInteger} denoting the r component of this signature.
*/
@Override
public BigInteger r() {
return r;
}
/**
* The s component of this {@link EcDsaSignature}.
* @return A {@link BigInteger} denoting the r component of this signature.
*/
@Override
public BigInteger s() {
return s;
}
/**
* Encode this {@link EcDsaSignature} to the ASN.1 DER format.
* @return An {@link UnsignedByteArray} containing the bytes of the encoded signature.
*/
@Override
public UnsignedByteArray der() {
return der;
}
/**
* Copy the current immutable object by setting a value for the {@link EcDsaSignature#r() r} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for r
* @return A modified copy of the {@code this} object
*/
public final ImmutableEcDsaSignature withR(BigInteger value) {
BigInteger newValue = Objects.requireNonNull(value, "r");
if (this.r.equals(newValue)) return this;
return validate(new ImmutableEcDsaSignature(newValue, this.s));
}
/**
* Copy the current immutable object by setting a value for the {@link EcDsaSignature#s() s} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for s
* @return A modified copy of the {@code this} object
*/
public final ImmutableEcDsaSignature withS(BigInteger value) {
BigInteger newValue = Objects.requireNonNull(value, "s");
if (this.s.equals(newValue)) return this;
return validate(new ImmutableEcDsaSignature(this.r, newValue));
}
/**
* This instance is equal to all instances of {@code ImmutableEcDsaSignature} 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 ImmutableEcDsaSignature
&& equalTo((ImmutableEcDsaSignature) another);
}
private boolean equalTo(ImmutableEcDsaSignature another) {
return r.equals(another.r)
&& s.equals(another.s)
&& der.equals(another.der);
}
/**
* Computes a hash code from attributes: {@code r}, {@code s}, {@code der}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + r.hashCode();
h += (h << 5) + s.hashCode();
h += (h << 5) + der.hashCode();
return h;
}
/**
* Prints the immutable value {@code EcDsaSignature} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("EcDsaSignature")
.omitNullValues()
.add("r", r)
.add("s", s)
.add("der", der)
.toString();
}
private static ImmutableEcDsaSignature validate(ImmutableEcDsaSignature instance) {
instance.isStrictlyCanonical();
return instance;
}
/**
* Creates an immutable copy of a {@link EcDsaSignature} 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 EcDsaSignature instance
*/
public static ImmutableEcDsaSignature copyOf(EcDsaSignature instance) {
if (instance instanceof ImmutableEcDsaSignature) {
return (ImmutableEcDsaSignature) instance;
}
return ImmutableEcDsaSignature.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableEcDsaSignature ImmutableEcDsaSignature}.
*
* ImmutableEcDsaSignature.builder()
* .r(java.math.BigInteger) // required {@link EcDsaSignature#r() r}
* .s(java.math.BigInteger) // required {@link EcDsaSignature#s() s}
* .build();
*
* @return A new ImmutableEcDsaSignature builder
*/
public static ImmutableEcDsaSignature.Builder builder() {
return new ImmutableEcDsaSignature.Builder();
}
/**
* Builds instances of type {@link ImmutableEcDsaSignature ImmutableEcDsaSignature}.
* 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 = "EcDsaSignature", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_R = 0x1L;
private static final long INIT_BIT_S = 0x2L;
private long initBits = 0x3L;
private @Nullable BigInteger r;
private @Nullable BigInteger s;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code EcDsaSignature} 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(EcDsaSignature instance) {
Objects.requireNonNull(instance, "instance");
r(instance.r());
s(instance.s());
return this;
}
/**
* Initializes the value for the {@link EcDsaSignature#r() r} attribute.
* @param r The value for r
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder r(BigInteger r) {
this.r = Objects.requireNonNull(r, "r");
initBits &= ~INIT_BIT_R;
return this;
}
/**
* Initializes the value for the {@link EcDsaSignature#s() s} attribute.
* @param s The value for s
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder s(BigInteger s) {
this.s = Objects.requireNonNull(s, "s");
initBits &= ~INIT_BIT_S;
return this;
}
/**
* Builds a new {@link ImmutableEcDsaSignature ImmutableEcDsaSignature}.
* @return An immutable instance of EcDsaSignature
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableEcDsaSignature build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return ImmutableEcDsaSignature.validate(new ImmutableEcDsaSignature(r, s));
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_R) != 0) attributes.add("r");
if ((initBits & INIT_BIT_S) != 0) attributes.add("s");
return "Cannot build EcDsaSignature, some of required attributes are not set " + attributes;
}
}
}