Example usage for com.google.common.util.concurrent Futures transformAsync

List of usage examples for com.google.common.util.concurrent Futures transformAsync

Introduction

In this page you can find the example usage for com.google.common.util.concurrent Futures transformAsync.

Prototype

public static <I, O> ListenableFuture<O> transformAsync(ListenableFuture<I> input,
        AsyncFunction<? super I, ? extends O> function) 

Source Link

Document

Returns a new ListenableFuture whose result is asynchronously derived from the result of the given Future .

Usage

From source file:org.thingsboard.rule.engine.util.EntitiesRelatedDeviceIdAsyncLoader.java

public static ListenableFuture<DeviceId> findDeviceAsync(TbContext ctx, EntityId originator,
        DeviceRelationsQuery deviceRelationsQuery) {
    DeviceService deviceService = ctx.getDeviceService();
    DeviceSearchQuery query = buildQuery(originator, deviceRelationsQuery);

    ListenableFuture<List<Device>> asyncDevices = deviceService.findDevicesByQuery(ctx.getTenantId(), query);

    return Futures.transformAsync(asyncDevices,
            d -> CollectionUtils.isNotEmpty(d) ? Futures.immediateFuture(d.get(0).getId())
                    : Futures.immediateFuture(null));
}

From source file:org.thingsboard.rule.engine.util.EntitiesRelatedEntityIdAsyncLoader.java

public static ListenableFuture<EntityId> findEntityAsync(TbContext ctx, EntityId originator,
        RelationsQuery relationsQuery) {
    RelationService relationService = ctx.getRelationService();
    EntityRelationsQuery query = buildQuery(originator, relationsQuery);
    ListenableFuture<List<EntityRelation>> asyncRelation = relationService.findByQuery(ctx.getTenantId(),
            query);// w w w.  j  a  v  a 2 s  . c o m
    if (relationsQuery.getDirection() == EntitySearchDirection.FROM) {
        return Futures.transformAsync(asyncRelation,
                r -> CollectionUtils.isNotEmpty(r) ? Futures.immediateFuture(r.get(0).getTo())
                        : Futures.immediateFuture(null));
    } else if (relationsQuery.getDirection() == EntitySearchDirection.TO) {
        return Futures.transformAsync(asyncRelation,
                r -> CollectionUtils.isNotEmpty(r) ? Futures.immediateFuture(r.get(0).getFrom())
                        : Futures.immediateFuture(null));
    }
    return Futures.immediateFailedFuture(new IllegalStateException("Unknown direction"));
}

From source file:org.thingsboard.rule.engine.util.EntitiesCustomerIdAsyncLoader.java

private static <T extends HasCustomerId> ListenableFuture<CustomerId> getCustomerAsync(
        ListenableFuture<T> future) {
    return Futures.transformAsync(future,
            in -> in != null ? Futures.immediateFuture(in.getCustomerId()) : Futures.immediateFuture(null));
}

From source file:com.google.gapid.image.FetchedImage.java

public static ListenableFuture<FetchedImage> load(Client client, ListenableFuture<Path.ImageInfo> imageInfo) {
    return Futures.transformAsync(imageInfo, imageInfoPath -> load(client, imageInfoPath));
}

From source file:com.google.gapid.image.FetchedImage.java

public static ListenableFuture<FetchedImage> load(Client client, Path.ImageInfo imagePath) {
    return Futures.transformAsync(client.get(imageInfo(imagePath)), value -> {
        Images.Format format = getFormat(value.getImageInfo2D());
        return Futures.transform(client.get(imageData(imagePath, format.format)),
                pixelValue -> new FetchedImage(client, format, pixelValue.getImageInfo2D()));
    });/*from w  ww  . j a va 2  s  . c om*/
}

From source file:org.thingsboard.rule.engine.util.EntitiesTenantIdAsyncLoader.java

private static <T extends HasTenantId> ListenableFuture<TenantId> getTenantAsync(ListenableFuture<T> future) {
    return Futures.transformAsync(future, in -> {
        return in != null ? Futures.immediateFuture(in.getTenantId()) : Futures.immediateFuture(null);
    });/*www .  j ava 2 s .  c o m*/
}

From source file:com.google.gapid.image.FetchedImage.java

public static ListenableFuture<FetchedImage> load(Client client, Path.ResourceData imagePath) {
    return Futures.transformAsync(client.get(resourceInfo(imagePath)), value -> {
        switch (value.getValCase()) {
        case TEXTURE_2D:
            return load(client, imagePath, getFormat(value.getTexture2D()));
        case CUBEMAP:
            return load(client, imagePath, getFormat(value.getCubemap()));
        default:/*from   w  ww .ja va 2  s.  co m*/
            throw new UnsupportedOperationException("Unexpected resource type: " + value);
        }
    });
}

From source file:com.orangerhymelabs.helenus.cassandra.table.ViewService.java

public ListenableFuture<View> create(View view) {
    ListenableFuture<Boolean> tableFuture = tables.exists(view.databaseName(), view.tableName());
    return Futures.transformAsync(tableFuture, new AsyncFunction<Boolean, View>() {
        @Override//from   ww w  .ja  v a  2s . com
        public ListenableFuture<View> apply(Boolean exists) throws Exception {
            if (exists) {
                try {
                    ValidationEngine.validateAndThrow(view);
                    return views.create(view);
                } catch (ValidationException e) {
                    return Futures.immediateFailedFuture(e);
                }
            } else {
                return Futures.immediateFailedFuture(
                        new ItemNotFoundException("Table not found: " + view.tableName()));
            }
        }
    });
}

From source file:com.google.gapid.util.FutureCache.java

public ListenableFuture<V> get(K key) {
    // Look up the value in the cache using the executor.
    ListenableFuture<V> cacheLookUp = EXECUTOR.submit(() -> cache.getIfPresent(key));
    return Futures.transformAsync(cacheLookUp, fromCache -> {
        if (fromCache != null) {
            return Futures.immediateFuture(fromCache);
        }/*  w  ww. j  av a  2  s  .  c  o  m*/

        return Futures.transform(fetcher.apply(key), value -> {
            if (shouldCache.test(value)) {
                cache.put(key, value);
            }
            return value;
        });
    });
}

From source file:org.thingsboard.rule.engine.util.EntitiesFieldsAsyncLoader.java

private static <T extends BaseData> ListenableFuture<EntityFieldsData> getAsync(ListenableFuture<T> future,
        Function<T, EntityFieldsData> converter) {
    return Futures.transformAsync(future, in -> in != null ? Futures.immediateFuture(converter.apply(in))
            : Futures.immediateFailedFuture(new RuntimeException("Entity not found!")));
}