7
7
8
8
package xsbti .api ;
9
9
10
+ import java .util .function .Supplier ;
11
+
10
12
/**
11
13
* Implement a Scala `lazy val` in Java for the facing sbt interface.
12
14
*
13
15
* It holds a reference to a thunk that is lazily evaluated and then
14
16
* its reference is clear to avoid memory leaks in memory-intensive code.
15
- * It needs to be defined in [[xsbti]] or a subpackage, see
16
- * [[xsbti.api.Lazy]] or [[xsbti.F0]] for similar definitions.
17
+ * It needs to be defined in [[xsbti]] or a subpackage, see [[xsbti.api.Lazy]]
18
+ * for similar definitions.
17
19
*/
18
20
public final class SafeLazy {
19
21
20
22
/* We do not use conversions from and to Scala functions because [[xsbti]]
21
23
* cannot hold any reference to Scala code nor the Scala library. */
22
24
23
25
/** Return a sbt [[xsbti.api.Lazy]] from a given Scala parameterless function. */
24
- public static <T > xsbti .api .Lazy <T > apply (xsbti . F0 <T > sbtThunk ) {
26
+ public static <T > xsbti .api .Lazy <T > apply (Supplier <T > sbtThunk ) {
25
27
return new Impl <T >(sbtThunk );
26
28
}
27
29
28
30
/** Return a sbt [[xsbti.api.Lazy]] from a strict value. */
29
31
public static <T > xsbti .api .Lazy <T > strict (T value ) {
30
32
// Convert strict parameter to sbt function returning it
31
- return apply (new xsbti .F0 <T >() {
32
- public T apply () {
33
+ return apply (new Supplier <T >() {
34
+ @ Override
35
+ public T get () {
33
36
return value ;
34
37
}
35
38
});
36
39
}
37
40
38
41
private static final class Impl <T > extends xsbti .api .AbstractLazy <T > {
39
- private xsbti . F0 <T > thunk ;
42
+ private Supplier <T > thunk ;
40
43
private T result ;
41
44
private boolean flag = false ;
42
45
43
- Impl (xsbti . F0 <T > thunk ) {
46
+ Impl (Supplier <T > thunk ) {
44
47
this .thunk = thunk ;
45
48
}
46
49
@@ -52,7 +55,7 @@ private static final class Impl<T> extends xsbti.api.AbstractLazy<T> {
52
55
public T get () {
53
56
if (flag ) return result ;
54
57
else {
55
- result = thunk .apply ();
58
+ result = thunk .get ();
56
59
flag = true ;
57
60
// Clear reference so that thunk is GC'ed
58
61
thunk = null ;
0 commit comments