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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations