diff options
author | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-09-10 21:22:56 +0300 |
---|---|---|
committer | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-09-10 21:22:56 +0300 |
commit | 3aeb0b08a2b243a6318fe2459a395b258544e9e8 (patch) | |
tree | d6e126b11ed8107d8ea06c64cab1f24312866a31 /src/main | |
parent | d37bd5259adb79e2043cf63bb0e3578418531952 (diff) | |
download | LibGui-3aeb0b08a2b243a6318fe2459a395b258544e9e8.tar.gz LibGui-3aeb0b08a2b243a6318fe2459a395b258544e9e8.tar.bz2 LibGui-3aeb0b08a2b243a6318fe2459a395b258544e9e8.zip |
Add some utility methods to ObservableView
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java | 6 | ||||
-rw-r--r-- | src/main/java/io/github/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<T> implements ObservableView<T> { 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; /** @@ -28,6 +29,20 @@ public interface ObservableView<T> extends Supplier<T> { 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<T> find() { + return Optional.ofNullable(getOrNull()); + } + + /** * Adds a change listener to this property view. * * @param listener the added listener |