Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableAutoConnect.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.concurrent.atomic.AtomicInteger;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.functions.Consumer;
21 import io.reactivex.rxjava3.observables.ConnectableObservable;
22
23 /**
24 * Wraps a ConnectableObservable and calls its connect() method once
25 * the specified number of Observers have subscribed.
26 *
27 * @param <T> the value type of the chain
28 */
29 public final class ObservableAutoConnect<T> extends Observable<T> {
30 final ConnectableObservable<? extends T> source;
31 final int numberOfObservers;
32 final Consumer<? super Disposable> connection;
33 final AtomicInteger clients;
34
35 public ObservableAutoConnect(ConnectableObservable<? extends T> source,
36 int numberOfObservers,
37 Consumer<? super Disposable> connection) {
38 this.source = source;
39 this.numberOfObservers = numberOfObservers;
40 this.connection = connection;
41 this.clients = new AtomicInteger();
42 }
43
44 @Override
45 public void subscribeActual(Observer<? super T> child) {
46 source.subscribe(child);
47 if (clients.incrementAndGet() == numberOfObservers) {
48 source.connect(connection);
49 }
50 }
51 }