-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #221 from jvican/fix-asf
Fix #113 and apply several other fixes
- Loading branch information
Showing
17 changed files
with
183 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
internal/compiler-interface/src/main/java/xsbti/api/SafeLazy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package xsbti.api; | ||
|
||
/** | ||
* 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. | ||
*/ | ||
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) { | ||
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 value; | ||
} | ||
}); | ||
} | ||
|
||
private static final class Impl<T> extends xsbti.api.AbstractLazy<T> { | ||
private xsbti.F0<T> thunk; | ||
private T result; | ||
private boolean flag = false; | ||
|
||
Impl(xsbti.F0<T> thunk) { | ||
this.thunk = thunk; | ||
} | ||
|
||
/** | ||
* Return cached result or force lazy evaluation. | ||
* | ||
* Don't call it in a multi-threaded environment. | ||
*/ | ||
public T get() { | ||
if (flag) return result; | ||
else { | ||
result = thunk.apply(); | ||
flag = true; | ||
// Clear reference so that thunk is GC'ed | ||
thunk = null; | ||
return result; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.