rx.internal.operators.SingleOnErrorReturn Maven / Gradle / Ivy
The newest version!
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.internal.operators;
import rx.*;
import rx.Single.OnSubscribe;
import rx.exceptions.Exceptions;
import rx.functions.Func1;
/**
* Signal a value returned by a resumeFunction when the source signals a Throwable.
*
* @param the value type
*/
public final class SingleOnErrorReturn implements Single.OnSubscribe {
final Single.OnSubscribe source;
final Func1 resumeFunction;
public SingleOnErrorReturn(OnSubscribe source, Func1 resumeFunction) {
this.source = source;
this.resumeFunction = resumeFunction;
}
@Override
public void call(SingleSubscriber t) {
OnErrorReturnsSingleSubscriber parent = new OnErrorReturnsSingleSubscriber(t, resumeFunction);
t.add(parent);
source.call(parent);
}
static final class OnErrorReturnsSingleSubscriber extends SingleSubscriber {
final SingleSubscriber actual;
final Func1 resumeFunction;
public OnErrorReturnsSingleSubscriber(SingleSubscriber actual,
Func1 resumeFunction) {
this.actual = actual;
this.resumeFunction = resumeFunction;
}
@Override
public void onSuccess(T value) {
actual.onSuccess(value);
}
@Override
public void onError(Throwable error) {
T v;
try {
v = resumeFunction.call(error);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
actual.onError(ex);
return;
}
actual.onSuccess(v);
}
}
}