Interface TypeSwitch<T>

Type Parameters:
T - the super type to switch over.
All Known Implementing Classes:
TypeSwitch.Default, TypeSwitch.Never

public interface TypeSwitch<T>
Util to enable switching over types.

Example:

 
 Mqtt5MessageException e;
 TypeSwitch.when(e)
         .is(Mqtt5ConnAckException.class, c -> System.out.println(c.getMqttMessage().getReasonCode()))
         .is(Mqtt5DisconnectException.class, d -> System.out.println(d.getMqttMessage().getServerReference()));
 
 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
     
    static class 
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <I extends T>
    @NotNull TypeSwitch<T>
    is(@NotNull Class<I> type, @NotNull Consumer<I> consumer)
    Checks if the object that is switched over is of a given type and if so executes a callback.
    static <T> @NotNull TypeSwitch<T>
    Returns a TypeSwitch object which does not match any type.
    static <T> @NotNull TypeSwitch<T>
    when(T t)
    Returns a TypeSwitch object for switching over an object of type T.
  • Method Details

    • never

      @NotNull static <T> @NotNull TypeSwitch<T> never()
      Returns a TypeSwitch object which does not match any type.
      Type Parameters:
      T - the super type to switch over.
      Returns:
      the TypeSwitch object.
    • when

      @NotNull static <T> @NotNull TypeSwitch<T> when(@Nullable T t)
      Returns a TypeSwitch object for switching over an object of type T.
      Type Parameters:
      T - the super type to switch over.
      Parameters:
      t - the object of type T.
      Returns:
      the TypeSwitch object.
    • is

      @NotNull <I extends T> @NotNull TypeSwitch<T> is(@NotNull @NotNull Class<I> type, @NotNull @NotNull Consumer<I> consumer)
      Checks if the object that is switched over is of a given type and if so executes a callback.

      If the type matches the returned TypeSwitch will not match any further type.

      Type Parameters:
      I - the type to check
      Parameters:
      type - the class of the type to check.
      consumer - the callback to execute if the type matches.
      Returns:
      the TypeSwitch object.