aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/src/main/java/com/vaadin/data/HasValue.java13
1 files changed, 13 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/data/HasValue.java b/server/src/main/java/com/vaadin/data/HasValue.java
index 962a3e9620..de2f86550f 100644
--- a/server/src/main/java/com/vaadin/data/HasValue.java
+++ b/server/src/main/java/com/vaadin/data/HasValue.java
@@ -19,6 +19,7 @@ import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.EventObject;
import java.util.Objects;
+import java.util.Optional;
import com.vaadin.event.SerializableEventListener;
import com.vaadin.server.Setter;
@@ -220,6 +221,18 @@ public interface HasValue<V> extends Serializable {
}
/**
+ * Returns the current value of this object, wrapped in an {@code Optional}.
+ * <p>
+ * The {@code Optional} will be empty if the value is {@code null} or
+ * {@code isEmpty()} returns {@code true}.
+ *
+ * @return the current value, wrapped in an {@code Optional}
+ */
+ public default Optional<V> getOptionalValue() {
+ return isEmpty() ? Optional.empty() : Optional.ofNullable(getValue());
+ }
+
+ /**
* Returns whether this {@code HasValue} is considered to be empty.
* <p>
* By default this is an equality check between current value and empty