Debian Codegraph @

src/test/java/io/reactivex/rxjava3/flowable/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.flowable;
15
16 import java.util.*;
17 import java.util.concurrent.ConcurrentLinkedQueue;
18 import java.util.concurrent.atomic.AtomicLong;
19
20 import org.reactivestreams.*;
21
22 import io.reactivex.rxjava3.core.Flowable;
23 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
24 import io.reactivex.rxjava3.internal.util.BackpressureHelper;
25
26 /**
27 * Creates {@link Flowable} of a number of items followed by either an error or
28 * completion. Cancellation has no effect on preventing emissions until the
29 * currently outstanding requests have been met.
30 * @param <T> the value type
31 */
32 public final class Burst<T> extends Flowable<T> {
33
34 final List<T> items;
35 final Throwable error;
36
37 Burst(Throwable error, List<T> items) {
38 if (items.isEmpty()) {
39 throw new IllegalArgumentException("items cannot be empty");
40 }
41 for (T item : items) {
42 if (item == null) {
43 throw new IllegalArgumentException("items cannot include null");
44 }
45 }
46 this.error = error;
47 this.items = items;
48 }
49
50 @Override
51 protected void subscribeActual(final Subscriber<? super T> subscriber) {
52 subscriber.onSubscribe(new BurstSubscription(subscriber));
53
54 }
55
56 public static <T> Builder<T> item(T item) {
57 return items(item);
58 }
59
60 @SafeVarargs
61 public static <T> Builder<T> items(T... items) {
62 return new Builder<>(Arrays.asList(items));
63 }
64
65 final class BurstSubscription implements Subscription {
66 private final Subscriber<? super T> subscriber;
67 final Queue<T> q = new ConcurrentLinkedQueue<>(items);
68 final AtomicLong requested = new AtomicLong();
69 volatile boolean cancelled;
70
71 BurstSubscription(Subscriber<? super T> subscriber) {
72 this.subscriber = subscriber;
73 }
74
75 @Override
76 public void request(long n) {
77 if (cancelled) {
78 // required by reactive-streams-jvm 3.6
79 return;
80 }
81 if (SubscriptionHelper.validate(n)) {
82 // just for testing, don't care about perf
83 // so no attempt made to reduce volatile reads
84 if (BackpressureHelper.add(requested, n) == 0) {
85 if (q.isEmpty()) {
86 return;
87 }
88 while (!q.isEmpty() && requested.get() > 0) {
89 T item = q.poll();
90 requested.decrementAndGet();
91 subscriber.onNext(item);
92 }
93 if (q.isEmpty()) {
94 if (error != null) {
95 subscriber.onError(error);
96 } else {
97 subscriber.onComplete();
98 }
99 }
100 }
101 }
102 }
103
104 @Override
105 public void cancel() {
106 cancelled = true;
107 }
108 }
109
110 public static final class Builder<T> {
111
112 private final List<T> items;
113 private Throwable error;
114
115 Builder(List<T> items) {
116 this.items = items;
117 }
118
119 public Flowable<T> error(Throwable e) {
120 this.error = e;
121 return create();
122 }
123
124 public Flowable<T> create() {
125 return new Burst<>(error, items);
126 }
127
128 }
129
130 }