1
1
package io .github .ragazoor
2
2
3
- import io .github .ragazoor .IOUtils .{ failedFailure , zipWithTuple2Fun }
3
+ import io .github .ragazoor .AttemptUtils .{ failedFailure , zipWithTuple2Fun }
4
4
5
5
import scala .concurrent .ExecutionContext .parasitic
6
6
import scala .concurrent .duration .Duration
7
- import scala .concurrent .{ Awaitable , CanAwait , ExecutionContext , Future => StdFuture , TimeoutException }
7
+ import scala .concurrent .{Awaitable , CanAwait , ExecutionContext , TimeoutException , Future => StdFuture }
8
8
import scala .util .control .NonFatal
9
- import scala .util .{ Failure , Success , Try }
9
+ import scala .util .{Failure , Success , Try }
10
10
11
- sealed trait IO [+ E <: Throwable , + A ] extends Awaitable [A ] {
11
+ sealed trait Attempt [+ E <: Throwable , + A ] extends Awaitable [A ] {
12
12
self =>
13
13
def toFuture : StdFuture [A ]
14
14
val isFatal : Boolean
15
15
16
16
def value : Option [Try [A ]] = self.toFuture.value
17
17
18
- def map [B ](f : A => B )(implicit ec : ExecutionContext ): IO [E , B ] =
19
- IO (self.toFuture.transform(_ map f), isFatal)
18
+ def map [B ](f : A => B )(implicit ec : ExecutionContext ): Attempt [E , B ] =
19
+ Attempt (self.toFuture.transform(_ map f), isFatal)
20
20
21
- def mapTry [B ](f : A => Try [B ])(implicit ec : ExecutionContext ): IO [Throwable , B ] =
22
- IO (self.toFuture.transform(_ flatMap f), isFatal)
21
+ def mapTry [B ](f : A => Try [B ])(implicit ec : ExecutionContext ): Attempt [Throwable , B ] =
22
+ Attempt (self.toFuture.transform(_ flatMap f), isFatal)
23
23
24
- def mapEither [E2 >: E <: Throwable , B ](f : A => Either [E2 , B ])(implicit ec : ExecutionContext ): IO [E2 , B ] =
25
- IO (self.toFuture.transform(_ flatMap (f(_).toTry)), isFatal)
24
+ def mapEither [E2 >: E <: Throwable , B ](f : A => Either [E2 , B ])(implicit ec : ExecutionContext ): Attempt [E2 , B ] =
25
+ Attempt (self.toFuture.transform(_ flatMap (f(_).toTry)), isFatal)
26
26
27
- def flatMap [E2 >: E <: Throwable , B ](f : A => IO [E2 , B ])(implicit ec : ExecutionContext ): IO [E2 , B ] =
28
- IO (self.toFuture.flatMap(f(_).toFuture), isFatal)
27
+ def flatMap [E2 >: E <: Throwable , B ](f : A => Attempt [E2 , B ])(implicit ec : ExecutionContext ): Attempt [E2 , B ] =
28
+ Attempt (self.toFuture.flatMap(f(_).toFuture), isFatal)
29
29
30
- def flatten [E2 >: E <: Throwable , B ](implicit ev : A <:< IO [E2 , B ]): IO [E2 , B ] =
30
+ def flatten [E2 >: E <: Throwable , B ](implicit ev : A <:< Attempt [E2 , B ]): Attempt [E2 , B ] =
31
31
flatMap(ev)(parasitic)
32
32
33
- def mapError [E2 <: Throwable ](f : E => E2 )(implicit ec : ExecutionContext ): IO [E2 , A ] = {
33
+ def mapError [E2 <: Throwable ](f : E => E2 )(implicit ec : ExecutionContext ): Attempt [E2 , A ] = {
34
34
var isFutureFatal = isFatal
35
35
val transformedFuture = self.toFuture.transform {
36
36
case Failure (e) if NonFatal (e) && ! isFatal => Failure (f(e.asInstanceOf [E ]))
@@ -39,20 +39,20 @@ sealed trait IO[+E <: Throwable, +A] extends Awaitable[A] {
39
39
Failure (e)
40
40
case success => success
41
41
}
42
- IO [E2 , A ](transformedFuture, isFutureFatal)
42
+ Attempt [E2 , A ](transformedFuture, isFutureFatal)
43
43
}
44
44
45
- def zip [E2 >: E <: Throwable , B ](that : IO [E2 , B ]): IO [E2 , (A , B )] =
45
+ def zip [E2 >: E <: Throwable , B ](that : Attempt [E2 , B ]): Attempt [E2 , (A , B )] =
46
46
zipWith(that)(zipWithTuple2Fun)(parasitic)
47
47
48
- def zipWith [E2 >: E <: Throwable , U , R ](that : IO [E2 , U ])(f : (A , U ) => R )(implicit
49
- ec : ExecutionContext
50
- ): IO [E2 , R ] =
51
- IO (self.toFuture.zipWith(that.toFuture)(f), isFatal || that.isFatal)
48
+ def zipWith [E2 >: E <: Throwable , U , R ](that : Attempt [E2 , U ])(f : (A , U ) => R )(implicit
49
+ ec : ExecutionContext
50
+ ): Attempt [E2 , R ] =
51
+ Attempt (self.toFuture.zipWith(that.toFuture)(f), isFatal || that.isFatal)
52
52
53
- def catchAll [E2 >: E <: Throwable , A2 >: A ](f : E => IO [E2 , A2 ])(implicit
54
- ec : ExecutionContext
55
- ): IO [E2 , A2 ] = {
53
+ def catchAll [E2 >: E <: Throwable , A2 >: A ](f : E => Attempt [E2 , A2 ])(implicit
54
+ ec : ExecutionContext
55
+ ): Attempt [E2 , A2 ] = {
56
56
var isFutureFatal = isFatal
57
57
val transformedFuture = self.toFuture.transformWith {
58
58
case Failure (e) if NonFatal (e) && ! isFatal => f(e.asInstanceOf [E ]).toFuture
@@ -61,19 +61,19 @@ sealed trait IO[+E <: Throwable, +A] extends Awaitable[A] {
61
61
self.toFuture
62
62
case _ => self.toFuture
63
63
}
64
- IO [E2 , A2 ](transformedFuture, isFutureFatal)
64
+ Attempt [E2 , A2 ](transformedFuture, isFutureFatal)
65
65
}
66
66
67
- def catchSome [E2 >: E <: Throwable , A2 >: A ](pf : PartialFunction [E , IO [E2 , A2 ]])(implicit
68
- ec : ExecutionContext
69
- ): IO [E2 , A2 ] = {
67
+ def catchSome [E2 >: E <: Throwable , A2 >: A ](pf : PartialFunction [E , Attempt [E2 , A2 ]])(implicit
68
+ ec : ExecutionContext
69
+ ): Attempt [E2 , A2 ] = {
70
70
val transformedFuture = self.toFuture.transformWith {
71
71
case Failure (e) if NonFatal (e) && pf.isDefinedAt(e.asInstanceOf [E ]) && ! isFatal =>
72
72
pf(e.asInstanceOf [E ]).toFuture
73
73
case _ =>
74
74
self.toFuture
75
75
}
76
- IO [E2 , A2 ](transformedFuture, isFatal)
76
+ Attempt [E2 , A2 ](transformedFuture, isFatal)
77
77
}
78
78
79
79
private final def failedFun [B ](v : Try [B ]): Try [E ] =
@@ -83,8 +83,8 @@ sealed trait IO[+E <: Throwable, +A] extends Awaitable[A] {
83
83
case Success (_) => failedFailure
84
84
}
85
85
86
- def failed : IO [NoSuchElementException , E ] =
87
- transform(failedFun)(parasitic).asInstanceOf [IO [NoSuchElementException , E ]]
86
+ def failed : Attempt [NoSuchElementException , E ] =
87
+ transform(failedFun)(parasitic).asInstanceOf [Attempt [NoSuchElementException , E ]]
88
88
89
89
def foreach [U ](f : A => U )(implicit executor : ExecutionContext ): Unit =
90
90
onComplete(_ foreach f)
@@ -94,13 +94,13 @@ sealed trait IO[+E <: Throwable, +A] extends Awaitable[A] {
94
94
95
95
def isCompleted : Boolean = self.toFuture.isCompleted
96
96
97
- def transform [B ](f : Try [A ] => Try [B ])(implicit executor : ExecutionContext ): IO [Throwable , B ] =
98
- IO (self.toFuture.transform(f), isFatal)
97
+ def transform [B ](f : Try [A ] => Try [B ])(implicit executor : ExecutionContext ): Attempt [Throwable , B ] =
98
+ Attempt (self.toFuture.transform(f), isFatal)
99
99
100
- def transformWith [E2 >: E <: Throwable , B ](f : Try [A ] => IO [E2 , B ])(implicit
101
- executor : ExecutionContext
102
- ): IO [E2 , B ] =
103
- IO (self.toFuture.transformWith(f(_).toFuture), isFatal)
100
+ def transformWith [E2 >: E <: Throwable , B ](f : Try [A ] => Attempt [E2 , B ])(implicit
101
+ executor : ExecutionContext
102
+ ): Attempt [E2 , B ] =
103
+ Attempt (self.toFuture.transformWith(f(_).toFuture), isFatal)
104
104
105
105
@ throws(classOf [TimeoutException ])
106
106
@ throws(classOf [InterruptedException ])
@@ -114,61 +114,61 @@ sealed trait IO[+E <: Throwable, +A] extends Awaitable[A] {
114
114
def result (atMost : Duration )(implicit permit : CanAwait ): A =
115
115
self.toFuture.result(atMost)
116
116
117
- def recover [B >: A ](pf : PartialFunction [E , B ])(implicit executor : ExecutionContext ): IO [E , B ] =
117
+ def recover [B >: A ](pf : PartialFunction [E , B ])(implicit executor : ExecutionContext ): Attempt [E , B ] =
118
118
transform(_.recover(pf.asInstanceOf [PartialFunction [Throwable , B ]]))
119
- .asInstanceOf [IO [E , B ]]
119
+ .asInstanceOf [Attempt [E , B ]]
120
120
}
121
121
122
- object IO {
123
- def unapply [E <: Throwable , A ](result : IO [E , A ]): Option [(StdFuture [A ], Boolean )] =
122
+ object Attempt {
123
+ def unapply [E <: Throwable , A ](result : Attempt [E , A ]): Option [(StdFuture [A ], Boolean )] =
124
124
Some ((result.toFuture, result.isFatal))
125
125
126
- private [ragazoor] final def apply [E <: Throwable , A ](future : StdFuture [A ], fatal : Boolean ): IO [E , A ] =
127
- new IO [E , A ] {
126
+ private [ragazoor] final def apply [E <: Throwable , A ](future : StdFuture [A ], fatal : Boolean ): Attempt [E , A ] =
127
+ new Attempt [E , A ] {
128
128
override def toFuture : StdFuture [A ] = future
129
129
override val isFatal : Boolean = fatal
130
130
}
131
131
132
- final def apply [A ](body : => A )(implicit ec : ExecutionContext ): IO [Throwable , A ] =
133
- IO [Throwable , A ](StdFuture (body), fatal = false )
132
+ final def apply [A ](body : => A )(implicit ec : ExecutionContext ): Attempt [Throwable , A ] =
133
+ Attempt [Throwable , A ](StdFuture (body), fatal = false )
134
134
135
- final def fromFuture [A ](future : StdFuture [A ]): IO [Throwable , A ] =
136
- IO (future, fatal = false )
135
+ final def fromFuture [A ](future : StdFuture [A ]): Attempt [Throwable , A ] =
136
+ Attempt (future, fatal = false )
137
137
138
- final def fromEither [E <: Throwable , A ](either : Either [E , A ]): IO [E , A ] =
139
- IO (StdFuture .fromTry(either.toTry), fatal = false )
138
+ final def fromEither [E <: Throwable , A ](either : Either [E , A ]): Attempt [E , A ] =
139
+ Attempt (StdFuture .fromTry(either.toTry), fatal = false )
140
140
141
- final def fromTry [A ](result : Try [A ]): IO [Throwable , A ] =
142
- IO [Throwable , A ](StdFuture .fromTry(result), fatal = false )
141
+ final def fromTry [A ](result : Try [A ]): Attempt [Throwable , A ] =
142
+ Attempt [Throwable , A ](StdFuture .fromTry(result), fatal = false )
143
143
144
- final def successful [A ](result : A ): IO [Nothing , A ] = {
144
+ final def successful [A ](result : A ): Attempt [Nothing , A ] = {
145
145
val future = StdFuture .successful(result)
146
- new IO [Nothing , A ] {
146
+ new Attempt [Nothing , A ] {
147
147
override def toFuture : StdFuture [A ] = future
148
148
override val isFatal : Boolean = false
149
149
}
150
150
}
151
151
152
- final def failed [E <: Exception ](exception : E ): IO [E , Nothing ] = {
152
+ final def failed [E <: Exception ](exception : E ): Attempt [E , Nothing ] = {
153
153
val future = StdFuture .failed(exception)
154
- new IO [E , Nothing ] {
154
+ new Attempt [E , Nothing ] {
155
155
override def toFuture : StdFuture [Nothing ] = future
156
156
override val isFatal : Boolean = false
157
157
}
158
158
}
159
159
160
- final def fatal (exception : Exception ): IO [Nothing , Nothing ] = {
160
+ final def fatal (exception : Exception ): Attempt [Nothing , Nothing ] = {
161
161
val future = StdFuture .failed(exception)
162
- new IO [Nothing , Nothing ] {
162
+ new Attempt [Nothing , Nothing ] {
163
163
override def toFuture : StdFuture [Nothing ] = future
164
164
override val isFatal : Boolean = true
165
165
}
166
166
}
167
167
168
- final def sequence [E <: Throwable , A ](results : Seq [IO [E , A ]])(implicit
169
- ec : ExecutionContext
170
- ): IO [E , Seq [A ]] =
171
- IO (StdFuture .sequence(results.map(_.toFuture)), fatal = false )
168
+ final def sequence [E <: Throwable , A ](results : Seq [Attempt [E , A ]])(implicit
169
+ ec : ExecutionContext
170
+ ): Attempt [E , Seq [A ]] =
171
+ Attempt (StdFuture .sequence(results.map(_.toFuture)), fatal = false )
172
172
173
- val unit = IO .successful(())
173
+ val unit = Attempt .successful(())
174
174
}
0 commit comments