Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Remove any reference to F0 and F1
Browse files Browse the repository at this point in the history
Syncs up with sbt/util#84.
  • Loading branch information
jvican committed Jul 14, 2017
1 parent d6d5771 commit 0b33327
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/contraband/incremental.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
{ "name": "maxErrors", "type": "int" },
{
"name": "sourcePositionMapper",
"type": "xsbti.F1<xsbti.Position, xsbti.Position>"
"type": "java.util.function.Function<xsbti.Position, xsbti.Position>"
},
{
"name": "order",
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/xsbti/api/SafeLazy.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,43 @@

package xsbti.api;

import java.util.function.Supplier;

/**
* Implement a Scala `lazy val` in Java for the facing sbt interface.
*
* It holds a reference to a thunk that is lazily evaluated and then
* its reference is clear to avoid memory leaks in memory-intensive code.
* It needs to be defined in [[xsbti]] or a subpackage, see
* [[xsbti.api.Lazy]] or [[xsbti.F0]] for similar definitions.
* It needs to be defined in [[xsbti]] or a subpackage, see [[xsbti.api.Lazy]]
* for similar definitions.
*/
public final class SafeLazy {

/* We do not use conversions from and to Scala functions because [[xsbti]]
* cannot hold any reference to Scala code nor the Scala library. */

/** Return a sbt [[xsbti.api.Lazy]] from a given Scala parameterless function. */
public static <T> xsbti.api.Lazy<T> apply(xsbti.F0<T> sbtThunk) {
public static <T> xsbti.api.Lazy<T> apply(Supplier<T> sbtThunk) {
return new Impl<T>(sbtThunk);
}

/** Return a sbt [[xsbti.api.Lazy]] from a strict value. */
public static <T> xsbti.api.Lazy<T> strict(T value) {
// Convert strict parameter to sbt function returning it
return apply(new xsbti.F0<T>() {
public T apply() {
return apply(new Supplier<T>() {
@Override
public T get() {
return value;
}
});
}

private static final class Impl<T> extends xsbti.api.AbstractLazy<T> {
private xsbti.F0<T> thunk;
private Supplier<T> thunk;
private T result;
private boolean flag = false;

Impl(xsbti.F0<T> thunk) {
Impl(Supplier<T> thunk) {
this.thunk = thunk;
}

Expand All @@ -52,7 +55,7 @@ private static final class Impl<T> extends xsbti.api.AbstractLazy<T> {
public T get() {
if (flag) return result;
else {
result = thunk.apply();
result = thunk.get();
flag = true;
// Clear reference so that thunk is GC'ed
thunk = null;
Expand Down

0 comments on commit 0b33327

Please sign in to comment.