Search in sources :

Example 1 with Failure

use of org.osgi.util.promise.Failure 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 2 with Failure

use of org.osgi.util.promise.Failure 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)

Example 3 with Failure

use of org.osgi.util.promise.Failure 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 4 with Failure

use of org.osgi.util.promise.Failure 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 5 with Failure

use of org.osgi.util.promise.Failure 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)

Aggregations

Failure (org.osgi.util.promise.Failure)14 Promise (org.osgi.util.promise.Promise)11 TimeoutException (org.osgi.util.promise.TimeoutException)10 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 NoSuchElementException (java.util.NoSuchElementException)8 Deferred (org.osgi.util.promise.Deferred)3 Archive (aQute.maven.api.Archive)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Success (org.osgi.util.promise.Success)2 BundleDescriptor (aQute.bnd.repository.maven.provider.IndexFile.BundleDescriptor)1 TaggedData (aQute.bnd.service.url.TaggedData)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 List (java.util.List)1 Entry (java.util.Map.Entry)1