/** * Deprecated. * <p> * Use {@link #replace(Promise)}. * * @param next the promise to replace {@code this} with * @param <O> the type of value provided by the replacement promise * @return a promise * @deprecated replaced by {@link #replace(Promise)} as of 1.1.0 */ @Deprecated default <O> Promise<O> next(Promise<O> next) { return flatMap(in -> next); }
return flatMap(in -> next);
default <T> Promise<T> flatMap(Promise<T> promise) { return promise().flatMap(n -> promise); }
/** * Like {@link #map(Function)}, but performs the transformation on a blocking thread. * <p> * This is simply a more convenient form of using {@link Blocking#get(Factory)} and {@link #flatMap(Function)}. * * @param transformer the transformation to apply to the promised value, on a blocking thread * @param <O> the type of the transformed object * @return a promise for the transformed value */ default <O> Promise<O> blockingMap(Function<? super T, ? extends O> transformer) { return flatMap(t -> Blocking.get(() -> transformer.apply(t))); }
return flatMap(Function.when(predicate, onTrue, onFalse));
default <T> Promise<T> flatMap(Factory<? extends Promise<T>> factory) { return promise().flatMap(n -> factory.create()); }
@Override public Promise<List<Meeting>> getMeetings() { return meetingRepository.getMeetings() .flatMap(meetings -> Promise.value( meetings.stream() .peek( meeting -> ratingRepository.getAverageRating(meeting.getId()) .then(meeting::setRating) ) .collect(Collectors.toList())) ); }
/** * Executes the given action with the promise value, on a blocking thread. * <p> * Similar to {@link #blockingMap(Function)}, but does not provide a new value. * This can be used to do something with the value, without terminating the promise. * * @param action the action to to perform with the value, on a blocking thread * @return a promise for the same value given to the action */ default Promise<T> blockingOp(Action<? super T> action) { return flatMap(t -> Blocking.op(action.curry(t)).map(() -> t)); }
private <T> void doRender(T toRender, Context context) throws Exception { Class<T> type = Types.cast(toRender.getClass()); Iterator<? extends RenderableDecorator<?>> decorators = Iterables.filter( context.getAll(RENDERABLE_DECORATOR_TYPE), d -> d.getType().isAssignableFrom(type) ).iterator(); if (decorators.hasNext()) { Promise<T> promise = Promise.value(toRender); while (decorators.hasNext()) { RenderableDecorator<T> cast = Types.cast(decorators.next()); promise = promise.flatMap(r -> cast.decorate(context, r)); } promise.then(r -> doRender(context, type, r)); } else { doRender(context, type, toRender); } }
/** * Transforms the promised value to a {@link Pair}, with the value of the result of the given function as the {@code right}. * <p> * The function is called with the promised value. * The existing promised value will become the {@code left}. * * @param rightFunction a function that produces a promise for the right value from the promised value * @param <O> the type of the left value * @return a promise * @since 1.4 */ default <O> Promise<Pair<T, O>> flatRight(Function<? super T, ? extends Promise<O>> rightFunction) { return flatMap(left -> rightFunction.apply(left) .map(right -> Pair.of(left, right) ) ); }
/** * Transforms the promised value to a {@link Pair}, with the value of the result of the given function as the {@code left}. * <p> * The function is called with the promised value. * The existing promised value will become the {@code right}. * * @param leftFunction a function that produces a promise for the left value from the promised value * @param <O> the type of the left value * @return a promise * @since 1.4 */ default <O> Promise<Pair<O, T>> flatLeft(Function<? super T, ? extends Promise<O>> leftFunction) { return flatMap(right -> leftFunction.apply(right) .map(left -> Pair.of(left, right) ) ); }
private static <C extends Credentials, U extends UserProfile> Promise<Optional<U>> performDirectAuthentication(Context ctx, Class<? extends Client<C, U>> clientType) { return RatpackWebContext.from(ctx, false).flatMap(webContext -> Blocking.get(() -> { Clients clients = ctx.get(Clients.class); Client<C, U> client = clients.findClient(clientType); return userProfileFromCredentials(client, webContext); }) ); }
/** * Writes the given bytes to the given file, starting at the given position. * Use {@link #open(Path, Set, FileAttribute[])} to create a file promise. * <p> * The file channel is closed on success or failure. * * @param bytes the bytes to write * @param position the position in the file to start writing * @param file the file to write to * @return a write operation */ public static Operation write(ByteBuf bytes, long position, Promise<? extends AsynchronousFileChannel> file) { return file.flatMap(channel -> Promise.async(down -> channel.write(bytes.nioBuffer(), position, null, new CompletionHandler<Integer, Void>() { @Override public void completed(Integer result, Void attachment) { bytes.readerIndex(bytes.readerIndex() + result); down.success(null); } @Override public void failed(Throwable exc, Void attachment) { down.error(exc); } }) ) .close(bytes::release) .close(Blocking.op(((AsynchronousFileChannel) channel)::close)) ).operation(); }
/** Writes the bytes of the given publisher to the given file, returning the number of bytes written. * <p> * Use {@link #open(Path, Set, FileAttribute[])} to create a file promise. * <p> * The file channel is closed on success or failure. * <p> * As file system writes are expensive, * you may want to consider using {@link ByteBufStreams#buffer(Publisher, long, int, ByteBufAllocator)} * to “buffer” the data in memory before writing to disk. * * @param publisher the bytes to write * @param position the position in the file to start writing (must be >= 0) * @param file a promise for the file to write to * @return a promise for the number of bytes written */ public static Promise<Long> write(Publisher<? extends ByteBuf> publisher, long position, Promise<? extends AsynchronousFileChannel> file) { return file.flatMap(fileChannel -> Promise.<Long>async(down -> publisher.subscribe(new FileWritingSubscriber(fileChannel, position, down)) ) .close(Blocking.op(((AsynchronousFileChannel) fileChannel)::close)) ); }
public static Promise<RatpackWebContext> from(Context ctx, boolean bodyBacked) { Promise<SessionData> sessionDataPromise = ctx.get(Session.class).getData(); if (bodyBacked) { return ctx.getRequest().getBody().flatMap(body -> sessionDataPromise.map(sessionData -> new RatpackWebContext(ctx, body, sessionData)) ); } else { return sessionDataPromise.map(sessionData -> new RatpackWebContext(ctx, null, sessionData)); } }
if (isDirect(clientType)) { return userProfile(ctx) .flatMap(p -> { if (p.isPresent()) { Optional<U> cast = Types.cast(p);
public static void main(String... args) throws Exception { Db db = new Db(); RatpackServer.start(server -> server .registryOf(rs -> rs.add(db)) .handlers(chain -> chain .all(ctx -> { LOGGER.info("All"); ctx.next(); }) .get("callout", ctx -> { Db database = ctx.get(Db.class); Promise<String> response = ctx.get(HttpClient.class) .get(new URI("http://wiremock:7070/name")) .map(r -> r.getBody().getText()); Promise<String> withDb = response.flatMap(database::getRecord); ctx.render(withDb); }) .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!")) )); } }