1

I'm implementing a byte message deserializer, which would dispatch deserialized messages on a dispatcher interface and return an observable of all Throwables thrown, so that client code can handle the errors.

A sketch of the prototype of the method doing that:

Observable<Throwable> dispatchDeserializedMessages(Observable<byte[]>, Dispatcher)

Now as of recent I am familiar with Subject<T, R>, which would fit here perfectly, e.g.

Subject<byte[], Throwable> dispatchDeserializedMessages(Dispatcher)

But there are no convenience methods like create() which could easily delegate to an observer and an observable. All concrete implementations unify T with R, so there's no way I could use one of those.

So my concrete question: Is there a way I can instantiate a suitable Subject<byte[], Throwable> which delegates to an Observer and Observable? Is there any other way I can create such a Subject without having to implement (in the sense of having to delegate each implemented method by hand) the whole of Subject, Observable and Observer?

Sebastian Graf
  • 3,602
  • 3
  • 27
  • 38
  • Would you find an advantage in wrapping your `byte[]` in a Class, maybe named `DeserializedMessage` or such? – John Hascall Jan 21 '16 at 09:48
  • Not really, I'd call that a disadvantage. If I understand correctly, you propose to outsource the decoding step into a method producing `DeserializedMessage`s from `byte[]` chunks. I'd much rather stay with dual/visitor coding for now (call the `onTypeOfMessage()` on the dispatcher), because it solves the needed part (type-safe extensible tools) of the expression problem for us. – Sebastian Graf Jan 21 '16 at 15:10
  • 1
    YMMV, but I just hate passing primitive types around, because invariably it seems something changes, and you've got no room to maneuver. – John Hascall Jan 21 '16 at 15:20
  • You might be right when facing a long-term industry project, but this is just for a homework assignment implementing an IETF RFC. The `byte[]` comes straight from the socket's `InputStream` and I doubt that will change for the next 2 months :). – Sebastian Graf Jan 21 '16 at 15:24
  • Oh, homework -- yeah, the simplest, quickest-written solution is best :) – John Hascall Jan 21 '16 at 15:41

1 Answers1

2

Switching to Subject-based API might not be the best idea because you change a potentially cold API into a mandatory hot API. In your original design, the consumer of the Throwable sequence would assume when it subscribes, the Observable<byte[]> gets subscribed to too.

Otherwise, I have a blog series about creating Subjects but you can't avoid the heavy lifting with them.

akarnokd
  • 69,132
  • 14
  • 157
  • 192