Skip to content

Commit e0fb95e

Browse files
committed
Add methods to convert from Managed into scala.util.Try
- Some TLC on ExtractedEither - Update apply method to use supressed exceptions (when you can only through/send one exception, might as well, just prefer keeping them in Either until necessary). - Added tests for scala.util.Try conversion.
1 parent 72d1fba commit e0fb95e

File tree

5 files changed

+66
-3
lines changed

5 files changed

+66
-3
lines changed

src/main/scala/resource/AbstractManagedResource.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ private[resource] class DeferredExtractableManagedResource[+A,R](val resource: M
3030

3131
override def opt = either.either.right.toOption
3232

33+
override def tried = scala.util.Try(resource apply translate)
34+
3335
override def equals(that: Any) = that match {
3436
case x : DeferredExtractableManagedResource[A,R] => (x.resource == resource) && (x.translate == translate)
3537
case _ => false

src/main/scala/resource/ExtractableManagedResource.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package resource
1515

1616
import collection.Seq
17+
import util.Try
1718

1819
/**
1920
* This trait represents a resource that has been modified (or will be modified) inside an ARM block in such
@@ -47,6 +48,17 @@ trait ExtractableManagedResource[+A] extends ManagedResource[A] {
4748
* the right hand side will contain the sequence of throwable encountered.
4849
*/
4950
def either: ExtractedEither[Seq[Throwable], A]
50-
}
5151

52-
case class ExtractedEither[+A, +B](either: Either[A, B])
52+
/**
53+
* This method is used to extract the resource being managed.
54+
*
55+
* This allows you to pull information out of the Managed resource, as such, the reosurce will not be "available"
56+
* after this method call.
57+
*
58+
* @returns
59+
* A [[scala.util.Try]] instance, which is [[scala.util.Success]] if there were no exceptions pulling out the
60+
* resource, or a [[scala.util.Failure]] if there were. In the event of multiple failures, they will
61+
* be added to the supressed exception list of the resulting Failure.
62+
*/
63+
def tried: Try[A]
64+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// -----------------------------------------------------------------------------
2+
//
3+
// scala.arm - The Scala Incubator Project
4+
// Copyright (c) 2009 and onwards The Scala Incubator Project. All rights reserved.
5+
//
6+
// The primary distribution site is http://jsuereth.github.com/scala-arm
7+
//
8+
// This software is released under the terms of the Revised BSD License.
9+
// There is NO WARRANTY. See the file LICENSE for the full text.
10+
//
11+
// -----------------------------------------------------------------------------
12+
package resource
13+
14+
15+
/**
16+
* This is a helper class to work around `Either` pattern matching issues.
17+
*/
18+
case class ExtractedEither[+A, +B](either: Either[A, B])

src/main/scala/resource/ManagedResourceOperations.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ import _root_.scala.concurrent.{ ExecutionContext, Future }
2323
trait ManagedResourceOperations[+R] extends ManagedResource[R] { self =>
2424
//TODO - Can we always grab the top exception?
2525
override def acquireAndGet[B](f: R => B): B = apply(f)
26-
override def apply[B](f: R => B): B = acquireFor(f).fold( liste => throw liste.head, x => x)
26+
override def apply[B](f: R => B): B = acquireFor(f).fold(
27+
liste => throw liste reduce { (prev, next) =>
28+
prev.addSuppressed(next)
29+
prev
30+
},
31+
x => x)
2732

2833
override def toTraversable[B](implicit ev: R <:< TraversableOnce[B]): Traversable[B] =
2934
new ManagedTraversable[B,R] {

src/test/scala/resource/TestManagedResource.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,4 +454,30 @@ class TestManagedResource {
454454
assertEquals("Failure result", "KO", result)
455455
assertFalse("Failed to close resource", r.isOpened)
456456
}
457+
458+
459+
@Test
460+
def mustBeSuccessTry() {
461+
val r = new FakeResource()
462+
463+
assertFalse("Failed to begin closed!", r.isOpened)
464+
val result = managed(r).map[String](_ => "OK").tried
465+
assertTrue("Successful result", result.isSuccess)
466+
assertFalse("Failed to close resource", r.isOpened)
467+
}
468+
469+
@Test
470+
def mustBeFailedTry() {
471+
val r = new FakeResource()
472+
473+
assertFalse("Failed to begin closed!", r.isOpened)
474+
475+
val result = managed(r).map[String](_ => {
476+
sys.error("Error")
477+
"OK"
478+
}).tried
479+
480+
assertFalse("Failure result", result.isSuccess)
481+
assertFalse("Failed to close resource", r.isOpened)
482+
}
457483
}

0 commit comments

Comments
 (0)