|
| 1 | +package rx.operators; |
| 2 | + |
| 3 | +import java.io.ObjectStreamException; |
| 4 | +import java.io.Serializable; |
| 5 | + |
| 6 | +import rx.Notification; |
| 7 | +import rx.Notification.Kind; |
| 8 | +import rx.Observer; |
| 9 | + |
| 10 | +/** |
| 11 | + * For use in internal operators that need something like materialize and dematerialize wholly |
| 12 | + * within the implementation of the operator but don't want to incur the allocation cost of actually |
| 13 | + * creating {@link rx.Notification} objects for every onNext and onComplete. |
| 14 | + * |
| 15 | + * An object is allocated inside {@link #error(Throwable)} to wrap the {@link Throwable} but this |
| 16 | + * shouldn't effect performance because exceptions should be exceptionally rare. |
| 17 | + * |
| 18 | + * It's implemented as a singleton to maintain some semblance of type safety that is completely |
| 19 | + * non-existent. |
| 20 | + * |
| 21 | + * @author gscampbell |
| 22 | + * |
| 23 | + * @param <T> |
| 24 | + */ |
| 25 | +public final class NotificationLite<T> { |
| 26 | + private NotificationLite() { |
| 27 | + } |
| 28 | + |
| 29 | + private static final NotificationLite INSTANCE = new NotificationLite(); |
| 30 | + |
| 31 | + @SuppressWarnings("unchecked") |
| 32 | + public static <T> NotificationLite<T> instance() { |
| 33 | + return INSTANCE; |
| 34 | + } |
| 35 | + |
| 36 | + private static final Object ON_COMPLETED_SENTINEL = new Serializable() { |
| 37 | + private static final long serialVersionUID = 1; |
| 38 | + }; |
| 39 | + |
| 40 | + private static final Object ON_NEXT_NULL_SENTINEL = new Serializable() { |
| 41 | + private static final long serialVersionUID = 2; |
| 42 | + }; |
| 43 | + |
| 44 | + private static class OnErrorSentinel implements Serializable { |
| 45 | + private static final long serialVersionUID = 3; |
| 46 | + private final Throwable e; |
| 47 | + |
| 48 | + public OnErrorSentinel(Throwable e) { |
| 49 | + this.e = e; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates a lite onNext notification for the value passed in without doing any allocation. Can |
| 55 | + * be unwrapped and sent with the {@link #accept} method. |
| 56 | + * |
| 57 | + * @param t |
| 58 | + * @return |
| 59 | + */ |
| 60 | + public Object next(T t) { |
| 61 | + if (t == null) |
| 62 | + return ON_NEXT_NULL_SENTINEL; |
| 63 | + else |
| 64 | + return t; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a lite onComplete notification without doing any allocation. Can be unwrapped and |
| 69 | + * sent with the {@link #accept} method. |
| 70 | + * |
| 71 | + * @return |
| 72 | + */ |
| 73 | + public Object completed() { |
| 74 | + return ON_COMPLETED_SENTINEL; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Create a lite onError notification. This call does new up an object to wrap the |
| 79 | + * {@link Throwable} but since there should only be one of these the performance impact should |
| 80 | + * be small. Can be unwrapped and sent with the {@link #accept} method. |
| 81 | + * |
| 82 | + * @param e |
| 83 | + * @return |
| 84 | + */ |
| 85 | + public Object error(Throwable e) { |
| 86 | + return new OnErrorSentinel(e); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Unwraps the lite notification and calls the appropriate method on the {@link Observer}. |
| 91 | + * |
| 92 | + * @param o |
| 93 | + * the {@link Observer} to call onNext, onCompleted or onError. |
| 94 | + * @param n |
| 95 | + * @throws IllegalArgumentException |
| 96 | + * if the notification is null. |
| 97 | + * @throws NullPointerException |
| 98 | + * if the {@link Observer} is null. |
| 99 | + */ |
| 100 | + @SuppressWarnings("unchecked") |
| 101 | + public void accept(Observer<? super T> o, Object n) { |
| 102 | + switch (kind(n)) { |
| 103 | + case OnNext: |
| 104 | + o.onNext(getValue(n)); |
| 105 | + break; |
| 106 | + case OnCompleted: |
| 107 | + o.onCompleted(); |
| 108 | + break; |
| 109 | + case OnError: |
| 110 | + o.onError(getError(n)); |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public boolean isCompleted(Object n) { |
| 116 | + return n == ON_COMPLETED_SENTINEL; |
| 117 | + } |
| 118 | + |
| 119 | + public boolean isError(Object n) { |
| 120 | + return n instanceof OnErrorSentinel; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * If there is custom logic that isn't as simple as call the right method on an {@link Observer} |
| 125 | + * then this method can be used to get the {@link Notification.Kind} |
| 126 | + * |
| 127 | + * @param n |
| 128 | + * @return |
| 129 | + */ |
| 130 | + public Kind kind(Object n) { |
| 131 | + if (n == null) |
| 132 | + throw new IllegalArgumentException("The lite notification can not be null"); |
| 133 | + else if (n == ON_COMPLETED_SENTINEL) |
| 134 | + return Kind.OnCompleted; |
| 135 | + else if (n instanceof OnErrorSentinel) |
| 136 | + return Kind.OnError; |
| 137 | + else |
| 138 | + // value or ON_NEXT_NULL_SENTINEL but either way it's an OnNext |
| 139 | + return Kind.OnNext; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * returns value passed in {@link #next(Object)} method call. Bad things happen if you call this |
| 144 | + * the onComplete or onError notification type. For performance you are expected to use this |
| 145 | + * when it is appropriate. |
| 146 | + * |
| 147 | + * @param n |
| 148 | + * @return |
| 149 | + */ |
| 150 | + @SuppressWarnings("unchecked") |
| 151 | + public T getValue(Object n) { |
| 152 | + return n == ON_NEXT_NULL_SENTINEL ? null : (T) n; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * returns {@link Throwable} passed in {@link #error(Throwable)} method call. Bad things happen |
| 157 | + * if you |
| 158 | + * call this the onComplete or onNext notification type. For performance you are expected to use |
| 159 | + * this when it is appropriate. |
| 160 | + * |
| 161 | + * @param n |
| 162 | + * @return The {@link Throwable} wrapped inside n |
| 163 | + */ |
| 164 | + public Throwable getError(Object n) { |
| 165 | + return ((OnErrorSentinel) n).e; |
| 166 | + } |
| 167 | +} |
0 commit comments