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