Skip to content

Commit

Permalink
use collect in ListenersTrait instead of filter and map
Browse files Browse the repository at this point in the history
  • Loading branch information
Henri Kerola committed Jan 2, 2015
1 parent 0c4c9eb commit e3297cf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ trait ListenersTrait[E, L <: Listener] extends mutable.Set[E => Unit] with Liste
iterator.contains(key)
}
def iterator(): Iterator[E => Unit] = {
val list = listeners.asScala.filter(_.isInstanceOf[L]).map(_.asInstanceOf[L].action)
val list = listeners.asScala.collect { case l: L => l.action }
list.iterator.asInstanceOf[Iterator[E => Unit]]
}
def +=(elem: => Unit) = { addListener((e: E) => elem); this }
def +=(elem: E => Unit) = { addListener(elem); this }
def -=(elem: E => Unit) = {
val list = listeners.asScala.filter(_.isInstanceOf[L]).foreach { e =>
if (e.asInstanceOf[L].action == elem) {
removeListener(e.asInstanceOf[L])
val list = listeners.asScala.collect { case l: L =>
if (l.action == elem) {
removeListener(l)
this
}
}
Expand Down
36 changes: 36 additions & 0 deletions addon/src/test/scala/vaadin/scala/tests/ListenersTraitTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package vaadin.scala.tests

import com.vaadin.ui.Button.{ClickEvent, ClickListener}
import vaadin.scala._

/**
*
* @author Henri Kerola / Vaadin
*/
class ListenersTraitTests extends ScaladinTestSuite {

test("iterator") {
val button = new Button

button.clickListeners += { e => }

button.p.addClickListener(new ClickListener {
override def buttonClick(clickEvent: ClickEvent): Unit = {}
})

assert(1 == button.clickListeners.size)
assert(2 == button.p.getListeners(classOf[com.vaadin.ui.Button.ClickEvent]).size)
}

test("-=") {
val button = new Button

val clickListener = { e: Button.ClickEvent => }
button.clickListeners += clickListener
assert(1 == button.clickListeners.size)

button.clickListeners -= clickListener
assert(0 == button.clickListeners.size)
}

}

0 comments on commit e3297cf

Please sign in to comment.