|
| 1 | +/** |
| 2 | + * Copyright 2016 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 | + * compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 | + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 | + * the License for the specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package io.reactivex.internal.operators.maybe; |
| 15 | + |
| 16 | +import java.util.concurrent.atomic.AtomicInteger; |
| 17 | + |
| 18 | +import io.reactivex.*; |
| 19 | +import io.reactivex.annotations.Experimental; |
| 20 | +import io.reactivex.disposables.Disposable; |
| 21 | +import io.reactivex.exceptions.Exceptions; |
| 22 | +import io.reactivex.functions.Action; |
| 23 | +import io.reactivex.internal.disposables.DisposableHelper; |
| 24 | +import io.reactivex.plugins.RxJavaPlugins; |
| 25 | + |
| 26 | +/** |
| 27 | + * Execute an action after an onSuccess, onError, onComplete or a dispose event. |
| 28 | + * |
| 29 | + * @param <T> the value type |
| 30 | + * @since 2.0.1 - experimental |
| 31 | + */ |
| 32 | +@Experimental |
| 33 | +public final class MaybeDoFinally<T> extends AbstractMaybeWithUpstream<T, T> { |
| 34 | + |
| 35 | + final Action onFinally; |
| 36 | + |
| 37 | + public MaybeDoFinally(MaybeSource<T> source, Action onFinally) { |
| 38 | + super(source); |
| 39 | + this.onFinally = onFinally; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void subscribeActual(MaybeObserver<? super T> s) { |
| 44 | + source.subscribe(new DoFinallyObserver<T>(s, onFinally)); |
| 45 | + } |
| 46 | + |
| 47 | + static final class DoFinallyObserver<T> extends AtomicInteger implements MaybeObserver<T>, Disposable { |
| 48 | + |
| 49 | + private static final long serialVersionUID = 4109457741734051389L; |
| 50 | + |
| 51 | + final MaybeObserver<? super T> actual; |
| 52 | + |
| 53 | + final Action onFinally; |
| 54 | + |
| 55 | + Disposable d; |
| 56 | + |
| 57 | + DoFinallyObserver(MaybeObserver<? super T> actual, Action onFinally) { |
| 58 | + this.actual = actual; |
| 59 | + this.onFinally = onFinally; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onSubscribe(Disposable d) { |
| 64 | + if (DisposableHelper.validate(this.d, d)) { |
| 65 | + this.d = d; |
| 66 | + |
| 67 | + actual.onSubscribe(this); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void onSuccess(T t) { |
| 73 | + actual.onSuccess(t); |
| 74 | + runFinally(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void onError(Throwable t) { |
| 79 | + actual.onError(t); |
| 80 | + runFinally(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onComplete() { |
| 85 | + actual.onComplete(); |
| 86 | + runFinally(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void dispose() { |
| 91 | + d.dispose(); |
| 92 | + runFinally(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean isDisposed() { |
| 97 | + return d.isDisposed(); |
| 98 | + } |
| 99 | + |
| 100 | + void runFinally() { |
| 101 | + if (compareAndSet(0, 1)) { |
| 102 | + try { |
| 103 | + onFinally.run(); |
| 104 | + } catch (Throwable ex) { |
| 105 | + Exceptions.throwIfFatal(ex); |
| 106 | + RxJavaPlugins.onError(ex); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments