001/*******************************************************************************
002 * Copyright (c) 2017 Red Hat Inc
003 * All rights reserved. This program and the accompanying materials
004 * are made available under the terms of the Eclipse Public License v1.0
005 * which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v10.html
007 *
008 * Contributors:
009 *     Jens Reimann - initial API and implementation
010 *******************************************************************************/
011package de.dentrassi.varlink.spi;
012
013import java.util.Objects;
014import java.util.concurrent.CompletableFuture;
015
016import io.netty.util.concurrent.Future;
017
018public final class Futures {
019    private Futures() {
020    }
021
022    public static <T> CompletableFuture<T> handle(final Future<T> future) {
023        Objects.requireNonNull(future);
024
025        final CompletableFuture<T> result = new CompletableFuture<>();
026
027        future.addListener(r -> {
028            try {
029                result.complete(future.get());
030            } catch (final Throwable e) {
031                result.completeExceptionally(e);
032            }
033        });
034
035        return result;
036    }
037}