예제 #1
0
파일: Gen.java 프로젝트: travisfw/javaslang
 /**
  * Maps generated Ts to Us.
  *
  * @param mapper A function that maps a generated T to a new generator which generates objects of
  *     type U.
  * @param <U> Type of generated objects of the new generator
  * @return A new generator
  */
 @SuppressWarnings("unchecked")
 @Override
 default <U> Gen<U> flatMap(
     Function<? super T, ? extends java.lang.Iterable<? extends U>> mapper) {
   Objects.requireNonNull(mapper, "mapper is null");
   return random -> {
     final Iterable<? extends U> iterable = mapper.apply(apply(random));
     if (iterable instanceof Gen) {
       return ((Gen<U>) iterable).apply(random);
     } else {
       return Value.get(iterable);
     }
   };
 }
예제 #2
0
 @SuppressWarnings("unchecked")
 default <U> Try<U> flatMapTry(
     CheckedFunction<? super T, ? extends java.lang.Iterable<? extends U>> mapper) {
   if (isFailure()) {
     return (Failure<U>) this;
   } else {
     try {
       final Iterable<? extends U> iterable = mapper.apply(get());
       if (iterable instanceof Value) {
         return ((Value<U>) iterable).toTry();
       } else {
         return Try.of(() -> Value.get(iterable));
       }
     } catch (Throwable t) {
       return new Failure<>(t);
     }
   }
 }