From 3aeb0b08a2b243a6318fe2459a395b258544e9e8 Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Fri, 10 Sep 2021 21:22:56 +0300 Subject: Add some utility methods to ObservableView --- .../cotton/gui/widget/data/ObservableProperty.java | 6 ------ .../cottonmc/cotton/gui/widget/data/ObservableView.java | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java index 49dbf9f..eb92387 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java @@ -45,12 +45,6 @@ public final class ObservableProperty implements ObservableView { return hasValue; } - /** - * {@return the value of this property} - * @throws IllegalStateException if not initialized - * @throws NullPointerException if the value is null and null values aren't allowed - * @see #hasValue() - */ @Override public T get() { if (!hasValue) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableView.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableView.java index 164f51b..8bad55c 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableView.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableView.java @@ -3,6 +3,7 @@ package io.github.cottonmc.cotton.gui.widget.data; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import java.util.Optional; import java.util.function.Supplier; /** @@ -27,6 +28,20 @@ public interface ObservableView extends Supplier { @Override T get(); + /** + * {@return the value of this property, or null if not initialized} + */ + default @Nullable T getOrNull() { + return hasValue() ? get() : null; + } + + /** + * {@return the nonnull value of this property, or {@link Optional#empty()} if null or not initialized} + */ + default Optional find() { + return Optional.ofNullable(getOrNull()); + } + /** * Adds a change listener to this property view. * -- cgit