From b30802c1b38ce69adb0dc1843cb664015df61ef5 Mon Sep 17 00:00:00 2001 From: Badbird5907 <50347938+Badbird5907@users.noreply.github.com> Date: Thu, 26 Oct 2023 03:01:24 -0400 Subject: [PATCH] API: Add Tristate#fromOptionalBoolean (#1125) --- .../api/permission/Tristate.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/com/velocitypowered/api/permission/Tristate.java b/api/src/main/java/com/velocitypowered/api/permission/Tristate.java index 189cc2789..ddbe26a86 100644 --- a/api/src/main/java/com/velocitypowered/api/permission/Tristate.java +++ b/api/src/main/java/com/velocitypowered/api/permission/Tristate.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 Velocity Contributors + * Copyright (C) 2018-2023 Velocity Contributors * * The Velocity API is licensed under the terms of the MIT License. For more details, * reference the LICENSE file in the api top-level directory. @@ -7,6 +7,7 @@ package com.velocitypowered.api.permission; +import java.util.Optional; import net.kyori.adventure.util.TriState; import org.checkerframework.checker.nullness.qual.Nullable; @@ -66,6 +67,21 @@ public enum Tristate { return val ? TRUE : FALSE; } + /** + * Returns a {@link Tristate} from an {@link Optional}. + * + *

Unlike {@link #fromBoolean(boolean)}, this method returns {@link #UNDEFINED} + * if the value is empty.

+ * + * @param val the optional boolean value + * @return {@link #UNDEFINED}, {@link #TRUE} or {@link #FALSE}, if the value is empty, + * true or false, respectively. + */ + public static Tristate fromOptionalBoolean(Optional val) { + return val.map(Tristate::fromBoolean).orElse(UNDEFINED); + } + + private final boolean booleanValue; Tristate(boolean booleanValue) {