Interface View<S>

Type Parameters:
S - The state type this view renders
All Superinterfaces:
Function<S,String>
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface View<S> extends Function<S,String>
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 Type
    Method
    Description
    default View<S>
    andThen(View<S> next)
    Compose this view with another, concatenating their outputs.
    default View<S>
    andThen(String separator, View<S> next)
    Compose with a separator between views.
    static <S> View<S>
    compose(View<S>... views)
    Combine multiple views into one.
    static <S> View<S>
    Empty view - renders nothing.
    static <S> View<S>
    Create a view from a function.
    static <S> View<S>
    Newline view - renders a blank line.
    static <S> View<S>
    of(String text)
    Literal view - always renders the same string.
    static <S> View<S>
    when(Predicate<S> condition, View<S> view)
    Conditional view with empty fallback.
    static <S> View<S>
    when(Predicate<S> condition, View<S> ifTrue, View<S> ifFalse)
    Conditional view - renders one of two views based on predicate.

    Methods inherited from interface java.util.function.Function

    andThen, apply, compose
  • Method Details

    • andThen

      default View<S> andThen(View<S> next)
      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

      default View<S> andThen(String separator, View<S> next)
      Compose with a separator between views.
      Parameters:
      separator - String to insert between views
      next - The view to append
      Returns:
      A new composed view
    • when

      static <S> View<S> when(Predicate<S> condition, View<S> ifTrue, View<S> ifFalse)
      Conditional view - renders one of two views based on predicate.
      Parameters:
      condition - Predicate to test against state
      ifTrue - View to render if condition is true
      ifFalse - View to render if condition is false
      Returns:
      A new conditional view
    • when

      static <S> View<S> when(Predicate<S> condition, View<S> view)
      Conditional view with empty fallback.
      Parameters:
      condition - Predicate to test against state
      view - View to render if condition is true
      Returns:
      A new conditional view (empty if false)
    • empty

      static <S> View<S> empty()
      Empty view - renders nothing.
      Returns:
      A view that returns empty string
    • of

      static <S> View<S> of(String text)
      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

      static <S> View<S> newline()
      Newline view - renders a blank line.
      Returns:
      A view that returns a newline
    • from

      static <S> View<S> from(Function<S,String> fn)
      Create a view from a function.
      Parameters:
      fn - Function to convert to a View
      Returns:
      The function as a View
    • compose

      @SafeVarargs static <S> View<S> compose(View<S>... views)
      Combine multiple views into one.
      Parameters:
      views - Views to combine
      Returns:
      A single view that renders all in sequence