Skip to content

Commit fa58d36

Browse files
mibac138akarnokd
authored andcommitted
More nullability annotations (ReactiveX#5251)
* More nullability annotations * Refactored imports * Changes based on akarnokd's review * A few more annotations * Changes based on akarnokd's 2nd review
1 parent 5ec4f76 commit fa58d36

35 files changed

+187
-87
lines changed

src/main/java/io/reactivex/Notification.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package io.reactivex;
1515

16+
import io.reactivex.annotations.*;
1617
import io.reactivex.internal.functions.ObjectHelper;
1718
import io.reactivex.internal.util.NotificationLite;
1819

@@ -66,6 +67,7 @@ public boolean isOnNext() {
6667
* @see #isOnNext()
6768
*/
6869
@SuppressWarnings("unchecked")
70+
@Nullable
6971
public T getValue() {
7072
Object o = value;
7173
if (o != null && !NotificationLite.isError(o)) {
@@ -80,6 +82,7 @@ public T getValue() {
8082
* @return the Throwable error contained or null
8183
* @see #isOnError()
8284
*/
85+
@Nullable
8386
public Throwable getError() {
8487
Object o = value;
8588
if (NotificationLite.isError(o)) {
@@ -122,7 +125,8 @@ public String toString() {
122125
* @return the new Notification instance
123126
* @throws NullPointerException if value is null
124127
*/
125-
public static <T> Notification<T> createOnNext(T value) {
128+
@NonNull
129+
public static <T> Notification<T> createOnNext(@NonNull T value) {
126130
ObjectHelper.requireNonNull(value, "value is null");
127131
return new Notification<T>(value);
128132
}
@@ -134,7 +138,8 @@ public static <T> Notification<T> createOnNext(T value) {
134138
* @return the new Notification instance
135139
* @throws NullPointerException if error is null
136140
*/
137-
public static <T> Notification<T> createOnError(Throwable error) {
141+
@NonNull
142+
public static <T> Notification<T> createOnError(@NonNull Throwable error) {
138143
ObjectHelper.requireNonNull(error, "error is null");
139144
return new Notification<T>(NotificationLite.error(error));
140145
}
@@ -146,6 +151,7 @@ public static <T> Notification<T> createOnError(Throwable error) {
146151
* @return the shared Notification instance representing an onComplete signal
147152
*/
148153
@SuppressWarnings("unchecked")
154+
@NonNull
149155
public static <T> Notification<T> createOnComplete() {
150156
return (Notification<T>)COMPLETE;
151157
}

src/main/java/io/reactivex/Observer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package io.reactivex;
1515

16+
import io.reactivex.annotations.NonNull;
1617
import io.reactivex.disposables.Disposable;
1718

1819
/**
@@ -40,7 +41,7 @@ public interface Observer<T> {
4041
* be called anytime to cancel the connection
4142
* @since 2.0
4243
*/
43-
void onSubscribe(Disposable d);
44+
void onSubscribe(@NonNull Disposable d);
4445

4546
/**
4647
* Provides the Observer with a new item to observe.
@@ -53,7 +54,7 @@ public interface Observer<T> {
5354
* @param t
5455
* the item emitted by the Observable
5556
*/
56-
void onNext(T t);
57+
void onNext(@NonNull T t);
5758

5859
/**
5960
* Notifies the Observer that the {@link Observable} has experienced an error condition.
@@ -64,7 +65,7 @@ public interface Observer<T> {
6465
* @param e
6566
* the exception encountered by the Observable
6667
*/
67-
void onError(Throwable e);
68+
void onError(@NonNull Throwable e);
6869

6970
/**
7071
* Notifies the Observer that the {@link Observable} has finished sending push-based notifications.

src/main/java/io/reactivex/Single.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import java.util.NoSuchElementException;
1717
import java.util.concurrent.*;
1818

19-
import org.reactivestreams.Publisher;
20-
2119
import io.reactivex.annotations.*;
2220
import io.reactivex.disposables.Disposable;
2321
import io.reactivex.exceptions.Exceptions;
@@ -34,6 +32,7 @@
3432
import io.reactivex.observers.TestObserver;
3533
import io.reactivex.plugins.RxJavaPlugins;
3634
import io.reactivex.schedulers.Schedulers;
35+
import org.reactivestreams.Publisher;
3736

3837
/**
3938
* The Single class implements the Reactive Pattern for a single value response.
@@ -2714,7 +2713,7 @@ public final void subscribe(SingleObserver<? super T> subscriber) {
27142713
* Override this method in subclasses to handle the incoming SingleObservers.
27152714
* @param observer the SingleObserver to handle, not null
27162715
*/
2717-
protected abstract void subscribeActual(SingleObserver<? super T> observer);
2716+
protected abstract void subscribeActual(@NonNull SingleObserver<? super T> observer);
27182717

27192718
/**
27202719
* Subscribes a given SingleObserver (subclass) to this Single and returns the given

src/main/java/io/reactivex/disposables/ActionDisposable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package io.reactivex.disposables;
1414

15+
import io.reactivex.annotations.NonNull;
1516
import io.reactivex.functions.Action;
1617
import io.reactivex.internal.util.ExceptionHelper;
1718

@@ -24,7 +25,7 @@ final class ActionDisposable extends ReferenceDisposable<Action> {
2425
}
2526

2627
@Override
27-
protected void onDisposed(Action value) {
28+
protected void onDisposed(@NonNull Action value) {
2829
try {
2930
value.run();
3031
} catch (Throwable ex) {

src/main/java/io/reactivex/disposables/CompositeDisposable.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.util.*;
1616

17+
import io.reactivex.annotations.NonNull;
1718
import io.reactivex.exceptions.*;
1819
import io.reactivex.internal.disposables.DisposableContainer;
1920
import io.reactivex.internal.functions.ObjectHelper;
@@ -39,7 +40,7 @@ public CompositeDisposable() {
3940
* Creates a CompositeDisposables with the given array of initial elements.
4041
* @param resources the array of Disposables to start with
4142
*/
42-
public CompositeDisposable(Disposable... resources) {
43+
public CompositeDisposable(@NonNull Disposable... resources) {
4344
ObjectHelper.requireNonNull(resources, "resources is null");
4445
this.resources = new OpenHashSet<Disposable>(resources.length + 1);
4546
for (Disposable d : resources) {
@@ -52,7 +53,7 @@ public CompositeDisposable(Disposable... resources) {
5253
* Creates a CompositeDisposables with the given Iterable sequence of initial elements.
5354
* @param resources the Iterable sequence of Disposables to start with
5455
*/
55-
public CompositeDisposable(Iterable<? extends Disposable> resources) {
56+
public CompositeDisposable(@NonNull Iterable<? extends Disposable> resources) {
5657
ObjectHelper.requireNonNull(resources, "resources is null");
5758
this.resources = new OpenHashSet<Disposable>();
5859
for (Disposable d : resources) {
@@ -85,7 +86,7 @@ public boolean isDisposed() {
8586
}
8687

8788
@Override
88-
public boolean add(Disposable d) {
89+
public boolean add(@NonNull Disposable d) {
8990
ObjectHelper.requireNonNull(d, "d is null");
9091
if (!disposed) {
9192
synchronized (this) {
@@ -110,7 +111,7 @@ public boolean add(Disposable d) {
110111
* @param ds the array of Disposables
111112
* @return true if the operation was successful, false if the container has been disposed
112113
*/
113-
public boolean addAll(Disposable... ds) {
114+
public boolean addAll(@NonNull Disposable... ds) {
114115
ObjectHelper.requireNonNull(ds, "ds is null");
115116
if (!disposed) {
116117
synchronized (this) {
@@ -135,7 +136,7 @@ public boolean addAll(Disposable... ds) {
135136
}
136137

137138
@Override
138-
public boolean remove(Disposable d) {
139+
public boolean remove(@NonNull Disposable d) {
139140
if (delete(d)) {
140141
d.dispose();
141142
return true;
@@ -144,7 +145,7 @@ public boolean remove(Disposable d) {
144145
}
145146

146147
@Override
147-
public boolean delete(Disposable d) {
148+
public boolean delete(@NonNull Disposable d) {
148149
ObjectHelper.requireNonNull(d, "Disposable item is null");
149150
if (disposed) {
150151
return false;

src/main/java/io/reactivex/disposables/Disposables.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
import java.util.concurrent.Future;
1717

18-
import org.reactivestreams.Subscription;
19-
18+
import io.reactivex.annotations.NonNull;
2019
import io.reactivex.functions.Action;
2120
import io.reactivex.internal.disposables.EmptyDisposable;
2221
import io.reactivex.internal.functions.*;
22+
import org.reactivestreams.Subscription;
2323

2424
/**
2525
* Utility class to help create disposables by wrapping
@@ -38,7 +38,8 @@ private Disposables() {
3838
* @param run the Runnable to wrap
3939
* @return the new Disposable instance
4040
*/
41-
public static Disposable fromRunnable(Runnable run) {
41+
@NonNull
42+
public static Disposable fromRunnable(@NonNull Runnable run) {
4243
ObjectHelper.requireNonNull(run, "run is null");
4344
return new RunnableDisposable(run);
4445
}
@@ -49,7 +50,8 @@ public static Disposable fromRunnable(Runnable run) {
4950
* @param run the Action to wrap
5051
* @return the new Disposable instance
5152
*/
52-
public static Disposable fromAction(Action run) {
53+
@NonNull
54+
public static Disposable fromAction(@NonNull Action run) {
5355
ObjectHelper.requireNonNull(run, "run is null");
5456
return new ActionDisposable(run);
5557
}
@@ -60,7 +62,8 @@ public static Disposable fromAction(Action run) {
6062
* @param future the Future to wrap
6163
* @return the new Disposable instance
6264
*/
63-
public static Disposable fromFuture(Future<?> future) {
65+
@NonNull
66+
public static Disposable fromFuture(@NonNull Future<?> future) {
6467
ObjectHelper.requireNonNull(future, "future is null");
6568
return fromFuture(future, true);
6669
}
@@ -72,7 +75,8 @@ public static Disposable fromFuture(Future<?> future) {
7275
* @param allowInterrupt if true, the future cancel happens via Future.cancel(true)
7376
* @return the new Disposable instance
7477
*/
75-
public static Disposable fromFuture(Future<?> future, boolean allowInterrupt) {
78+
@NonNull
79+
public static Disposable fromFuture(@NonNull Future<?> future, boolean allowInterrupt) {
7680
ObjectHelper.requireNonNull(future, "future is null");
7781
return new FutureDisposable(future, allowInterrupt);
7882
}
@@ -83,7 +87,8 @@ public static Disposable fromFuture(Future<?> future, boolean allowInterrupt) {
8387
* @param subscription the Runnable to wrap
8488
* @return the new Disposable instance
8589
*/
86-
public static Disposable fromSubscription(Subscription subscription) {
90+
@NonNull
91+
public static Disposable fromSubscription(@NonNull Subscription subscription) {
8792
ObjectHelper.requireNonNull(subscription, "subscription is null");
8893
return new SubscriptionDisposable(subscription);
8994
}
@@ -92,6 +97,7 @@ public static Disposable fromSubscription(Subscription subscription) {
9297
* Returns a new, non-disposed Disposable instance.
9398
* @return a new, non-disposed Disposable instance
9499
*/
100+
@NonNull
95101
public static Disposable empty() {
96102
return fromRunnable(Functions.EMPTY_RUNNABLE);
97103
}
@@ -100,6 +106,7 @@ public static Disposable empty() {
100106
* Returns a disposed Disposable instance.
101107
* @return a disposed Disposable instance
102108
*/
109+
@NonNull
103110
public static Disposable disposed() {
104111
return EmptyDisposable.INSTANCE;
105112
}

src/main/java/io/reactivex/disposables/ReferenceDisposable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.util.concurrent.atomic.AtomicReference;
1717

18+
import io.reactivex.annotations.NonNull;
1819
import io.reactivex.internal.functions.ObjectHelper;
1920

2021
/**
@@ -31,7 +32,7 @@ abstract class ReferenceDisposable<T> extends AtomicReference<T> implements Disp
3132
super(ObjectHelper.requireNonNull(value, "value is null"));
3233
}
3334

34-
protected abstract void onDisposed(T value);
35+
protected abstract void onDisposed(@NonNull T value);
3536

3637
@Override
3738
public final void dispose() {

src/main/java/io/reactivex/disposables/RunnableDisposable.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package io.reactivex.disposables;
1414

15+
import io.reactivex.annotations.NonNull;
16+
1517
/**
1618
* A disposable container that manages a Runnable instance.
1719
*/
@@ -24,7 +26,7 @@ final class RunnableDisposable extends ReferenceDisposable<Runnable> {
2426
}
2527

2628
@Override
27-
protected void onDisposed(Runnable value) {
29+
protected void onDisposed(@NonNull Runnable value) {
2830
value.run();
2931
}
3032

src/main/java/io/reactivex/disposables/SerialDisposable.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
import java.util.concurrent.atomic.AtomicReference;
1717

18-
import io.reactivex.internal.disposables.*;
18+
import io.reactivex.annotations.Nullable;
19+
import io.reactivex.internal.disposables.DisposableHelper;
1920

2021
/**
2122
* A Disposable container that allows atomically updating/replacing the contained
@@ -36,7 +37,7 @@ public SerialDisposable() {
3637
* Constructs a SerialDisposable with the given initial Disposable instance.
3738
* @param initialDisposable the initial Disposable instance to use, null allowed
3839
*/
39-
public SerialDisposable(Disposable initialDisposable) {
40+
public SerialDisposable(@Nullable Disposable initialDisposable) {
4041
this.resource = new AtomicReference<Disposable>(initialDisposable);
4142
}
4243

@@ -47,7 +48,7 @@ public SerialDisposable(Disposable initialDisposable) {
4748
* @return true if the operation succeeded, false if the container has been disposed
4849
* @see #replace(Disposable)
4950
*/
50-
public boolean set(Disposable next) {
51+
public boolean set(@Nullable Disposable next) {
5152
return DisposableHelper.set(resource, next);
5253
}
5354

@@ -58,14 +59,15 @@ public boolean set(Disposable next) {
5859
* @return true if the operation succeeded, false if the container has been disposed
5960
* @see #set(Disposable)
6061
*/
61-
public boolean replace(Disposable next) {
62+
public boolean replace(@Nullable Disposable next) {
6263
return DisposableHelper.replace(resource, next);
6364
}
6465

6566
/**
6667
* Returns the currently contained Disposable or null if this container is empty.
6768
* @return the current Disposable, may be null
6869
*/
70+
@Nullable
6971
public Disposable get() {
7072
Disposable d = resource.get();
7173
if (d == DisposableHelper.DISPOSED) {

src/main/java/io/reactivex/disposables/SubscriptionDisposable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package io.reactivex.disposables;
1414

15+
import io.reactivex.annotations.NonNull;
1516
import org.reactivestreams.Subscription;
1617

1718
/**
@@ -26,7 +27,7 @@ final class SubscriptionDisposable extends ReferenceDisposable<Subscription> {
2627
}
2728

2829
@Override
29-
protected void onDisposed(Subscription value) {
30+
protected void onDisposed(@NonNull Subscription value) {
3031
value.cancel();
3132
}
3233
}

0 commit comments

Comments
 (0)