LMAX-Exchange/disruptor — pull request #88 by nicholassm (2014-05-23 07:32:50+00:00)
Title: Improve DSL: Make EventHandler's type argument contravariant.

This pull request improves the DSL by making the EventHandler's type argument `T` contravariant:

EventHandler

This makes the DSL more usable while still being type safe. As an example, if I write a generic persistent store for events and the client code's event type just has to implement `java.io.Serializable`, I can now write a generic `EventHandler` that works with any Disruptor with a type `T` that implements `java.io.Serializable`:

class T implements Serializable { /*...*/ }

final Disruptor disruptor = //...
final EventHandler persister = new Persister();

disruptor.handleEventsWith(persister);

Without changing the variance of `T`, you currently have to use a "bridging" closure, because `EventHandler` is invariant:

class T implements Serializable { /*...*/ }

final Disruptor disruptor = //...
final EventHandler persister = new Persister();
EventHandler bridgingEventHandler = new EventHandler
{
@Override public void onEvent(T event, long sequence, boolean endOfBatch) throws Exception
{
persister.onEvent(event, sequence, enOfBatch);
}
};

disruptor.handleEventsWith(bridgingEventHandler);

(The pull request also contains two minor commits -- basic cleanup.)