@Test
  public void forEachWithErrorsAsync() {

    List<Integer> list = new ArrayList<>();
    assertThat(error, nullValue());
    LazyFutureStream<Integer> stream =
        LazyFutureStream.of(
                () -> 1,
                () -> 2,
                () -> 3,
                (Supplier<Integer>)
                    () -> {
                      throw new RuntimeException();
                    },
                () -> 4,
                () -> 5)
            .async()
            .map(Supplier::get);
    stream.forEachWithError(i -> list.add(i), e -> error = e);

    ExceptionSoftener.softenRunnable(() -> Thread.sleep(100)).run();
    assertThat(list, hasItems(1, 2, 3, 4, 5));
    assertThat(list.size(), equalTo(5));

    assertThat(list, hasItems(1, 2, 3, 4, 5));
    assertThat(list.size(), equalTo(5));

    assertThat(error, instanceOf(RuntimeException.class));
  }
 default SoftenedCacheable<OUT> soften() {
   return (key, fn) -> {
     try {
       return computeIfAbsent(key, fn);
     } catch (Throwable t) {
       throw ExceptionSoftener.throwSoftenedException(t);
     }
   };
 }
Beispiel #3
0
  public static <T> T convertFromJson(final String jsonString, final JavaType type) {
    try {

      return getMapper().readValue(jsonString, type);

    } catch (final Exception ex) {
      ExceptionSoftener.throwSoftenedException(ex);
    }
    return null;
  }
Beispiel #4
0
 public static String serializeToJson(final Object data) {
   String jsonString = "";
   if (data == null) return jsonString;
   try {
     jsonString = getMapper().writeValueAsString(data);
   } catch (final Exception ex) {
     ExceptionSoftener.throwSoftenedException(ex);
   }
   return jsonString;
 }