discord4j.discordjson.OptionalIdEncoding Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of discord-json-encoding Show documentation
Show all versions of discord-json-encoding Show documentation
Discord entity domain as immutable Jackson objects
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;
}
}
}