我的一個項目是throwing-lambdas;其中我的目的是爲了緩解在Stream
s中潛在@FunctionalInterface
的使用,其唯一在流中使用的「缺陷」是它們拋出檢查的異常(就我而言,我寧願稱爲有缺陷的事實,即你不能拋出從流中檢查異常但這是另一回事)。在這種情況下,我如何處理函數<T, R>和省略號/可變參數?
所以,Function<T, R>
我定義的:
@FunctionalInterface
public interface ThrowingFunction<T, R>
extends Function<T, R>
{
R doApply(T t)
throws Throwable;
default R apply(T t)
{
try {
return doApply(t);
} catch (Error | RuntimeException e) {
throw e;
} catch (Throwable throwable) {
throw new ThrownByLambdaException(throwable);
}
}
}
這讓我來定義,比如:
final ThrowingFunction<Path, Path> = Path::toRealPath;
(爲什麼Path::toRealPath
......好吧,precisely because it has an ellipsis)。
因爲不想停在這裏我希望能夠寫出這樣的東西:
Throwing.function(Path::toRealPath).fallbackTo(Path::toAbsolutePath)
以上NEARLY工程...閱讀。
我還定義了這一點:
public abstract class Chainer<N, T extends N, C extends Chainer<N, T, C>>
{
protected final T throwing;
protected Chainer(final T throwing)
{
this.throwing = throwing;
}
public abstract C orTryWith(T other);
public abstract <E extends RuntimeException> T orThrow(
final Class<E> exclass);
public abstract N fallbackTo(N fallback);
public final <E extends RuntimeException> T as(final Class<E> exclass)
{
return orThrow(exclass);
}
}
這是它Function
S上的實現:
public final class ThrowingFunctionChain<T, R>
extends Chainer<Function<T, R>, ThrowingFunction<T, R>, ThrowingFunctionChain<T, R>>
implements ThrowingFunction<T, R>
{
public ThrowingFunctionChain(final ThrowingFunction<T, R> function)
{
super(function);
}
@Override
public R doApply(final T t)
throws Throwable
{
return throwing.doApply(t);
}
@Override
public ThrowingFunctionChain<T, R> orTryWith(
final ThrowingFunction<T, R> other)
{
final ThrowingFunction<T, R> function = t -> {
try {
return throwing.doApply(t);
} catch (Error | RuntimeException e) {
throw e;
} catch (Throwable ignored) {
return other.doApply(t);
}
};
return new ThrowingFunctionChain<>(function);
}
@Override
public <E extends RuntimeException> ThrowingFunction<T, R> orThrow(
final Class<E> exclass)
{
return t -> {
try {
return throwing.doApply(t);
} catch (Error | RuntimeException e) {
throw e;
} catch (Throwable throwable) {
throw ThrowablesFactory.INSTANCE.get(exclass, throwable);
}
};
}
@Override
public Function<T, R> fallbackTo(final Function<T, R> fallback)
{
return t -> {
try {
return doApply(t);
} catch (Error | RuntimeException e) {
throw e;
} catch (Throwable ignored) {
return fallback.apply(t);
}
};
}
}
到目前爲止好(雖然IDEA fails to recognize the code of orTryWith()
as valid,但那是另一回事)。
我還定義了一個名爲Throwing
工具類的問題就出在這個班,我寫了一個測試的main()
:現在
public final class Throwing
{
private Throwing()
{
throw new Error("nice try!");
}
public static <T, R> ThrowingFunctionChain<T, R> function(
final ThrowingFunction<T, R> function)
{
return new ThrowingFunctionChain<>(function);
}
public static void main(final String... args)
{
// FAILS TO COMPILE
final Function<Path, Path> f = function(Path::toRealPath)
.fallbackTo(Path::toAbsolutePath);
}
}
,對於上面的代碼中的錯誤信息是:
Error:(29, 48) java: incompatible types: cannot infer type-variable(s) T,R
(argument mismatch; invalid method reference
method toRealPath in interface java.nio.file.Path cannot be applied to given types
required: java.nio.file.LinkOption[]
found: java.lang.Object
reason: varargs mismatch; java.lang.Object cannot be converted to java.nio.file.LinkOption)
Error:(29, 49) java: invalid method reference
non-static method toRealPath(java.nio.file.LinkOption...) cannot be referenced from a static context
Error:(30, 25) java: invalid method reference
non-static method toAbsolutePath() cannot be referenced from a static context
我無法診斷錯誤的確切原因在這裏,但對我來說,它看起來像省略號阻礙了;其實,如果我這樣做:
final ThrowingFunctionChain<Path, Path> f = function(Path::toRealPath);
try (
final Stream<Path> stream = Files.list(Paths.get(""));
) {
stream.map(f.fallbackTo(Path::toAbsolutePath))
.forEach(System.out::println);
}
然後再編譯:所以這意味着Stream.map()
不承認結果作爲一個Function
...
那麼爲什麼不會Throwing.function(Path::toRealPath).fallbackTo(Path::toAbsolutePath)
編譯?
只是一個建議:'公共接口ThrowingFunction 延伸功能 { řdoApply(T T) 拋出Ë;' –
Bohemian
2015-03-03 03:53:27
@Bohemian這種方法的問題是它不會對拋出多個異常的方法效果不佳;還有,你是剝奪了一些非常酷的潛在候選人的λ如'MethodHandle :: invoke' – fge 2015-03-03 15:09:59