vavr
by Rajasekar Elango
由Rajasekar Elango
In this post, I will provide tips for better exception handling in Java 8 streams using the Functional Java library Vavr.
在這篇文章中,我將提供使用Functional Java庫Vavr在Java 8流中更好地處理異常的技巧。
問題 (The problem)
To illustrate with an example, let’s say we want to print the day of the week for a given stream of date strings in the format MM/dd/YYYY
.
為了舉例說明,假設我們要以MM/dd/YYYY
格式打印給定日期字符串流的星期幾。
初始解決方案 (Initial solution)
Let’s start with an initial solution, as seen below, and iteratively improve on it.
讓我們從一個初始解決方案開始,如下所示,然后對其進行迭代改進。
This will output
這將輸出
Huh, if a date string is invalid, this fails at the first DateTimeParseException without continuing with valid dates.
呵呵 ,如果日期字符串是無效的,這未能在第一DateTimeParseException沒有與有效日期繼續。
Java 8搶救之選 (Java 8 Optional to the rescue)
We can refactor parseDate
to return Optional<LocalDa
te> to make it discard invalids and continue processing valid dates.
我們可以重構parseDate
以返回Optional<LocalDa
te>,以使其放棄無效值并繼續處理有效日期。
This will skip errors and converts all the valid dates.
這將跳過錯誤并轉換所有有效日期。
WEDNESDAY Text '01-01-2015' could not be parsed at index 2 THURSDAY Text 'not a date' could not be parsed at index 0 FRIDAY
This is a great improvement, but the exception has to be handled within the parseDate
method and can’t be passed back to the main method to deal with it. We can’t make the parseDate
method throw a checked exception as the Streams API doesn’t play well with methods that throw exceptions.
這是一個很大的改進,但是必須在parseDate
方法中處理異常,并且不能將其傳遞回main方法進行處理。 我們不能使parseDate
方法拋出檢查異常,因為Streams API與拋出異常的方法不能很好地配合使用。
Vavr的Try Monad更好的解決方案 (A better solution with Vavr’s Try Monad)
Vavr is a functional library for Java 8+. We will use the Try
object from Vavr, which can be either an instance of Success
or Failure
. Basically Try is a monadic container type which represents a computation that may either result in an exception, or return a successfully computed value. Here is the modified code using Try
:
Vavr是Java 8+的功能庫。 我們將使用Vavr中的Try
對象,該對象可以是Success
或Failure
的實例。 基本上, Try是一種Monadic容器類型,它表示可能導致異常或返回成功計算值的計算。 這是使用Try
修改的代碼:
The output is
輸出是
WEDNESDAY Failed due to Text '01-01-2015' could not be parsed at index 2 THURSDAYFailed due to Text 'not a date' could not be parsed at index 0 FRIDAY
Now the exception is passed back to main to deal with it. Try also has APIs to implement recovery logic or return a default value in case of error.
現在,異常被傳遞回main進行處理。 Try還提供API來實現恢復邏輯或在出現錯誤的情況下返回默認值。
To demonstrate this, let’s say we also want to support MM-dd-YYYY
as an alternate string format for dates. The below example shows how we can easily implement recovery logic.
為了說明這一點,假設我們還希望支持MM-dd-YYYY
作為日期的備用字符串格式。 以下示例顯示了我們如何輕松實現恢復邏輯。
The output shows that now the date 01-01-2015
is also successfully converted.
輸出顯示,現在日期01-01-2015
也已成功轉換。
WEDNESDAY THURSDAY THURSDAY Failed due to Text 'not a date' could not be parsed at index 0 FRIDAY
So Try Monad can be used to elegantly deal with exceptions and fail fast on errors.
因此, Try Monad可以用于優雅地處理異常,并在發生錯誤時快速失敗 。
Update on 12/03/2018:
2018年12月3日更新:
The code examples are updated to use Doculet.
代碼示例已更新為使用Doculet 。
Originally published at http://erajasekar.com/posts/better-exception-handling-java8-streams-using-javaslang/.
最初發布在http://erajasekar.com/posts/better-exception-handling-java8-streams-using-javaslang/中 。
翻譯自: https://www.freecodecamp.org/news/better-exception-handling-in-java-8-streams-using-vavr-6eda31285ce9/
vavr