-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3916 from mliarakos/feature/javalib-consumer
Implement `java.util.function.Consumer`.
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Scala.js (https://www.scala-js.org/) | ||
* | ||
* Copyright EPFL. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (https://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package java.util.function | ||
|
||
import scala.scalajs.js.annotation.JavaDefaultMethod | ||
|
||
@FunctionalInterface | ||
trait Consumer[T] { self => | ||
def accept(t: T): Unit | ||
|
||
@JavaDefaultMethod | ||
def andThen(after: Consumer[_ >: T]): Consumer[T] = { | ||
new Consumer[T] { | ||
def accept(t: T): Unit = { | ||
self.accept(t) | ||
after.accept(t) | ||
} | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...uite/shared/src/test/scala/org/scalajs/testsuite/javalib/util/function/ConsumerTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Scala.js (https://www.scala-js.org/) | ||
* | ||
* Copyright EPFL. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (https://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package org.scalajs.testsuite.javalib.util.function | ||
|
||
import java.util.function.Consumer | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
|
||
import org.scalajs.testsuite.utils.AssertThrows._ | ||
|
||
class ConsumerTest { | ||
import ConsumerTest._ | ||
|
||
@Test def accept(): Unit = { | ||
// Side-effects | ||
var current: Int = 0 | ||
val add = makeConsumer[Int](num => current += num) | ||
|
||
add.accept(1) | ||
assertTrue(current == 1) | ||
|
||
add.accept(2) | ||
assertTrue(current == 3) | ||
} | ||
|
||
@Test def andThen(): Unit = { | ||
// Side-effects | ||
var current: Int = 0 | ||
val add = makeConsumer[Int](num => current += num) | ||
val multiply = makeConsumer[Int](num => current *= num) | ||
val addAndMultiply = add.andThen(multiply) | ||
|
||
addAndMultiply.accept(2) | ||
assertTrue(current == 4) | ||
|
||
addAndMultiply.accept(3) | ||
assertTrue(current == 21) | ||
|
||
// Sequential operations | ||
val throwingConsumer = | ||
makeConsumer[Any](x => throw new ThrowingConsumerException(x)) | ||
val dontCallConsumer = | ||
makeConsumer[Any](x => throw new AssertionError(s"dontCallConsumer.accept($x)")) | ||
|
||
assertThrows(classOf[ThrowingConsumerException], | ||
throwingConsumer.andThen(dontCallConsumer).accept(0)) | ||
|
||
assertThrows(classOf[ThrowingConsumerException], | ||
add.andThen(throwingConsumer).accept(1)) | ||
assertTrue(current == 22) | ||
} | ||
} | ||
|
||
object ConsumerTest { | ||
final class ThrowingConsumerException(x: Any) | ||
extends Exception(s"throwing consumer called with $x") | ||
|
||
def makeConsumer[T](f: T => Unit): Consumer[T] = { | ||
new Consumer[T] { | ||
def accept(t: T): Unit = f(t) | ||
} | ||
} | ||
} |