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

discord4j.discordjson.OptionalIdEncoding Maven / Gradle / Ivy

There is a newer version: 1.7.10
Show newest version
package discord4j.discordjson;

import org.immutables.encode.Encoding;

import java.util.Objects;
import java.util.Optional;

@Encoding
public class OptionalIdEncoding {

    @Encoding.Impl(virtual = true)
    private Optional optional = Optional.empty();

    private final long value = optional
            .map(discord4j.discordjson.Id::asLong)
            .orElse(0L);
    private final boolean present = optional.isPresent();

    @Encoding.Expose
    Optional get() {
        return present ? Optional.of(discord4j.discordjson.Id.of(value)) : Optional.empty();
    }

    @Encoding.Naming("is*Present")
    boolean isPresent() {
        return present;
    }

    @Encoding.Naming("*OrElse")
    long orElse(long defaultValue) {
        return present ? value : defaultValue;
    }

    @Encoding.Copy
    public Optional withOptional(final Optional value) {
        return Objects.requireNonNull(value);
    }

    @Encoding.Copy
    public Optional with(final long value) {
        return Optional.of(discord4j.discordjson.Id.of(value));
    }

    @Encoding.Builder
    static class Builder {

        private Optional optional = Optional.empty();

        @Encoding.Init
        public void setStringValue(String value) {
            this.optional = Optional.of(discord4j.discordjson.Id.of(value));
        }

        @Encoding.Init
        public void setLongValue(long value) {
            this.optional = Optional.of(discord4j.discordjson.Id.of(value));
        }

        @Encoding.Init
        @Encoding.Copy
        public void copyOptionalId(Optional value) {
            this.optional = value;
        }

        @Encoding.Build
        Optional build() {
            return this.optional;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy