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); }
/** * Answers the question how should this type behave when returned in a flatMap function by another * type? For example - Optional uses comp.of(opt.get()) when a value is present and comp.empty() * when no value is present. * * @param comp * @param apply * @return */ default Object resolveForCrossTypeFlatMap(Comprehender comp, T apply) { return comp.of(apply); }
static <T> T unwrapOtherMonadTypes(Comprehender<T> comp, Object apply) { if (apply instanceof Collection) { return (T) ((Collection) apply).stream(); } return Comprehender.unwrapOtherMonadTypes(comp, apply); }