From f06d9233a6f75031741693e1f822b182484ec986 Mon Sep 17 00:00:00 2001 From: Greg Gibeling Date: Mon, 7 Oct 2024 17:53:23 -0700 Subject: [PATCH] G2-1659 castIfInstanceOptional --- .../java/com/g2forge/alexandria/java/type/IDynamicType.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ax-java/src/main/java/com/g2forge/alexandria/java/type/IDynamicType.java b/ax-java/src/main/java/com/g2forge/alexandria/java/type/IDynamicType.java index fe1ada4e..a88d7c31 100644 --- a/ax-java/src/main/java/com/g2forge/alexandria/java/type/IDynamicType.java +++ b/ax-java/src/main/java/com/g2forge/alexandria/java/type/IDynamicType.java @@ -1,5 +1,6 @@ package com.g2forge.alexandria.java.type; +import java.util.Optional; import java.util.stream.Stream; import com.g2forge.alexandria.annotations.note.Note; @@ -14,6 +15,11 @@ public default Stream castIfInstance(Object value) { return Stream.of(cast(value)); } + public default Optional castIfInstanceOptional(Object value) { + if (!isInstance(value)) return Optional.empty(); + return Optional.of(cast(value)); + } + @Note(type = NoteType.TODO, value = "All implementations of this method need to be implemented in a generic-safe manner") public default boolean isAssignableFrom(IDynamicType type) { throw new NotYetImplementedError();