Skip to content

Instantly share code, notes, and snippets.

@patriknw
Created February 6, 2014 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patriknw/8840129 to your computer and use it in GitHub Desktop.
Save patriknw/8840129 to your computer and use it in GitHub Desktop.
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.event.japi
/**
* Java API: See documentation for [[akka.event.EventBus]]
* E is the Event type
* S is the Subscriber type
* C is the Classifier type
*/
trait EventBus[E, S, C] {
/**
* Attempts to register the subscriber to the specified Classifier
* @return true if successful and false if not (because it was already subscribed to that Classifier, or otherwise)
*/
def subscribe(subscriber: S, to: C): Boolean
/**
* Attempts to deregister the subscriber from the specified Classifier
* @return true if successful and false if not (because it wasn't subscribed to that Classifier, or otherwise)
*/
def unsubscribe(subscriber: S, from: C): Boolean
/**
* Attempts to deregister the subscriber from all Classifiers it may be subscribed to
*/
def unsubscribe(subscriber: S): Unit
/**
* Publishes the specified Event to this bus
*/
def publish(event: E): Unit
}
/**
* Java API: See documentation for [[akka.event.LookupClassification]]
* E is the Event type
* S is the Subscriber type
* C is the Classifier type
*/
abstract class LookupEventBus[E, S, C] extends EventBus[E, S, C] {
private val bus = new akka.event.EventBus with akka.event.LookupClassification {
type Event = E
type Subscriber = S
type Classifier = C
override protected def mapSize: Int = LookupEventBus.this.mapSize
override protected def compareSubscribers(a: S, b: S): Int =
LookupEventBus.this.compareSubscribers(a, b)
override protected def classify(event: E): C =
LookupEventBus.this.classify(event)
override protected def publish(event: E, subscriber: S): Unit =
LookupEventBus.this.publish(event, subscriber)
}
/**
* This is a size hint for the number of Classifiers you expect to have (use powers of 2)
*/
protected def mapSize(): Int
/**
* Provides a total ordering of Subscribers (think java.util.Comparator.compare)
*/
protected def compareSubscribers(a: S, b: S): Int
/**
* Returns the Classifier associated with the given Event
*/
protected def classify(event: E): C
/**
* Publishes the given Event to the given Subscriber
*/
protected def publish(event: E, subscriber: S): Unit
override def subscribe(subscriber: S, to: C): Boolean = bus.subscribe(subscriber, to)
override def unsubscribe(subscriber: S, from: C): Boolean = bus.unsubscribe(subscriber, from)
override def unsubscribe(subscriber: S): Unit = bus.unsubscribe(subscriber)
override def publish(event: E): Unit = bus.publish(event)
}
@Favorwilliams
Copy link

My Pleasure to write you,
My name is Favor Williams,
My email address is
( Favor24@live.com)
Am interested to know
more about you,
Contact me for my
photo and other
important issue via,

Favor24@live.com

@Favorwilliams
Copy link

My Pleasure to write you,
My name is Favor Williams,
My email address is
( Favor24@live.com)
Am interested to know
more about you,
Contact me for my
photo and other
important issue via,

Favor24@live.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment