optional: java.util.Optional module (factories, extraction, orElse vs orElseGet, anti-patterns, Stream interop, JSpecify contrast)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b6gdUKYmBw6bUKcM7t
This commit is contained in:
Ankur
2026-11-25 22:58:57 +05:30
co-authored by Claude Sonnet 5.5
parent cca173d104
commit 47e4ccbefa
25 changed files with 793 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
Compiled from "OptionalInt.java"
public final class java.util.OptionalInt {
public static java.util.OptionalInt empty();
public static java.util.OptionalInt of(int);
public int getAsInt();
public boolean isPresent();
public boolean isEmpty();
public void ifPresent(java.util.function.IntConsumer);
public void ifPresentOrElse(java.util.function.IntConsumer, java.lang.Runnable);
public java.util.stream.IntStream stream();
public int orElse(int);
public int orElseGet(java.util.function.IntSupplier);
public int orElseThrow();
public <X extends java.lang.Throwable> int orElseThrow(java.util.function.Supplier<? extends X>) throws X;
public boolean equals(java.lang.Object);
public int hashCode();
public java.lang.String toString();
static {};
}
+6
View File
@@ -0,0 +1,6 @@
isEmpty() since 11
ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) since 9
or(Supplier<? extends Optional<? extends T>> supplier) since 9
stream() since 9
orElseThrow() since 10
orElseThrow(Supplier<? extends X> exceptionSupplier) since 1.8 (class-level)
+6
View File
@@ -0,0 +1,6 @@
empty : Optional.empty
of : Optional[ankur]
ofNullable : Optional.empty
of(null) : java.lang.NullPointerException, message=null
equals : true
empty==empty : true
+10
View File
@@ -0,0 +1,10 @@
isPresent : true
isEmpty : true
get (present): Mumbai
get (empty) : java.util.NoSuchElementException: No value present
orElseThrow(): java.util.NoSuchElementException: No value present
orElse : Unknown City
orElseGet : DefaultCity
orElseThrow(s): java.lang.IllegalStateException: City must be set
ifPresent : City is Mumbai
ifPresentOrElse: No city configured
+10
View File
@@ -0,0 +1,10 @@
name : Ankur
pincode : 411001
missing pin : N/A
map to null : Optional.empty
map nests : Optional[Optional[Address[city=Pune, pincode=411001]]]
flatMap flat : Optional[Address[city=Pune, pincode=411001]]
flatMap city : Pune
filter match : 411001
filter miss : not 400xx
or fallback : 110001
+5
View File
@@ -0,0 +1,5 @@
Found : Laptop
Required : Laptop
Phone - Rs 29999.00
Product not available
Missing : Product 99 not found
+7
View File
@@ -0,0 +1,7 @@
orElse on PRESENT : default built 1 time(s)
orElseGet on PRESENT : default built 0 time(s)
orElse on EMPTY : default built 1 time(s)
orElseGet on EMPTY : default built 1 time(s)
orElse on PRESENT with failing default : threw default lookup failed
orElseGet on PRESENT with failing default : cached
200 calls, slow default (indicative): orElse >= 1000 ms: true, orElseGet < 100 ms: true
+11
View File
@@ -0,0 +1,11 @@
1 isPresent+get : ANKUR | map+orElse : ANKUR
2 Optional field : java.io.NotSerializableException: java.util.Optional
2 nullable field : serialized OK, getEmail() = Optional[[email protected]]
3 greet(empty) : Hello, guest
3 greet(null) : NullPointerException, Cannot invoke "java.util.Optional.orElse(Object)" because "name" is null
4 return null : NullPointerException, Cannot invoke "java.util.Optional.map(java.util.function.Function)" because the return value of "com.ankurm.optional.AntiPatternsDemo.brokenLookup()" is null
4 return empty() : 0
5 of(map.get(..)) : NullPointerException (use ofNullable)
5 ofNullable : Optional.empty
6 Optional<List> : empty list wrapped = Optional[[]] | plain list = []
6 states : Optional<List> has 3 (empty, present-empty, present-full), List has 2
+16
View File
@@ -0,0 +1,16 @@
filter+get : [a, c]
flatMap(::stream) : [a, c]
stream sizes : present=1 empty=0
findFirst hit : Optional[8]
findFirst miss : Optional.empty
max : Optional[pune]
OptionalInt : OptionalInt[9] getAsInt=9
OptionalDouble : OptionalDouble.empty orElse(-1)=-1.0
or() chain, cache hits:
consulted cache
result = Optional[from-cache]
or() chain, cache and db miss:
consulted cache
consulted database
consulted config
result = Optional[from-config]
+6
View File
@@ -0,0 +1,6 @@
display (null nickname) : acc-1
display (set nickname) : Ankur
rawNickname : null
describe(null) : no value
@Nullable targets : [TYPE_USE]
package is @NullMarked : true
+4
View File
@@ -0,0 +1,4 @@
-------------------------------------------------------------------------------
Test set: com.ankurm.optional.OptionalContractTest
-------------------------------------------------------------------------------
Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.135 s -- in com.ankurm.optional.OptionalContractTest