Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: nummulus/boite
base: e7b562fe2c
...
head fork: nummulus/boite
compare: 2c0fe42043
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
108 src/main/scala/com/nummulus/boite/Boite.scala
@@ -1,108 +0,0 @@
-package com.nummulus
-package boite
-
-sealed abstract class Boite[+A] {
- self =>
-
- /**
- * Returns {@code true} if boite contains no value (Empty or Failure),
- * {@code false} otherwise.
- */
- def isEmpty: Boolean
-
- /**
- * Returns {@code true} if boite contains a value, {@code false}
- */
- def isDefined: Boolean = !isEmpty
-
- /**
- * Returns the value of the boite if it's full, else the specified default.
- */
- def openOr[B >: A](default: => B): B = default
-
- /**
- * Applies a function to the value of the boite if it's full and returns a
- * new boite containing the result. Returns empty otherwise.
- * <p>
- * Differs from flatMap in that the given function is not expected to wrap
- * the result in a boite.
- *
- * @see flatMap
- */
- def map[B](f: A => B): Boite[B] = Empty
-
- /**
- * Applies a function to the value of the boite if it's full and returns a
- * new boite containing the result. Returns empty otherwise.
- * <p>
- * Differs from map in that the given function is expected to return a boite.
- *
- * @see map
- */
- def flatMap[B](f: A => Boite[B]): Boite[B] = Empty
-
- /**
- * Applies a function to the value of boite if it's full, otherwise do
- * nothing.
- */
- def foreach[U](f: A => U): Unit = {}
-
- /**
- * Returns a List of one element if boite is full or an empty list
- * otherwise.
- */
- def toList: List[A] = List()
-
- /**
- * Returns {@code true} if both objects are equal based on the contents of
- * this boite. For Failures, equality is based on equivalence of failure
- * causes.
- */
- override def equals(other: Any): Boolean = (this, other) match {
- case (Full(x), Full(y)) => x == y
- case (Full(x), y) => x == y
- case (x, y: AnyRef) => x eq y
- case _ => false
- }
-}
-
-final case class Full[+A](value: A) extends Boite[A] {
- def isEmpty = false
-
- override def openOr[B >: A](default: => B): B = value
-
- override def map[B](f: A => B): Boite[B] = Full(f(value))
-
- override def flatMap[B](f: A => Boite[B]): Boite[B] = f(value)
-
- override def foreach[U](f: A => U): Unit = f(value)
-
- override def toList: List[A] = List(value)
-}
-
-case object Empty extends BoiteVide
-
-sealed abstract class BoiteVide extends Boite[Nothing] {
- def isEmpty = true
-
- override def openOr[B >: Nothing](default: => B): B = default
-}
-
-object Failure {
- def apply(message: String) = new Failure(message, Empty, Empty)
- def apply(throwable: Throwable) = new Failure(throwable.getMessage, Full(throwable), Empty)
-}
-
-sealed case class Failure(message: String, exception: Boite[Throwable], chain: Boite[Failure]) extends BoiteVide {
- type A = Nothing
-
- override def map[B](f: A => B): Boite[B] = this
-
- override def flatMap[B](f: A => Boite[B]): Boite[B] = this
-
- override def equals(other: Any): Boolean = (this, other) match {
- case (Failure(x, y, z), Failure(a, b, c)) => (x, y, z) == (a, b, c)
- case (x, y: AnyRef) => x eq y
- case _ => false
- }
-}
View
109 src/main/scala/com/nummulus/boite/Box.scala
@@ -0,0 +1,109 @@
+package com.nummulus
+package boite
+
+sealed abstract class Box[+A] {
+ self =>
+
+ /**
+ * Returns {@code true} if the box contains no value (Empty or Failure),
+ * {@code false} otherwise.
+ */
+ def isEmpty: Boolean
+
+ /**
+ * Returns {@code true} if the box contains a value, {@code false}
+ */
+ def isDefined: Boolean = !isEmpty
+
+ /**
+ * Returns the value of the box if it's full, else the specified default.
+ */
+ def getOrElse[B >: A](default: => B): B = default
+
+ /**
+ * Applies a function to the value of the box if it's full and returns a
+ * new box containing the result. Returns empty otherwise.
+ * <p>
+ * Differs from flatMap in that the given function is not expected to wrap
+ * the result in a box.
+ *
+ * @see flatMap
+ */
+ def map[B](f: A => B): Box[B] = Empty
+
+ /**
+ * Applies a function to the value of the box if it's full and returns a
+ * new box containing the result. Returns empty otherwise.
+ * <p>
+ * Differs from map in that the given function is expected to return a box.
+ *
+ * @see map
+ */
+ def flatMap[B](f: A => Box[B]): Box[B] = Empty
+
+ /**
+ * Applies a function to the value of the box if it's full, otherwise do
+ * nothing.
+ */
+ def foreach[U](f: A => U): Unit = {}
+
+ /**
+ * Returns a List of one element if the box is full or an empty list
+ * otherwise.
+ */
+ def toList: List[A] = List()
+
+ /**
+ * Returns {@code true} if both objects are equal based on the contents of
+ * the box. For failures, equality is based on equivalence of failure
+ * causes.
+ */
+ override def equals(other: Any): Boolean = (this, other) match {
+ case (Full(x), Full(y)) => x == y
+ case (Full(x), y) => x == y
+ case (x, y: AnyRef) => x eq y
+ case _ => false
+ }
+}
+
+final case class Full[+A](value: A) extends Box[A] {
+ def isEmpty = false
+
+ override def getOrElse[B >: A](default: => B): B = value
+
+ override def map[B](f: A => B): Box[B] = Full(f(value))
+
+ override def flatMap[B](f: A => Box[B]): Box[B] = f(value)
+
+ override def foreach[U](f: A => U): Unit = f(value)
+
+ override def toList: List[A] = List(value)
+}
+
+case object Empty extends BoiteVide
+
+sealed abstract class BoiteVide extends Box[Nothing] {
+ def isEmpty = true
+
+ override def getOrElse[B >: Nothing](default: => B): B = default
+}
+
+object Failure {
+ def apply(message: String) = new Failure(message, Empty)
+ def apply(throwable: Throwable) = new Failure(throwable.getMessage, Full(throwable))
+ def apply(message: String, throwable: Throwable) = new Failure(message, Full(throwable))
+}
+
+sealed case class Failure(message: String, exception: Box[Throwable]) extends BoiteVide {
+ type A = Nothing
+
+ override def map[B](f: A => B): Box[B] = this
+
+ override def flatMap[B](f: A => Box[B]): Box[B] = this
+
+ override def equals(other: Any): Boolean = (this, other) match {
+ case (Failure(x, y), Failure(a, b)) => (x, y) == (a, b)
+ case (x, y: AnyRef) => x eq y
+ case _ => false
+ }
+}
View
4 src/test/scala/com/nummulus/boite/EmptyTest.scala
@@ -18,10 +18,10 @@ class EmptyTest extends FlatSpec with ShouldMatchers with MockitoSugar {
}
"Open or" should "return the default value if empty" in {
- Empty.openOr(404) should equal (404)
+ Empty.getOrElse(404) should equal (404)
}
- "Two empty boites" should "be equal" in {
+ "Two empty boxes" should "be equal" in {
Empty == Empty should be (true)
}
View
12 src/test/scala/com/nummulus/boite/FailureTest.scala
@@ -14,7 +14,7 @@ class FailureTest extends FlatSpec with ShouldMatchers with MockitoSugar {
val Throwable = new Exception
val FileNotFoundException = new FileNotFoundException(Message)
- val failure = new Failure(Message, Full(Throwable), null)
+ val failure = new Failure(Message, Full(Throwable))
"Failure" should "be empty" in {
failure.isEmpty should equal (true)
@@ -25,17 +25,17 @@ class FailureTest extends FlatSpec with ShouldMatchers with MockitoSugar {
}
"Open or" should "return the default value on failure" in {
- failure.openOr(404) should equal (404)
+ failure.getOrElse(404) should equal (404)
}
"Two failures with the same message and exception" should "be equal" in {
- failure == new Failure(Message, Full(Throwable), null) should be (true)
+ failure == new Failure(Message, Full(Throwable)) should be (true)
Failure(Message) == Failure(Message) should be (true)
}
"Two failures with the same message and different exceptions" should "not be equal" in {
- failure == new Failure(Message, Full(FileNotFoundException), null) should be (false)
+ failure == new Failure(Message, Full(FileNotFoundException)) should be (false)
Failure(Message) == Failure(Message + "!") should be (false)
}
@@ -62,4 +62,8 @@ class FailureTest extends FlatSpec with ShouldMatchers with MockitoSugar {
"To list" should "return an empty list" in {
Empty.toList should equal (List())
}
+
+ "Failure.apply(message, throwable)" should "be equal to calling new" in {
+ Failure(Message, Throwable) should equal (failure)
+ }
}
View
30 src/test/scala/com/nummulus/boite/FullTest.scala
@@ -11,48 +11,48 @@ import org.scalatest.mock.MockitoSugar
class FullTest extends FlatSpec with ShouldMatchers with MockitoSugar {
val JacquesBrel = "Jacques Brel"
- val boite = Full(JacquesBrel)
+ val box = Full(JacquesBrel)
"Full" should "not be empty" in {
- boite.isEmpty should equal (false)
+ box.isEmpty should equal (false)
}
"Full" should "be defined" in {
- boite.isDefined should equal (true)
+ box.isDefined should equal (true)
}
"Open or" should "return the value if full" in {
- boite.openOr("Edith Piaf") should equal (JacquesBrel)
+ box.getOrElse("Edith Piaf") should equal (JacquesBrel)
}
- "Two full boites with the same content" should "be equal" in {
- boite == Full(JacquesBrel) should be (true)
+ "Two full boxes with the same content" should "be equal" in {
+ box == Full(JacquesBrel) should be (true)
}
"Full and its raw value" should "be equal" in {
- boite == JacquesBrel should be (true)
+ box == JacquesBrel should be (true)
}
"Full" should "not be Empty" in {
- boite == Empty should be (false)
+ box == Empty should be (false)
}
- "Map the String.length function" should "return a full boite of type Int" in {
- boite.map(s => s.length) should equal (Full(JacquesBrel.length))
+ "Map the String.length function" should "return a full box of type Int" in {
+ box.map(s => s.length) should equal (Full(JacquesBrel.length))
}
- "Flat map the String.length function" should "return a full boite of type Int" in {
- boite.flatMap(s => Full(s.length)) should equal (Full(JacquesBrel.length))
+ "Flat map the String.length function" should "return a full box of type Int" in {
+ box.flatMap(s => Full(s.length)) should equal (Full(JacquesBrel.length))
}
- "Foreach" should "be called on a full boite" in {
+ "Foreach" should "be called on a full box" in {
val test = mock[DummyTrait]
- boite.foreach(s => test.calculate(s))
+ box.foreach(s => test.calculate(s))
verify (test) calculate(JacquesBrel)
}
"To list" should "return a list with one entry" in {
- boite.toList should equal (List(JacquesBrel))
+ box.toList should equal (List(JacquesBrel))
}
}

No commit comments for this range

Something went wrong with that request. Please try again.