forked from jenkinsci/winp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rethrow initialization error in Native.sendCtrlC (jenkinsci#56)
- Loading branch information
Showing
2 changed files
with
80 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.jvnet.winp; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
final class LazyValue<T> { | ||
private final Callable<T> myProvider; | ||
private final Object myLock = new Object(); | ||
private volatile Result<T> myResult; | ||
|
||
LazyValue(Callable<T> provider) { | ||
myProvider = provider; | ||
} | ||
|
||
public T getValue() throws Exception { | ||
Result<T> result = myResult; | ||
if (result != null) { | ||
return unpack(result); | ||
} | ||
synchronized (myLock) { | ||
result = myResult; | ||
if (result == null) { | ||
try { | ||
T value = myProvider.call(); | ||
result = new Result<T>(value, null); | ||
} | ||
catch (Throwable t) { | ||
result = new Result<T>(null, t); | ||
} | ||
myResult = result; | ||
} | ||
} | ||
return unpack(result); | ||
} | ||
|
||
private T unpack(Result<T> result) throws Exception { | ||
if (result.throwable != null) { | ||
if (result.throwable instanceof Exception) { | ||
throw (Exception)result.throwable; | ||
} | ||
if (result.throwable instanceof Error) { | ||
throw (Error)result.throwable; | ||
} | ||
throw new RuntimeException("Rethrowing unknown Throwable", result.throwable); | ||
} | ||
return result.value; | ||
} | ||
|
||
private static class Result<T> { | ||
private final T value; | ||
private final Throwable throwable; | ||
|
||
private Result(T value, Throwable throwable) { | ||
this.value = value; | ||
this.throwable = throwable; | ||
} | ||
} | ||
} |
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