Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/schedulers/ImmediateThinScheduler.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.schedulers;
15
16 import java.util.concurrent.TimeUnit;
17
18 import io.reactivex.rxjava3.annotations.NonNull;
19 import io.reactivex.rxjava3.core.Scheduler;
20 import io.reactivex.rxjava3.disposables.*;
21
22 /**
23 * A Scheduler partially implementing the API by allowing only non-delayed, non-periodic
24 * task execution on the current thread immediately.
25 * <p>
26 * Note that this doesn't support recursive scheduling and disposing the returned Disposable
27 * has no effect (because when the schedule() method returns, the task has been already run).
28 */
29 public final class ImmediateThinScheduler extends Scheduler {
30
31 /**
32 * The singleton instance of the immediate (thin) scheduler.
33 */
34 public static final Scheduler INSTANCE = new ImmediateThinScheduler();
35
36 static final Worker WORKER = new ImmediateThinWorker();
37
38 static final Disposable DISPOSED;
39
40 static {
41 DISPOSED = Disposable.empty();
42 DISPOSED.dispose();
43 }
44
45 private ImmediateThinScheduler() {
46 // singleton class
47 }
48
49 @NonNull
50 @Override
51 public Disposable scheduleDirect(@NonNull Runnable run) {
52 run.run();
53 return DISPOSED;
54 }
55
56 @NonNull
57 @Override
58 public Disposable scheduleDirect(@NonNull Runnable run, long delay, TimeUnit unit) {
59 throw new UnsupportedOperationException("This scheduler doesn't support delayed execution");
60 }
61
62 @NonNull
63 @Override
64 public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit) {
65 throw new UnsupportedOperationException("This scheduler doesn't support periodic execution");
66 }
67
68 @NonNull
69 @Override
70 public Worker createWorker() {
71 return WORKER;
72 }
73
74 static final class ImmediateThinWorker extends Worker {
75
76 @Override
77 public void dispose() {
78 // This worker is always stateless and won't track tasks
79 }
80
81 @Override
82 public boolean isDisposed() {
83 return false; // dispose() has no effect
84 }
85
86 @NonNull
87 @Override
88 public Disposable schedule(@NonNull Runnable run) {
89 run.run();
90 return DISPOSED;
91 }
92
93 @NonNull
94 @Override
95 public Disposable schedule(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) {
96 throw new UnsupportedOperationException("This scheduler doesn't support delayed execution");
97 }
98
99 @NonNull
100 @Override
101 public Disposable schedulePeriodically(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit) {
102 throw new UnsupportedOperationException("This scheduler doesn't support periodic execution");
103 }
104 }
105 }