Services.Callback

An example demonstrating the use of the callback interface to wrap around a service for the purpose of localized logging.

  static void wrapping () {

    final StringBuilder out =
      new StringBuilder ();

    final Callback< Signal > callback =
      ( orientation, signal ) ->
        out.append (
          format (
            "%s %s%n",
            orientation,
            signal
          )
        );

    final Service service =
      Services
        .service (
          Services
            .context ()
            .service (
              Services.name (
                "service.one"
              )
            ),
          callback
        );

    service.call ();

    assert
      format (
        "%s %s%n",
        EMIT, CALL
      ).equals (
        out.toString ()
      );

    out.setLength (
      CLEAR
    );

    Services
      .execute (
        service,
        () -> {
        }
      );

    assert
      format (
        "%s %s%n" +
          "%s %s%n" +
          "%s %s%n",
        EMIT, START,
        EMIT, SUCCEED,
        EMIT, STOP
      ).equals (
        out.toString ()
      );

  }