@@ -26,7 +26,10 @@ Java scope functions inspired by Kotlin
2626 * [ ` run ` , ` runCatching ` and ` runRec ` ] ( #run-runcatching-and-runrec )
2727 * [ ` with ` , ` withInt ` , ` withLong ` , ` withDouble ` and ` withResource ` ] ( #with-withint-withlong-withdouble-and-withresource )
2828 * [ ` let ` variations] ( #let-variations )
29- * [ ` Opt ` monad] ( #opt-monad )
29+ * [ ` opt ` and ` optNonNull ` ] ( #opt-and-optnonnull )
30+ * [ ` lazy ` and ` lazyOfValue ` ] ( #lazy-and-lazyofvalue )
31+ * [ ` Opt ` object] ( #opt-object )
32+ * [ ` Lazy ` object] ( #lazy-object )
3033 * [ Unchecked functions] ( #unchecked-functions )
3134 * [ Examples] ( #examples )
3235 * [ Collection initialization] ( #collection-initialization )
5255<dependency >
5356 <groupId >com.plugatar.jkscope</groupId >
5457 <artifactId >jkscope</artifactId >
55- <version >2.2 </version >
58+ <version >2.3 </version >
5659 <scope >compile</scope >
5760</dependency >
5861```
@@ -61,7 +64,7 @@ Gradle:
6164
6265``` groovy
6366dependencies {
64- implementation 'com.plugatar.jkscope:jkscope:2.2 '
67+ implementation 'com.plugatar.jkscope:jkscope:2.3 '
6568}
6669```
6770
@@ -165,15 +168,6 @@ with(value1, value2, (v1, v2) -> {
165168
166169#### ` let ` variations
167170
168- ` let ` returns ` Opt ` instance of given value, ` letNonNull ` returns ` Opt ` instance of given value of given value or
169- empty ` Opt ` instance if given value is null.
170-
171- ```
172- let(value).takeNonNull().takeUnless(it -> it.isEmpty()).takeIf(it -> it.length() < 100).letIt(it -> System.out.println(it));
173-
174- letNonNull(value).takeUnless(it -> it.isEmpty()).takeIf(it -> it.length() < 100).letIt(it -> System.out.println(it));
175- ```
176-
177171` let ` , ` letInt ` , ` letLong ` and ` letDouble ` returns result of function block.
178172
179173```
@@ -213,11 +207,35 @@ int value = letWith("42", it -> Integer.valueOf(it));
213207
214208` letWithResource ` method does the same thing, but with a ` AutoCloseable ` resource and closes this resource.
215209
216- ### ` Opt ` monad
210+ #### ` opt ` and ` optNonNull `
211+
212+ ` opt ` returns ` Opt ` instance of given value, ` optNonNull ` returns ` Opt ` instance of given value of given value or
213+ empty ` Opt ` instance if given value is null.
214+
215+ ```
216+ opt(value).takeNonNull().takeUnless(it -> it.isEmpty()).takeIf(it -> it.length() < 100).letIt(it -> System.out.println(it));
217+
218+ optNonNull(value).takeUnless(it -> it.isEmpty()).takeIf(it -> it.length() < 100).letIt(it -> System.out.println(it));
219+ ```
220+
221+ #### ` lazy ` and ` lazyOfValue `
222+
223+ ` lazy ` returns ` Lazy ` instance with given initializer. ` lazyOfValue ` returns ` Lazy ` instance of given value.
224+
225+ ```
226+ Lazy<String> lazy = lazy(() -> {
227+ //...
228+ return "value";
229+ });
230+
231+ Lazy<String> lazyOfValue = lazyOfValue("value");
232+ ```
233+
234+ ### ` Opt ` object
217235
218236The ` Opt ` monad is similar in meaning to Java ` Optional ` , but allows the null value.
219237
220- ` Opt ` monad contains some ` Optional ` methods and scope functions methods:
238+ ` Opt ` monad contains some ` Optional ` methods and scope functions methods.
221239
222240```
223241String result = Opt.of(value).takeIf(it -> it.length() > 10).orElse("");
@@ -227,6 +245,19 @@ String result = Opt.of(value).takeNonNull().orElseGet(() -> "");
227245String result = Opt.of(value).takeIf(it -> it.length() > 10).orElseThrow(() -> new IllegalArgumentException());
228246```
229247
248+ ### ` Lazy ` object
249+
250+ ` Lazy ` represents a value with lazy initialization.
251+
252+ ```
253+ Lazy<String> lazy = lazy(() -> {
254+ //...
255+ return "value";
256+ });
257+
258+ Lazy<String> lazyOfValue = lazyOfValue("value");
259+ ```
260+
230261### Unchecked functions
231262
232263All presented functions allow you to not process checked exceptions.
@@ -282,7 +313,7 @@ int value = letIntRec(10, (n, func) -> {
282313
283314```
284315public static String checkNonNullNonEmptyStr(String value) {
285- return let (value)
316+ return opt (value)
286317 .takeNonNull().throwIfEmpty(NullPointerException::new)
287318 .takeUnless(String::isEmpty).throwIfEmpty(IllegalArgumentException::new)
288319 .get();
0 commit comments