|
| 1 | +package rx.plugins; |
| 2 | + |
| 3 | +import rx.Notification; |
| 4 | +import rx.Observable.OnSubscribe; |
| 5 | +import rx.Observer; |
| 6 | +import rx.observers.SafeSubscriber; |
| 7 | +import rx.operators.DebugSubscriber; |
| 8 | +import rx.operators.Operator; |
| 9 | +import rx.plugins.DebugNotification.Kind; |
| 10 | + |
| 11 | +public class DebugNotification<T> { |
| 12 | + public static enum Kind { |
| 13 | + OnNext, OnError, OnCompleted, Subscribe, Unsubscribe |
| 14 | + } |
| 15 | + |
| 16 | + private final OnSubscribe<T> source; |
| 17 | + private final Operator<T, ?> from; |
| 18 | + private final Kind kind; |
| 19 | + private final Notification<T> notification; |
| 20 | + private final Operator<?, T> to; |
| 21 | + private final long nanoTime; |
| 22 | + private final long threadId; |
| 23 | + private Observer o; |
| 24 | + |
| 25 | + public static <T> DebugNotification<T> createSubscribe(Observer<? super T> o, OnSubscribe<T> source) { |
| 26 | + Operator<?, T> to = null; |
| 27 | + Operator<T, ?> from = null; |
| 28 | + if (o instanceof DebugSubscriber) { |
| 29 | + to = ((DebugSubscriber<T>) o).getTo(); |
| 30 | + from = ((DebugSubscriber<T>) o).getFrom(); |
| 31 | + o = ((DebugSubscriber) o).getActual(); |
| 32 | + } |
| 33 | + return new DebugNotification<T>(o, from, Kind.Subscribe, null, to, source); |
| 34 | + } |
| 35 | + |
| 36 | + public static <T> DebugNotification<T> createOnNext(Observer<? super T> o, Operator<T, ?> from, T t, Operator<?, T> to) { |
| 37 | + return new DebugNotification<T>(o, from, Kind.OnNext, Notification.createOnNext(t), to, null); |
| 38 | + } |
| 39 | + |
| 40 | + public static <T> DebugNotification<T> createOnError(Observer<? super T> o, Operator<T, ?> from, Throwable e, Operator<?, T> to) { |
| 41 | + return new DebugNotification<T>(o, from, Kind.OnError, Notification.<T> createOnError(e), to, null); |
| 42 | + } |
| 43 | + |
| 44 | + public static <T> DebugNotification<T> createOnCompleted(Observer<? super T> o, Operator<T, ?> from, Operator<?, T> to) { |
| 45 | + return new DebugNotification<T>(o, from, Kind.OnCompleted, Notification.<T> createOnCompleted(), to, null); |
| 46 | + } |
| 47 | + |
| 48 | + public static <T> DebugNotification<T> createUnsubscribe(Observer<? super T> o, Operator<T, ?> from, Operator<?, T> to) { |
| 49 | + return new DebugNotification<T>(o, from, Kind.Unsubscribe, null, to, null); |
| 50 | + } |
| 51 | + |
| 52 | + private DebugNotification(Observer o, Operator<T, ?> from, Kind kind, Notification<T> notification, Operator<?, T> to, OnSubscribe<T> source) { |
| 53 | + this.o = (o instanceof SafeSubscriber) ? ((SafeSubscriber) o).getActual() : o; |
| 54 | + this.from = from; |
| 55 | + this.kind = kind; |
| 56 | + this.notification = notification; |
| 57 | + this.to = to; |
| 58 | + this.source = source; |
| 59 | + this.nanoTime = System.nanoTime(); |
| 60 | + this.threadId = Thread.currentThread().getId(); |
| 61 | + } |
| 62 | + |
| 63 | + public Operator<T, ?> getFrom() { |
| 64 | + return from; |
| 65 | + } |
| 66 | + |
| 67 | + public Notification<T> getNotification() { |
| 68 | + return notification; |
| 69 | + } |
| 70 | + |
| 71 | + public Operator<?, T> getTo() { |
| 72 | + return to; |
| 73 | + } |
| 74 | + |
| 75 | + public long getNanoTime() { |
| 76 | + return nanoTime; |
| 77 | + } |
| 78 | + |
| 79 | + public long getThreadId() { |
| 80 | + return threadId; |
| 81 | + } |
| 82 | + |
| 83 | + public Kind getKind() { |
| 84 | + return kind; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String toString() { |
| 89 | + final StringBuilder s = new StringBuilder("{"); |
| 90 | + s.append(" \"nano\": ").append(nanoTime); |
| 91 | + s.append(", \"thread\": ").append(threadId); |
| 92 | + s.append(", \"observer\": \"").append(o.getClass().getName()).append("@").append(Integer.toHexString(o.hashCode())).append("\""); |
| 93 | + s.append(", \"type\": \"").append(kind).append("\""); |
| 94 | + if (notification != null) { |
| 95 | + if (notification.hasValue()) |
| 96 | + s.append(", \"value\": \"").append(notification.getValue()).append("\""); |
| 97 | + if (notification.hasThrowable()) |
| 98 | + s.append(", \"exception\": \"").append(notification.getThrowable().getMessage().replace("\\", "\\\\").replace("\"", "\\\"")).append("\""); |
| 99 | + } |
| 100 | + if (source != null) |
| 101 | + s.append(", \"source\": \"").append(source.getClass().getName()).append("@").append(Integer.toHexString(source.hashCode())).append("\""); |
| 102 | + if (from != null) |
| 103 | + s.append(", \"from\": \"").append(from.getClass().getName()).append("@").append(Integer.toHexString(from.hashCode())).append("\""); |
| 104 | + if (to != null) |
| 105 | + s.append(", \"to\": \"").append(to.getClass().getName()).append("@").append(Integer.toHexString(to.hashCode())).append("\""); |
| 106 | + s.append("}"); |
| 107 | + return s.toString(); |
| 108 | + } |
| 109 | +} |
0 commit comments