ThrowableFunction

Throwable 例外を投げて処理する関数型インターフェースを作っている。
yipuran-core/src/main/java/org/yipuran/function at master · yipuran/yipuran-core · GitHub

使い道はいっぱいある。

Strin str = "[ \"A\", \"B\", \"C\"  ]";

これを Jackson ObjectMapper で、List<String> を生成するコードを書くと、、

ObjectMapper mapper = new ObjectMapper();

try{
    List<String> list = mapper.readValue(str, new TypeReference<List<String>>(){});
}catch (JsonMappingException e){
    e.printStackTrace();
}catch (JsonProcessingException e){
    e.printStackTrace();
}

どうしても例外捕捉を書かなくてはならない。
org.yipuran.function.ThrowableFunction
を使えば、以下のように書ける。

Function<String, List<String>> function = ThrowableFunction.of(
    s->new ObjectMapper().readValue(s, new TypeReference<List<String>>(){})
);
List<String> list = function.apply(str);

1行で書くなら、、、

List<String> list =
ThrowableFunction.of(s->new ObjectMapper().readValue((String)s, new TypeReference<List<String>>(){}))
.apply(str);