|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.operators; |
| 17 | + |
| 18 | +import rx.Observable.Operator; |
| 19 | +import rx.Subscriber; |
| 20 | + |
| 21 | +/** |
| 22 | + * If the Observable completes after emitting a single item that matches a |
| 23 | + * predicate, return an Observable containing that item. If it emits more than |
| 24 | + * one such item or no item, throw an IllegalArgumentException. |
| 25 | + */ |
| 26 | +public final class OperatorSingle<T> implements Operator<T, T> { |
| 27 | + |
| 28 | + private final boolean hasDefaultValue; |
| 29 | + private final T defaultValue; |
| 30 | + |
| 31 | + public OperatorSingle() { |
| 32 | + this(false, null); |
| 33 | + } |
| 34 | + |
| 35 | + public OperatorSingle(T defaultValue) { |
| 36 | + this(true, defaultValue); |
| 37 | + } |
| 38 | + |
| 39 | + private OperatorSingle(boolean hasDefaultValue, final T defaultValue) { |
| 40 | + this.hasDefaultValue = hasDefaultValue; |
| 41 | + this.defaultValue = defaultValue; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public Subscriber<? super T> call(final Subscriber<? super T> subscriber) { |
| 46 | + return new Subscriber<T>(subscriber) { |
| 47 | + |
| 48 | + private T value; |
| 49 | + private boolean isNonEmpty = false; |
| 50 | + private boolean hasTooManyElemenets = false; |
| 51 | + |
| 52 | + @Override |
| 53 | + public void onNext(T value) { |
| 54 | + if (isNonEmpty) { |
| 55 | + hasTooManyElemenets = true; |
| 56 | + subscriber.onError(new IllegalArgumentException("Sequence contains too many elements")); |
| 57 | + } else { |
| 58 | + this.value = value; |
| 59 | + isNonEmpty = true; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onCompleted() { |
| 65 | + if (hasTooManyElemenets) { |
| 66 | + // We has already sent an onError message |
| 67 | + } else { |
| 68 | + if (isNonEmpty) { |
| 69 | + subscriber.onNext(value); |
| 70 | + subscriber.onCompleted(); |
| 71 | + } else { |
| 72 | + if (hasDefaultValue) { |
| 73 | + subscriber.onNext(defaultValue); |
| 74 | + subscriber.onCompleted(); |
| 75 | + } else { |
| 76 | + subscriber.onError(new IllegalArgumentException("Sequence contains no elements")); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onError(Throwable e) { |
| 84 | + subscriber.onError(e); |
| 85 | + } |
| 86 | + |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments