All Downloads are FREE. Search and download functionalities are using the official Maven repository.

discord4j.discordjson.Id Maven / Gradle / Ivy

The newest version!
package discord4j.discordjson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

/**
 * Represents an unsigned 64-bit ID. This object is serialized through String despite using long field for
 * storage and is used when building immutable objects for an alternative compact structure, reducing required memory.
 */
public class Id {

    private final long value;

    /**
     * Constructs an {@code ID} utilizing an unsigned ID.
     *
     * @param value The unsigned ID to construct a {@code ID}.
     * @return A constructed {@code ID} with the unsigned ID.
     */
    public static Id of(final long value) {
        return new Id(value);
    }

    /**
     * Constructs an {@code ID} utilizing an unsigned ID.
     *
     * @param value The unsigned ID to construct a {@code ID}. Must be non-null.
     * @return A constructed {@code ID} with the unsigned ID.
     * @throws NumberFormatException If {@code id} is not an unsigned ID.
     */
    public static Id of(final String value) {
        return new Id(value);
    }

    @JsonCreator
    private Id(String value) {
        this.value = Long.parseUnsignedLong(value);
    }

    @JsonCreator
    private Id(long value) {
        this.value = value;
    }

    /**
     * Gets the unsigned ID as an object String.
     *
     * @return The unsigned ID as String.
     */
    @JsonValue
    public String asString() {
        return Long.toUnsignedString(value);
    }

    /**
     * Gets the unsigned ID as a primitive long.
     *
     * @return The unsigned ID as long.
     */
    public long asLong() {
        return value;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Id id = (Id) o;
        return value == id.value;
    }

    @Override
    public int hashCode() {
        return Long.hashCode(value);
    }

    @Override
    public String toString() {
        return asString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy