Interface View<S>
- Type Parameters:
S- The state type this view renders
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
A View is a pure function from State to String.
Views are the building blocks of functional TUIs. They:
- Take immutable state as input
- Return a String representation
- Have no side effects
- Compose via
andThen(View)
Example usage:
View<AppState> header = state -> "=== " + state.title() + " ===\n";
View<AppState> content = state -> state.items().stream()...;
View<AppState> footer = state -> "\nTotal: " + state.count();
// Compose views
View<AppState> screen = header
.andThen(content)
.andThen(footer);
// Render (single side effect)
System.out.print(screen.apply(state));
-
Method Summary
Modifier and TypeMethodDescriptionCompose this view with another, concatenating their outputs.Compose with a separator between views.static <S> View<S>Combine multiple views into one.static <S> View<S>empty()Empty view - renders nothing.static <S> View<S>Create a view from a function.static <S> View<S>newline()Newline view - renders a blank line.static <S> View<S>Literal view - always renders the same string.static <S> View<S>Conditional view with empty fallback.static <S> View<S>Conditional view - renders one of two views based on predicate.
-
Method Details
-
andThen
Compose this view with another, concatenating their outputs.- Parameters:
next- The view to append after this one- Returns:
- A new view that renders both in sequence
-
andThen
Compose with a separator between views.- Parameters:
separator- String to insert between viewsnext- The view to append- Returns:
- A new composed view
-
when
Conditional view - renders one of two views based on predicate.- Parameters:
condition- Predicate to test against stateifTrue- View to render if condition is trueifFalse- View to render if condition is false- Returns:
- A new conditional view
-
when
Conditional view with empty fallback.- Parameters:
condition- Predicate to test against stateview- View to render if condition is true- Returns:
- A new conditional view (empty if false)
-
empty
Empty view - renders nothing.- Returns:
- A view that returns empty string
-
of
Literal view - always renders the same string.- Parameters:
text- The literal string to render- Returns:
- A view that ignores state and returns the literal
-
newline
Newline view - renders a blank line.- Returns:
- A view that returns a newline
-
from
Create a view from a function.- Parameters:
fn- Function to convert to a View- Returns:
- The function as a View
-
compose
Combine multiple views into one.- Parameters:
views- Views to combine- Returns:
- A single view that renders all in sequence
-