Skip to content

Commit 710c80f

Browse files
author
Mark Perry
committed
Added support for lambdas that throw exceptions
1 parent 9c1b446 commit 710c80f

24 files changed

+304
-91
lines changed

core/build.gradle

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11

2+
3+
buildscript {
4+
repositories {
5+
mavenCentral()
6+
}
7+
8+
dependencies {
9+
classpath 'me.tatarka:gradle-retrolambda:1.3.1'
10+
}
11+
}
12+
213
apply plugin: 'java'
314
apply plugin: 'maven'
415
//apply plugin: 'signing'
16+
apply plugin: 'retrolambda'
517

618
defaultTasks 'build'
719

@@ -23,19 +35,25 @@ dependencies {
2335
}
2436

2537

26-
task javadocJar(type: Jar, dependsOn: javadoc) {
27-
classifier = 'javadoc'
28-
from "build/docs/javadoc"
38+
retrolambda {
39+
jdk System.getenv("JAVA8_HOME")
40+
oldJdk System.getenv("JAVA7_HOME")
41+
javaVersion JavaVersion.VERSION_1_7
2942
}
3043

44+
//task javadocJar(type: Jar, dependsOn: javadoc) {
45+
// classifier = 'javadoc'
46+
// from "build/docs/javadoc"
47+
//}
48+
3149
task sourcesJar(type: Jar) {
3250
from sourceSets.main.allSource
3351
classifier = 'sources'
3452
}
3553

3654
artifacts {
3755
archives jar
38-
archives javadocJar
56+
// archives javadocJar
3957
archives sourcesJar
4058
}
4159

core/src/main/java/fj/F1Functions.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,12 +824,18 @@ static public <A, B> ArrayList<B> mapJ(final F<A, B> f, final ArrayList<A> as) {
824824
return new ArrayList<B>(iterableStream(as).map(f).toCollection());
825825
}
826826

827-
static public <A, B> F<A, Try<B>> toF1(final Try1<A, B> t) {
827+
/**
828+
* Promotes the TryCatch1 to a Validation that returns an Exception on the failure side and its result on the success side.
829+
*
830+
* @param t A TryCatch1 to promote
831+
* @return A Validation with an Exception on the failure side and its result on the success side.
832+
*/
833+
static public <A, B> F<A, Validation<Exception, B>> toF1(final TryCatch1<A, B> t) {
828834
return a -> {
829835
try {
830-
return Try.trySuccess(t.f(a));
836+
return Validation.success(t.f(a));
831837
} catch (Exception e) {
832-
return Try.<B>tryFail(e);
838+
return Validation.fail(e);
833839
}
834840
};
835841
}

core/src/main/java/fj/F2Functions.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,18 @@ public P3<Stream<Tree<C>>, C, Stream<Tree<C>>> f(final P3<Stream<Tree<A>>, A, St
347347
}
348348

349349

350-
static public <A, B, C> F2<A, B, Try<C>> toF2(final Try2<A, B, C> t) {
350+
/**
351+
* Promotes the TryCatch2 to a Validation that returns an Exception on the failure side and its result on the success side.
352+
*
353+
* @param t A TryCatch2 to promote
354+
* @return A Validation with an Exception on the failure side and its result on the success side.
355+
*/
356+
static public <A, B, C> F2<A, B, Validation<Exception, C>> toF2(final TryCatch2<A, B, C> t) {
351357
return (a, b) -> {
352358
try {
353-
return Try.trySuccess(t.f(a, b));
359+
return Validation.success(t.f(a, b));
354360
} catch (Exception e) {
355-
return Try.<C>tryFail(e);
361+
return Validation.fail(e);
356362
}
357363
};
358364
}

core/src/main/java/fj/F3Functions.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
package fj;
22

3+
import fj.data.Validation;
4+
5+
import static fj.data.Validation.fail;
6+
import static fj.data.Validation.success;
7+
38
/**
49
* Created by MarkPerry on 6/04/2014.
510
*/
611
public class F3Functions {
712

8-
9-
10-
static public <A, B, C, D> F3<A, B, C, Try<D>> toF3(final Try3<A, B, C, D> t) {
13+
/**
14+
* Promotes the TryCatch3 to a Validation that returns an Exception on the failure side and its result on the success side.
15+
*
16+
* @param t A TryCatch3 to promote
17+
* @return A Validation with an Exception on the failure side and its result on the success side.
18+
*/
19+
static public <A, B, C, D> F3<A, B, C, Validation<Exception, D>> toF3(final TryCatch3<A, B, C, D> t) {
1120
return (a, b, c) -> {
1221
try {
13-
return Try.trySuccess(t.f(a, b, c));
22+
return success(t.f(a, b, c));
1423
} catch (Exception e) {
15-
return Try.<D>tryFail(e);
24+
return fail(e);
1625
}
1726
};
1827
}

core/src/main/java/fj/F4Functions.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
package fj;
22

3+
import fj.data.Validation;
4+
35
/**
46
* Created by MarkPerry on 6/04/2014.
57
*/
68
public class F4Functions {
79

810

9-
static public <A, B, C, D, E> F4<A, B, C, D, Try<E>> toF4(final Try4<A, B, C, D, E> t) {
11+
/**
12+
* Promotes the TryCatch4 to a Validation that returns an Exception on the failure side and its result on the success side.
13+
*
14+
* @param t A TryCatch4 to promote
15+
* @return A Validation with an Exception on the failure side and its result on the success side.
16+
*/
17+
static public <A, B, C, D, E> F4<A, B, C, D, Validation<Exception, E>> toF4(final TryCatch4<A, B, C, D, E> t) {
1018
return (a, b, c, d) -> {
1119
try {
12-
return Try.trySuccess(t.f(a, b, c, d));
13-
} catch (Exception e) {
14-
return Try.<E>tryFail(e);
20+
return Validation.success(t.f(a, b, c, d));
21+
} catch (Exception ex) {
22+
return Validation.fail(ex);
1523
}
1624
};
1725
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
package fj;
22

3+
import fj.data.Validation;
4+
35
/**
46
* Created by MarkPerry on 6/04/2014.
57
*/
68
public class F5Functions {
9+
10+
/**
11+
* Promotes the TryCatch5 to a Validation that returns an Exception on the failure side and its result on the success side.
12+
*
13+
* @param t A TryCatch5 to promote
14+
* @return A Validation with an Exception on the failure side and its result on the success side.
15+
*/
16+
static public <A, B, C, D, E, F> F5<A, B, C, D, E, Validation<Exception, F>> toF5(final TryCatch5<A, B, C, D, E, F> t) {
17+
return (a, b, c, d, e) -> {
18+
try {
19+
return Validation.success(t.f(a, b, c, d, e));
20+
} catch (Exception ex) {
21+
return Validation.fail(ex);
22+
}
23+
};
24+
}
25+
726
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
package fj;
22

3+
import fj.data.Validation;
4+
35
/**
46
* Created by MarkPerry on 6/04/2014.
57
*/
68
public class F6Functions {
9+
10+
/**
11+
* Promotes the TryCatch6 to a Validation that returns an Exception on the failure side and its result on the success side.
12+
*
13+
* @param t A TryCatch6 to promote
14+
* @return A Validation with an Exception on the failure side and its result on the success side.
15+
*/
16+
static public <A, B, C, D, E, F, G> F6<A, B, C, D, E, F, Validation<Exception, G>> toF6(final TryCatch6<A, B, C, D, E, F, G> t) {
17+
return (a, b, c, d, e, f) -> {
18+
try {
19+
return Validation.success(t.f(a, b, c, d, e, f));
20+
} catch (Exception ex) {
21+
return Validation.fail(ex);
22+
}
23+
};
24+
}
25+
726
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
package fj;
22

3+
import fj.data.Validation;
4+
35
/**
46
* Created by MarkPerry on 6/04/2014.
57
*/
68
public class F7Functions {
9+
10+
/**
11+
* Promotes the TryCatch7 to a Validation that returns an Exception on the failure side and its result on the success side.
12+
*
13+
* @param t A TryCatch7 to promote
14+
* @return A Validation with an Exception on the failure side and its result on the success side.
15+
*/
16+
static public <A, B, C, D, E, F, G, H> F7<A, B, C, D, E, F, G, Validation<Exception, H>> toF7(final TryCatch7<A, B, C, D, E, F, G, H> t) {
17+
return (a, b, c, d, e, f, g) -> {
18+
try {
19+
return Validation.success(t.f(a, b, c, d, e, f, g));
20+
} catch (Exception ex) {
21+
return Validation.fail(ex);
22+
}
23+
};
24+
}
25+
726
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
package fj;
22

3+
import fj.data.Validation;
4+
35
/**
46
* Created by MarkPerry on 6/04/2014.
57
*/
68
public class F8Functions {
9+
10+
/**
11+
* Promotes the TryCatch8 to a Validation that returns an Exception on the failure side and its result on the success side.
12+
*
13+
* @param t A TryCatch8 to promote
14+
* @return A Validation with an Exception on the failure side and its result on the success side.
15+
*/
16+
static public <A, B, C, D, E, F, G, H, I> F8<A, B, C, D, E, F, G, H, Validation<Exception, I>> toF8(final TryCatch8<A, B, C, D, E, F, G, H, I> t) {
17+
return (a, b, c, d, e, f, g, h) -> {
18+
try {
19+
return Validation.success(t.f(a, b, c, d, e, f, g, h));
20+
} catch (Exception ex) {
21+
return Validation.fail(ex);
22+
}
23+
};
24+
}
25+
726
}

core/src/main/java/fj/P1Functions.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import fj.data.Array;
66
import fj.data.List;
77
import fj.data.Stream;
8+
import fj.data.Validation;
89

910
public final class P1Functions {
1011

@@ -217,4 +218,20 @@ public A f(final B b) {
217218
};
218219
}
219220

221+
/**
222+
* Promotes the TryCatch0 to a Validation that returns an Exception on the failure side and its result on the success side.
223+
*
224+
* @param t A TryCatch0 to promote
225+
* @return A Validation with an Exception on the failure side and its result on the success side.
226+
*/
227+
static public <A> P1<Validation<Exception, A>> toP1(final TryCatch0<A> t) {
228+
return () -> {
229+
try {
230+
return Validation.success(t.f());
231+
} catch (Exception e) {
232+
return Validation.fail(e);
233+
}
234+
};
235+
}
236+
220237
}

0 commit comments

Comments
 (0)