Search in sources :

Example 6 with Promise

use of org.osgi.util.promise.Promise in project aries by apache.

the class PromiseImpl method map.

@Override
public <R> Promise<R> map(final Function<? super T, ? extends R> mapper) {
    if (mapper == null)
        throw new NullPointerException();
    final PromiseImpl<R> result = new PromiseImpl<R>(exec, ses);
    then(new Success<T, T>() {

        @Override
        public Promise<T> call(Promise<T> resolved) throws Exception {
            try {
                R val = mapper.apply(resolved.getValue());
                result.resolve(val);
            } catch (Throwable t) {
                result.fail(t);
            }
            return null;
        }
    }, new Failure() {

        @Override
        public void fail(Promise<?> resolved) throws Exception {
            result.fail(resolved.getFailure());
        }
    });
    return result;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NoSuchElementException(java.util.NoSuchElementException) TimeoutException(org.osgi.util.promise.TimeoutException) Promise(org.osgi.util.promise.Promise) Failure(org.osgi.util.promise.Failure)

Example 7 with Promise

use of org.osgi.util.promise.Promise in project aries by apache.

the class PromiseImpl method recoverWith.

@Override
public Promise<T> recoverWith(final Function<Promise<?>, Promise<? extends T>> recovery) {
    if (recovery == null)
        throw new NullPointerException();
    final PromiseImpl<T> result = new PromiseImpl<T>(exec, ses);
    then(new Success<T, T>() {

        @Override
        public Promise<T> call(Promise<T> resolved) throws Exception {
            result.resolve(resolved.getValue());
            return null;
        }
    }, new Failure() {

        @Override
        public void fail(Promise<?> resolved) throws Exception {
            try {
                Promise<? extends T> recover = recovery.apply(resolved);
                if (recover != null) {
                    result.resolveWith(recover);
                } else {
                    result.fail(resolved.getFailure());
                }
            } catch (Throwable t) {
                result.fail(t);
            }
        }
    });
    return result;
}
Also used : Promise(org.osgi.util.promise.Promise) InvocationTargetException(java.lang.reflect.InvocationTargetException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NoSuchElementException(java.util.NoSuchElementException) TimeoutException(org.osgi.util.promise.TimeoutException) Failure(org.osgi.util.promise.Failure)

Example 8 with Promise

use of org.osgi.util.promise.Promise in project aries by apache.

the class PromiseImpl method fallbackTo.

@Override
public Promise<T> fallbackTo(final Promise<? extends T> fallback) {
    if (fallback == null)
        throw new NullPointerException();
    final PromiseImpl<T> result = new PromiseImpl<T>(exec, ses);
    then(new Success<T, T>() {

        @Override
        public Promise<T> call(Promise<T> resolved) throws Exception {
            result.resolve(resolved.getValue());
            return null;
        }
    }, new Failure() {

        @Override
        public void fail(Promise<?> resolved) throws Exception {
            @SuppressWarnings({ "not thrown", "all" }) Throwable fail = fallback.getFailure();
            if (fail != null) {
                result.fail(resolved.getFailure());
            } else {
                result.resolve(fallback.getValue());
            }
        }
    });
    return result;
}
Also used : Promise(org.osgi.util.promise.Promise) InvocationTargetException(java.lang.reflect.InvocationTargetException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NoSuchElementException(java.util.NoSuchElementException) TimeoutException(org.osgi.util.promise.TimeoutException) Failure(org.osgi.util.promise.Failure)

Example 9 with Promise

use of org.osgi.util.promise.Promise in project aries by apache.

the class PromiseImpl method filter.

@Override
public Promise<T> filter(final Predicate<? super T> predicate) {
    if (predicate == null)
        throw new NullPointerException();
    final PromiseImpl<T> result = new PromiseImpl<T>(exec, ses);
    then(new Success<T, T>() {

        @Override
        public Promise<T> call(Promise<T> resolved) throws Exception {
            try {
                if (predicate.test(resolved.getValue())) {
                    result.resolve(resolved.getValue());
                } else {
                    result.fail(new NoSuchElementException("predicate does not accept value"));
                }
            } catch (Throwable t) {
                result.fail(t);
            }
            return null;
        }
    }, new Failure() {

        @Override
        public void fail(Promise<?> resolved) throws Exception {
            result.fail(resolved.getFailure());
        }
    });
    return result;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NoSuchElementException(java.util.NoSuchElementException) TimeoutException(org.osgi.util.promise.TimeoutException) Promise(org.osgi.util.promise.Promise) NoSuchElementException(java.util.NoSuchElementException) Failure(org.osgi.util.promise.Failure)

Example 10 with Promise

use of org.osgi.util.promise.Promise in project aries by apache.

the class PromiseImpl method flatMap.

@Override
public <R> Promise<R> flatMap(final Function<? super T, Promise<? extends R>> mapper) {
    if (mapper == null)
        throw new NullPointerException();
    final PromiseImpl<R> result = new PromiseImpl<R>(exec, ses);
    then(new Success<T, T>() {

        @Override
        public Promise<T> call(Promise<T> resolved) throws Exception {
            try {
                Promise<? extends R> p = mapper.apply(resolved.getValue());
                result.resolveWith(p);
            } catch (Throwable t) {
                result.fail(t);
            }
            return null;
        }
    }, new Failure() {

        @Override
        public void fail(Promise<?> resolved) throws Exception {
            result.fail(resolved.getFailure());
        }
    });
    return result;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NoSuchElementException(java.util.NoSuchElementException) TimeoutException(org.osgi.util.promise.TimeoutException) Promise(org.osgi.util.promise.Promise) Failure(org.osgi.util.promise.Failure)

Aggregations

Promise (org.osgi.util.promise.Promise)21 Failure (org.osgi.util.promise.Failure)11 ArrayList (java.util.ArrayList)9 TimeoutException (org.osgi.util.promise.TimeoutException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 List (java.util.List)8 NoSuchElementException (java.util.NoSuchElementException)8 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)8 File (java.io.File)4 URI (java.net.URI)4 Deferred (org.osgi.util.promise.Deferred)4 ResourcesRepository (aQute.bnd.osgi.repository.ResourcesRepository)3 Resource (org.osgi.resource.Resource)3 BridgeRepository (aQute.bnd.osgi.repository.BridgeRepository)2 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)2 DownloadListenerPromise (aQute.bnd.util.repository.DownloadListenerPromise)2 PromiseCollectors.toPromise (aQute.lib.promise.PromiseCollectors.toPromise)2 Reporter (aQute.service.reporter.Reporter)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2