Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/Burst.java

raw
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.observable;
15
16 import java.util.*;
17
18 import io.reactivex.rxjava3.core.Observable;
19 import io.reactivex.rxjava3.core.Observer;
20 import io.reactivex.rxjava3.disposables.Disposable;
21
22 /**
23 * Creates {@link Observable} of a number of items followed by either an error or
24 * completion. Subscription status is not checked before emitting an event.
25 *
26 * @param <T> the value type
27 */
28 public final class Burst<T> extends Observable<T> {
29
30 final List<T> items;
31 final Throwable error;
32
33 Burst(Throwable error, List<T> items) {
34 this.error = error;
35 this.items = items;
36 }
37
38 @Override
39 protected void subscribeActual(final Observer<? super T> observer) {
40 observer.onSubscribe(Disposable.empty());
41 for (T item: items) {
42 observer.onNext(item);
43 }
44 if (error != null) {
45 observer.onError(error);
46 } else {
47 observer.onComplete();
48 }
49 }
50
51 public static <T> Builder<T> item(T item) {
52 return items(item);
53 }
54
55 @SafeVarargs
56 public static <T> Builder<T> items(T... items) {
57 return new Builder<>(Arrays.asList(items));
58 }
59
60 public static final class Builder<T> {
61
62 private final List<T> items;
63 private Throwable error;
64
65 Builder(List<T> items) {
66 this.items = items;
67 }
68
69 public Observable<T> error(Throwable e) {
70 this.error = e;
71 return create();
72 }
73
74 public Observable<T> create() {
75 return new Burst<>(error, items);
76 }
77
78 }
79
80 }