Ejemplo n.º 1
0
  static <T> T unwrapOtherMonadTypes(Comprehender<T> comp, Object apply) {

    if (comp.instanceOfT(apply)) return (T) apply;

    if (apply instanceof Optional) {
      if (((Optional) apply).isPresent()) return comp.of(((Optional) apply).get());
      return comp.empty();
    }

    if (apply instanceof Stream) {
      return comp.of(((Stream) apply).collect(Collectors.toCollection(MaterializedList::new)));
    }

    if (apply instanceof IntStream) {
      return comp.of(
          ((IntStream) apply).boxed().collect(Collectors.toCollection(MaterializedList::new)));
    }
    if (apply instanceof DoubleStream) {
      return comp.of(
          ((DoubleStream) apply).boxed().collect(Collectors.toCollection(MaterializedList::new)));
    }
    if (apply instanceof LongStream) {
      return comp.of(
          ((LongStream) apply).boxed().collect(Collectors.toCollection(MaterializedList::new)));
    }
    if (apply instanceof CompletableFuture) {
      return comp.of(((CompletableFuture) apply).join());
    }
    if (apply instanceof StreamT) return comp.of(((StreamT) apply).unwrap());

    return (T)
        new ComprehenderSelector()
            .selectComprehender(apply)
            .resolveForCrossTypeFlatMap(comp, apply);
  }