| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 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.rxjava3.internal.operators.flowable; |
| 15 |
|
| 16 |
import java.util.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.Flowable; |
| 19 |
import io.reactivex.rxjava3.internal.util.*; |
| 20 |
import io.reactivex.rxjava3.subscribers.DefaultSubscriber; |
| 21 |
|
| 22 |
/** |
| 23 |
* Returns an Iterable that always returns the item most recently emitted by an Observable, or a |
| 24 |
* seed value if no item has yet been emitted. |
| 25 |
* <p> |
| 26 |
* <img width="640" height="490" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/B.mostRecent.v3.png" alt=""> |
| 27 |
* |
| 28 |
* @param <T> the value type |
| 29 |
*/ |
| 30 |
public final class BlockingFlowableMostRecent<T> implements Iterable<T> { |
| 31 |
|
| 32 |
final Flowable<T> source; |
| 33 |
|
| 34 |
final T initialValue; |
| 35 |
|
| 36 |
public BlockingFlowableMostRecent(Flowable<T> source, T initialValue) { |
| 37 |
this.source = source; |
| 38 |
this.initialValue = initialValue; |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
public Iterator<T> iterator() { |
| 43 |
MostRecentSubscriber<T> mostRecentSubscriber = new MostRecentSubscriber<>(initialValue); |
| 44 |
|
| 45 |
source.subscribe(mostRecentSubscriber); |
| 46 |
|
| 47 |
return mostRecentSubscriber.getIterable(); |
| 48 |
} |
| 49 |
|
| 50 |
static final class MostRecentSubscriber<T> extends DefaultSubscriber<T> { |
| 51 |
volatile Object value; |
| 52 |
|
| 53 |
MostRecentSubscriber(T value) { |
| 54 |
this.value = NotificationLite.next(value); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void onComplete() { |
| 59 |
value = NotificationLite.complete(); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void onError(Throwable e) { |
| 64 |
value = NotificationLite.error(e); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public void onNext(T args) { |
| 69 |
value = NotificationLite.next(args); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* The {@link Iterator} return is not thread safe. In other words don't call {@link Iterator#hasNext()} in one |
| 74 |
* thread expect {@link Iterator#next()} called from a different thread to work. |
| 75 |
* @return the Iterator |
| 76 |
*/ |
| 77 |
public Iterator getIterable() { |
| 78 |
return new Iterator(); |
| 79 |
} |
| 80 |
|
| 81 |
final class Iterator implements java.util.Iterator<T> { |
| 82 |
/** |
| 83 |
* buffer to make sure that the state of the iterator doesn't change between calling hasNext() and next(). |
| 84 |
*/ |
| 85 |
private Object buf; |
| 86 |
|
| 87 |
@Override |
| 88 |
public boolean hasNext() { |
| 89 |
buf = value; |
| 90 |
return !NotificationLite.isComplete(buf); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public T next() { |
| 95 |
try { |
| 96 |
// if hasNext wasn't called before calling next. |
| 97 |
if (buf == null) { |
| 98 |
buf = value; |
| 99 |
} |
| 100 |
if (NotificationLite.isComplete(buf)) { |
| 101 |
throw new NoSuchElementException(); |
| 102 |
} |
| 103 |
if (NotificationLite.isError(buf)) { |
| 104 |
throw ExceptionHelper.wrapOrThrow(NotificationLite.getError(buf)); |
| 105 |
} |
| 106 |
return NotificationLite.getValue(buf); |
| 107 |
} |
| 108 |
finally { |
| 109 |
buf = null; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void remove() { |
| 115 |
throw new UnsupportedOperationException("Read only iterator"); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |