Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 4f536d2

Browse files
committed
API rework
1 parent 6ba7b59 commit 4f536d2

File tree

17 files changed

+164
-213
lines changed

17 files changed

+164
-213
lines changed

src/main/scala/org/codeoverflow/chatoverflow/requirement/EventInputImpl.scala

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.codeoverflow.chatoverflow.requirement.impl
2+
3+
import java.util.function.Consumer
4+
5+
import org.codeoverflow.chatoverflow.api.io.event.Event
6+
7+
private[impl] case class EventHandler[T <: Event](consumer: Consumer[T], clazz: Class[T])
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.codeoverflow.chatoverflow.requirement.impl
2+
3+
import java.util.function.Consumer
4+
5+
import org.codeoverflow.chatoverflow.api.io.event.Event
6+
import org.codeoverflow.chatoverflow.api.io.input.event.EventInput
7+
import org.codeoverflow.chatoverflow.connector.Connector
8+
9+
import scala.collection.mutable.ListBuffer
10+
import scala.reflect.ClassTag
11+
12+
/**
13+
* Default implementation for all inputs that provide events.
14+
*
15+
* The integrated event registry allows registering new Event handlers at any time.
16+
* @tparam T the event interface that all events for this EventInput share
17+
* @tparam C the connector to which this input belongs
18+
*/
19+
abstract class EventInputImpl[T <: Event, C <: Connector](implicit ctc: ClassTag[C]) extends InputImpl[C] with EventInput[T] {
20+
21+
protected val handlers: ListBuffer[EventHandler[_ <: T]] = ListBuffer[EventHandler[_ <: T]]()
22+
23+
override def registerEventHandler[S <: T](eventHandler: Consumer[S], eventClass: Class[S]): Unit = {
24+
handlers += EventHandler[S](eventHandler, eventClass)
25+
}
26+
27+
protected def call[S <: T](event: S)(implicit cts: ClassTag[S]): Unit = {
28+
handlers.filter(handler => handler.clazz == cts.runtimeClass)
29+
.foreach(handler => handler.consumer.asInstanceOf[Consumer[S]].accept(event))
30+
}
31+
}

src/main/scala/org/codeoverflow/chatoverflow/requirement/InputImpl.scala renamed to src/main/scala/org/codeoverflow/chatoverflow/requirement/impl/InputImpl.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package org.codeoverflow.chatoverflow.requirement
1+
package org.codeoverflow.chatoverflow.requirement.impl
22

33
import org.codeoverflow.chatoverflow.WithLogger
44
import org.codeoverflow.chatoverflow.api.io.input.Input
55
import org.codeoverflow.chatoverflow.connector.Connector
6+
import org.codeoverflow.chatoverflow.requirement.Connection
67

78
import scala.reflect.ClassTag
89

src/main/scala/org/codeoverflow/chatoverflow/requirement/OutputImpl.scala renamed to src/main/scala/org/codeoverflow/chatoverflow/requirement/impl/OutputImpl.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package org.codeoverflow.chatoverflow.requirement
1+
package org.codeoverflow.chatoverflow.requirement.impl
22

33
import org.codeoverflow.chatoverflow.WithLogger
44
import org.codeoverflow.chatoverflow.api.io.output.Output
55
import org.codeoverflow.chatoverflow.connector.Connector
6+
import org.codeoverflow.chatoverflow.requirement.Connection
67

78
import scala.reflect.ClassTag
89

0 commit comments

Comments
 (0)