summaryrefslogtreecommitdiffstats
path: root/StOptional.java
diff options
context:
space:
mode:
authorSquibid <me@zacharyscheiman.com>2024-05-09 22:39:43 -0400
committerSquibid <me@zacharyscheiman.com>2024-05-09 22:39:43 -0400
commitd9c0cd4cacb2b9e57db8d5359a837869bb651ed4 (patch)
treee5da110ab580aacf0edcfa96088ebdad746b2ed3 /StOptional.java
downloadStOptional-master.tar.gz
StOptional-master.tar.bz2
StOptional-master.zip
initial commitHEADmaster
Diffstat (limited to 'StOptional.java')
-rw-r--r--StOptional.java36
1 files changed, 36 insertions, 0 deletions
diff --git a/StOptional.java b/StOptional.java
new file mode 100644
index 0000000..308d29c
--- /dev/null
+++ b/StOptional.java
@@ -0,0 +1,36 @@
+/*
+ * copyright (c) squibid 2024 under the Beerware license.
+ *
+ * If we meet some day, and you think this stuff is worth it, you can buy me a
+ * beer in return.
+ */
+
+import java.util.Optional;
+
+/**
+ * Stop Optionals from being used!
+ * safely reducing optionals back to their original types.
+ */
+public final class StOptional {
+ /**
+ * Reduce any optional down to it's original type
+ *
+ * @param o Optional<T> to reduce to it's original type
+ * @return <T> value of optional in original type or null
+ */
+ public static final <T> T reduce(Optional<T> o) {
+ return nullish(o) ? null : o.get();
+ }
+
+ /**
+ * Checks if Optional<?> is null or empty
+ *
+ * @param o Optional<?> optional to check for null like properties
+ * @return boolean true if null or empty, false otherwise
+ */
+ public static final boolean nullish(Optional<?> o) {
+ if (o == null || o.isEmpty())
+ return true;
+ return false;
+ }
+}