| 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.core; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.annotations.*; |
| 17 |
import io.reactivex.rxjava3.internal.util.NotificationLite; |
| 18 |
import java.util.Objects; |
| 19 |
|
| 20 |
/** |
| 21 |
* Represents the reactive signal types: {@code onNext}, {@code onError} and {@code onComplete} and |
| 22 |
* holds their parameter values (a value, a {@link Throwable}, nothing). |
| 23 |
* @param <T> the value type |
| 24 |
*/ |
| 25 |
public final class Notification<T> { |
| 26 |
|
| 27 |
final Object value; |
| 28 |
|
| 29 |
/** Not meant to be implemented externally. |
| 30 |
* @param value the value to carry around in the notification, not {@code null} |
| 31 |
*/ |
| 32 |
private Notification(@Nullable Object value) { |
| 33 |
this.value = value; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns true if this notification is an {@code onComplete} signal. |
| 38 |
* @return true if this notification is an {@code onComplete} signal |
| 39 |
*/ |
| 40 |
public boolean isOnComplete() { |
| 41 |
return value == null; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns true if this notification is an {@code onError} signal and |
| 46 |
* {@link #getError()} returns the contained {@link Throwable}. |
| 47 |
* @return true if this notification is an {@code onError} signal |
| 48 |
* @see #getError() |
| 49 |
*/ |
| 50 |
public boolean isOnError() { |
| 51 |
return NotificationLite.isError(value); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns true if this notification is an {@code onNext} signal and |
| 56 |
* {@link #getValue()} returns the contained value. |
| 57 |
* @return true if this notification is an {@code onNext} signal |
| 58 |
* @see #getValue() |
| 59 |
*/ |
| 60 |
public boolean isOnNext() { |
| 61 |
Object o = value; |
| 62 |
return o != null && !NotificationLite.isError(o); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns the contained value if this notification is an {@code onNext} |
| 67 |
* signal, null otherwise. |
| 68 |
* @return the value contained or null |
| 69 |
* @see #isOnNext() |
| 70 |
*/ |
| 71 |
@SuppressWarnings("unchecked") |
| 72 |
@Nullable |
| 73 |
public T getValue() { |
| 74 |
Object o = value; |
| 75 |
if (o != null && !NotificationLite.isError(o)) { |
| 76 |
return (T)value; |
| 77 |
} |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns the container {@link Throwable} error if this notification is an {@code onError} |
| 83 |
* signal, null otherwise. |
| 84 |
* @return the {@code Throwable} error contained or {@code null} |
| 85 |
* @see #isOnError() |
| 86 |
*/ |
| 87 |
@Nullable |
| 88 |
public Throwable getError() { |
| 89 |
Object o = value; |
| 90 |
if (NotificationLite.isError(o)) { |
| 91 |
return NotificationLite.getError(o); |
| 92 |
} |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public boolean equals(Object obj) { |
| 98 |
if (obj instanceof Notification) { |
| 99 |
Notification<?> n = (Notification<?>) obj; |
| 100 |
return Objects.equals(value, n.value); |
| 101 |
} |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public int hashCode() { |
| 107 |
Object o = value; |
| 108 |
return o != null ? o.hashCode() : 0; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public String toString() { |
| 113 |
Object o = value; |
| 114 |
if (o == null) { |
| 115 |
return "OnCompleteNotification"; |
| 116 |
} |
| 117 |
if (NotificationLite.isError(o)) { |
| 118 |
return "OnErrorNotification[" + NotificationLite.getError(o) + "]"; |
| 119 |
} |
| 120 |
return "OnNextNotification[" + value + "]"; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Constructs an onNext notification containing the given value. |
| 125 |
* @param <T> the value type |
| 126 |
* @param value the value to carry around in the notification, not {@code null} |
| 127 |
* @return the new Notification instance |
| 128 |
* @throws NullPointerException if value is {@code null} |
| 129 |
*/ |
| 130 |
@NonNull |
| 131 |
public static <@NonNull T> Notification<T> createOnNext(T value) { |
| 132 |
Objects.requireNonNull(value, "value is null"); |
| 133 |
return new Notification<>(value); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Constructs an onError notification containing the error. |
| 138 |
* @param <T> the value type |
| 139 |
* @param error the error Throwable to carry around in the notification, not null |
| 140 |
* @return the new Notification instance |
| 141 |
* @throws NullPointerException if error is {@code null} |
| 142 |
*/ |
| 143 |
@NonNull |
| 144 |
public static <T> Notification<T> createOnError(@NonNull Throwable error) { |
| 145 |
Objects.requireNonNull(error, "error is null"); |
| 146 |
return new Notification<>(NotificationLite.error(error)); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Returns the empty and stateless shared instance of a notification representing |
| 151 |
* an {@code onComplete} signal. |
| 152 |
* @param <T> the target value type |
| 153 |
* @return the shared Notification instance representing an {@code onComplete} signal |
| 154 |
*/ |
| 155 |
@SuppressWarnings("unchecked") |
| 156 |
@NonNull |
| 157 |
public static <T> Notification<T> createOnComplete() { |
| 158 |
return (Notification<T>)COMPLETE; |
| 159 |
} |
| 160 |
|
| 161 |
/** The singleton instance for createOnComplete. */ |
| 162 |
static final Notification<Object> COMPLETE = new Notification<>(null); |
| 163 |
} |