public <B> Promise<B> flatMap(Function<? super T, Promise<B>> function) { return delegate.flatMap(function); }
@Override public Iterable<AggregateRoot> getExistingRoots() { Promise<Result<List<CollapsedRoom>>> allRooms = hipChat.getAllRooms(); return allRooms.flatMap(new Function<Result<List<CollapsedRoom>>, Promise<Iterable<AggregateRoot>>>() { @Override public Promise<Iterable<AggregateRoot>> apply(@Nullable Result<List<CollapsedRoom>> input) { if (input != null && input.isSuccess()) { return Promises.promise(Iterables.transform(input.success(), ROOM_TO_AGGREGATE_FUNCTION)); } else { return Promises.<Iterable<AggregateRoot>>promise(ImmutableList.<AggregateRoot>of()); } } }).claim(); }
@Override public <B> Promise<B> flatMap(Function<? super A, ? extends Promise<? extends B>> function) { return delegate().flatMap(function); }
@Override public Promise<Unit> copyAttachment(final AttachmentKey sourceAttachmentKey, final AttachmentKey newAttachmentKey) { return getAttachment(sourceAttachmentKey, new Function<InputStream, TemporaryAttachmentId>() { @Override public TemporaryAttachmentId get(final InputStream input) { return localTemporaryFileStore.createTemporaryFile(input); } }).flatMap(new com.google.common.base.Function<TemporaryAttachmentId, Promise<Unit>>() { @Override public Promise<Unit> apply(final TemporaryAttachmentId temporaryFile) { return moveTemporaryToAttachment(temporaryFile, newAttachmentKey); } }); }
@Override public Promise<Map<String, Result<ExpandedRoom>>> expandRoomsForIds(final Iterable<String> roomIds) { return client.list().flatMap( new Function<Result<List<Room>>, Promise<Map<String, Result<ExpandedRoom>>>>() { @Override public Promise<Map<String, Result<ExpandedRoom>>> apply(Result<List<Room>> response) { final ImmutableMap.Builder<String, Result<ExpandedRoom>> builder = new ImmutableMap.Builder<String, Result<ExpandedRoom>>(); if (response != null && response.isSuccess()) { final List<Room> rooms = response.success(); final Collection<Room> filteredCollection = Collections2.filter(rooms, new Predicate<Room>() { @Override public boolean apply(Room room) { return Iterables.contains(roomIds, String.valueOf(room.getId())); } }); for (Room room : filteredCollection) { builder.put(String.valueOf(room.getId()), Result.<ExpandedRoom>success(transform(room))); } } return Promises.promise((Map<String, Result<ExpandedRoom>>) builder.build()); } }); }
@Override public Promise<? extends StoreAttachmentResult> get(final TemporaryAttachmentId temporaryAttachmentId) { return moveTemporaryToAttachment(temporaryAttachmentId, attachmentKey).flatMap(Functions.toGoogleFunction(new Function<Unit, Promise<? extends StoreAttachmentResult>>() { @Override public Promise<? extends StoreAttachmentResult> get(final Unit input) { return Promises.promise(StoreAttachmentResult.created()); } })); } }));
private Promise<StoreAttachmentResult> putAttachment(final InputStream inputStream, final Long size, final AttachmentKey attachmentKey) { return putTemporaryAttachment(inputStream, size).flatMap(Functions.toGoogleFunction(new Function<TemporaryAttachmentId, Promise<? extends StoreAttachmentResult>>() { @Override public Promise<? extends StoreAttachmentResult> get(final TemporaryAttachmentId temporaryAttachmentId) { return moveTemporaryToAttachment(temporaryAttachmentId, attachmentKey).flatMap(Functions.toGoogleFunction(new Function<Unit, Promise<? extends StoreAttachmentResult>>() { @Override public Promise<? extends StoreAttachmentResult> get(final Unit input) { return Promises.promise(StoreAttachmentResult.created()); } })); } })); }
private Promise<Result<List<CollapsedRoom>>> getRooms(final HipChatAPI hipChatAPI, final int startIndex, final ImmutableList.Builder<CollapsedRoom> currentRooms, final boolean includePrivate) { final ImmutableList.Builder<Promise<Result<AllRoomsResult>>> builder = ImmutableList.builder(); for (int i = 0; i < BATCH_SIZE; i++) { builder.add(hipChatAPI.rooms() .getAllRooms() .startIndex(startIndex + i * PAGE_SIZE) .maxResults(PAGE_SIZE) .includePrivate(includePrivate) .build()); } return Promises.when(builder.build()) .flatMap(toRoomList(hipChatAPI, startIndex, currentRooms, includePrivate)); }
@Override public Promise<Result<ExpandedRoom>> createRoom(String name, HipChatUserId owner, Option<String> topic) { return client.create(name, Long.parseLong(owner.asEntityId()), Option.<Boolean>none(), topic, Option.option(false)) .flatMap(new Function<Result<Room>, Promise<Result<ExpandedRoom>>>() { @Override public Promise<Result<ExpandedRoom>> apply(Result<Room> input) { if (input.isSuccess()) { final ExpandedRoom expandedRoom = transform(input.success()); return Promises.promise(Result.success(expandedRoom)); } return Promises.promise(input.<ExpandedRoom>asError()); } }); }
@Override public Promise<Boolean> exists(final AttachmentKey attachmentKey) { return primaryStore.exists(attachmentKey).fold(new com.google.common.base.Function<Throwable, Promise<Boolean>>() { @Override public Promise<Boolean> apply(final Throwable ignored) { return secondaryStore.exists(attachmentKey); } }, new com.google.common.base.Function<Boolean, Promise<Boolean>>() { @Override public Promise<Boolean> apply(final Boolean exists) { if (exists) { return Promises.promise(true); } else { return secondaryStore.exists(attachmentKey); } } }).flatMap(Functions.<Promise<Boolean>>identity()); }
return thumbnailAccessor.deleteThumbnailDirectory(issue).flatMap(new Function<Void, Promise<Void>>()
return Promises.promise(input); }).flatMap(com.google.common.base.Functions.<Promise<V>>identity());
private Promise<Map<String, Result<ExpandedRoom>>> expandRoomsForIds(final HipChatAPI hipChatAPI, final Iterable<String> roomIds, final ImmutableMap.Builder<String, Result<ExpandedRoom>> results) { final ImmutableList.Builder<Promise<RoomResult>> builder = ImmutableList .builder(); final boolean usingPersonalToken = hipChatAPI.getTokenType() instanceof HipChatAPI.TokenType.PersonalToken; //Only schedule at most BATCH_SIZE requests for (final String roomId : Iterables.take(BATCH_SIZE, roomIds)) { Promise<Result<ExpandedRoom>> roomPromise = hipChatAPI.rooms() .getRoom(roomId); if (!usingPersonalToken) { roomPromise = roomPromise.map(FILTER_PRIVATE_ROOM); } builder.add(roomPromise.map(toRoomResult(roomId))); } return Promises.when(builder.build()).flatMap(expandRoomBatch(hipChatAPI, roomIds, results)); }
@Override public Promise<StoreAttachmentResult> putAttachment(final StoreAttachmentBean storeAttachmentBean) { //noinspection NullableProblems return primaryAttachmentStore.putTemporaryAttachment(storeAttachmentBean.getStream(), storeAttachmentBean.getSize()) .flatMap(new com.google.common.base.Function<TemporaryAttachmentId, Promise<Unit>>() { @Override public Promise<Unit> apply(final TemporaryAttachmentId temporaryAttachmentId) { return moveTemporaryToAttachment(temporaryAttachmentId, storeAttachmentBean.getAttachmentKey()); } }) .map(Functions.constant(StoreAttachmentResult.created())); }
@Override public Promise<Unit> copyAttachment(final AttachmentKey sourceAttachmentKey, final AttachmentKey newAttachmentKey) { final String sourceId = sourceAttachmentKey.getAttachmentId().toString(); final String newId = newAttachmentKey.getAttachmentId().toString(); final Function<Failure, Promise<Unit>> reject = reject(new Function<Failure, Throwable>() { @Override public Throwable apply(final Failure failure) { return new AttachmentRuntimeException(failure.message()); } }); final Function<Either<Failure, PutResult>, Promise<Unit>> fold = fold(reject, toUnitIgnoreResult()); return getBlobStore().copy(sourceId, newId).flatMap(fold); }
@Nonnull @Override public Promise<Boolean> exists(final AttachmentKey attachmentKey) { final String attachmentId = attachmentKey.getAttachmentId().toString(); final Function<Failure, Promise<Boolean>> failure = reject(new Function<Failure, Throwable>() { @Override public Throwable apply(final Failure failure) { return new AttachmentReadException( "Remote blobstore couldn't provide an input stream for attachment " + attachmentId + ": " + failure.message()); } }); final Function<Option<HeadResult>, Promise<Boolean>> result = promise(new Function<Option<HeadResult>, Boolean>() { @Override public Boolean apply(final Option<HeadResult> headResult) { return headResult.isDefined(); } }); final Function<Either<Failure, Option<HeadResult>>, Promise<Boolean>> fold = fold(failure, result); return getBlobStore() .head(attachmentId, FOREVER_CACHE) .flatMap(fold); }
return getBlobStore().delete(id).flatMap(fold);
private Promise<Unit> moveBlob(final String oldId, final String newId) { final Function<Failure, Promise<Unit>> failure = reject(new Function<Failure, AttachmentMoveException>() { @Override public AttachmentMoveException apply(final Failure failure) { return new AttachmentMoveException(failure.message()); } }); final Function<Either<Failure, PutResult>, Promise<Unit>> fold = fold(failure, toUnitIgnoreResult()); return getBlobStore() .move(oldId, newId) .flatMap(fold); }
return getBlobStore() .get(attachmentId, FOREVER_CACHE, Options.lift(compose(toGoogleFunction(attachmentGetDataProcessor), dataFromResult))) .flatMap(fold);
return getBlobStore() .put(id, dataStream, size) .flatMap(fold);