The decision
Show a preferred display name after stripping surrounding Java whitespace. If the profile or name is missing, or stripping leaves it empty, request a fallback exactly once. Loading the fallback can involve a directory lookup.
Read the Kotlin
Trace one receiver at a time
A ?. calls the next operation only on a non-null receiver. takeIf returns the receiver when its predicate is true and null otherwise. Follow a named profile and an unnamed one through the chain. The fallback lambda is a function value, and the parentheses in fallback() are where its work actually starts.
val name = " Jo ".trim().takeIf { it.isNotEmpty() }
val label = name ?: lookup() // keeps Jo, skips lookupGive blank text an explicit policy
Trimming a string of whitespace produces an empty string. Empty is a real String value, so Elvis accepts it. The filter between trim and Elvis is what turns empty into missing. Remove just that line and a tab-only display name can render as a blank label even when a fallback is available.
val trimmed = "\t".trim()
trimmed ?: "Guest" // ""
trimmed.takeIf { it.isNotEmpty() } ?: "Guest" // "Guest"Count fallback calls across languages
Java evaluates method arguments before calling the method. Passing lookup() to orElse therefore performs the lookup even when the Optional holds a name. With orElseGet Java passes a supplier and Optional invokes it only for absence. This is the evaluation order to preserve when a fallback reads the network or increments a counter.
The full example
import java.util.Optional;
import java.util.function.Supplier;
class Lesson {
record Profile(String preferred) {}
static String displayName(Profile profile, Supplier<String> fallback) {
return Optional.ofNullable(profile)
.map(Profile::preferred)
.map(String::strip)
.filter(s -> !s.isEmpty())
.orElseGet(fallback);
}
}data class Profile(val preferred: String?)
fun displayName(profile: Profile?, fallback: () -> String): String =
profile?.preferred
?.trim(Character::isWhitespace)
?.takeIf { it.isNotEmpty() }
?: fallback()Inputs and outcomes
| Input | Outcome | Why it matters |
|---|---|---|
Preferred name " Ada " and fallback "Guest" | Ada | The fallback is never requested. |
Preferred name containing only a tab | Guest | Strip before checking whether the name is empty. |
Missing profile | Guest | The lookup runs once, at the end of the chain. |
Walk through the code
- The first safe call handles a missing profile. The next handles a missing preferred name. A null receiver skips that step and carries null forward.
- trim takes the JVM whitespace predicate so both examples follow String.strip semantics. takeIf turns the resulting empty string into null. The Elvis operator itself considers only null.
- The expression to the right of ?: runs only when the left side is null. The corresponding Optional method is orElseGet.
The Java habit
Computing fallback() in a local variable before the chain triggers the lookup for every profile. In Java, orElse(fallback.get()) has the same eager evaluation pitfall.
When to choose differently
If support needs different messages for a missing profile, a missing setting, and a blank setting, branching preserves those distinctions. This chain intentionally collapses them.