4343import java .util .concurrent .atomic .AtomicReference ;
4444
4545import static com .plugatar .jkscope .Utils .blockArgNotNull ;
46+ import static com .plugatar .jkscope .Utils .initializerArgNotNull ;
4647import static com .plugatar .jkscope .Utils .uncheckedCast ;
4748
4849/**
8283 * <li>{@link #withLong(long, ThLongConsumer)}</li>
8384 * <li>{@link #withDouble(double, ThDoubleConsumer)}</li>
8485 * <li>{@link #withResource(AutoCloseable, ThConsumer)}</li>
86+ * <li>{@link #withResourceInit(ThSupplier, ThConsumer)}</li>
8587 * <li>{@link #with(Object, Object, ThBiConsumer)}</li>
8688 * <li>{@link #with(Object, Object, Object, ThTriConsumer)}</li>
8789 * <li>{@link #let(ThSupplier)}</li>
101103 * <li>{@link #letLongWith(Object, ThToLongFunction)}</li>
102104 * <li>{@link #letDoubleWith(Object, ThToDoubleFunction)}</li>
103105 * <li>{@link #letWithResource(AutoCloseable, ThFunction)}</li>
106+ * <li>{@link #letWithResourceInit(ThSupplier, ThFunction)}</li>
104107 * <li>{@link #letWith(Object, Object, ThBiFunction)}</li>
105108 * <li>{@link #letWith(Object, Object, Object, ThTriFunction)}</li>
106109 * <li>{@link #opt(Object)}</li>
@@ -235,7 +238,9 @@ static void runCatching(final ThRunnable<?> block) {
235238 static void runCatching (final ThRunnable <?> block ,
236239 final Class <? extends Throwable >... exceptionTypes ) {
237240 blockArgNotNull (block );
238- if (exceptionTypes == null ) { throw new NullPointerException ("exceptionTypes arg is null" ); }
241+ if (exceptionTypes == null ) {
242+ throw new NullPointerException ("exceptionTypes arg is null" );
243+ }
239244 for (int idx = 0 ; idx < exceptionTypes .length ; idx ++) {
240245 if (exceptionTypes [idx ] == null ) {
241246 throw new NullPointerException ("exceptionTypes arg array contains null element at index " + idx );
@@ -351,7 +356,9 @@ static void withDouble(final double value,
351356 /**
352357 * Performs given function block on given {@link AutoCloseable} value and close this value.
353358 * <pre>{@code
354- * with(new MyResource(), it -> System.out.println(it.getValue()));
359+ * withResource(new PrintWriter("C:\\file.txt"), writer -> {
360+ * writer.println("text");
361+ * });
355362 * }</pre>
356363 *
357364 * @param value the value
@@ -369,6 +376,30 @@ static <V extends AutoCloseable> void withResource(final V value,
369376 }).accept (value , block );
370377 }
371378
379+ /**
380+ * Performs given function block on specified by initializer {@link AutoCloseable} value and close this value.
381+ * <pre>{@code
382+ * withResourceInit(() -> new PrintWriter("C:\\file.txt"), writer -> {
383+ * writer.println("text");
384+ * });
385+ * }</pre>
386+ *
387+ * @param initializer the value initializer
388+ * @param block the function block
389+ * @param <V> the type of the value
390+ * @throws NullPointerException if {@code initializer} or {@code block} arg is null
391+ */
392+ static <V extends AutoCloseable > void withResourceInit (final ThSupplier <? extends V , ?> initializer ,
393+ final ThConsumer <? super V , ?> block ) {
394+ initializerArgNotNull (initializer );
395+ blockArgNotNull (block );
396+ ThBiConsumer .<ThSupplier <? extends V , ?>, ThConsumer <? super V , ?>>unchecked ((i , b ) -> {
397+ try (final V resource = i .get ()) {
398+ b .accept (resource );
399+ }
400+ }).accept (initializer , block );
401+ }
402+
372403 /**
373404 * Performs given function block on given values.
374405 * <pre>{@code
@@ -756,7 +787,10 @@ static <V> double letDoubleWith(final V value,
756787 /**
757788 * Performs given function block on {@link AutoCloseable} value, close this value and returns result.
758789 * <pre>{@code
759- * String value = letWith(new MyResource(), it -> it.getValue());
790+ * String value = letWithResource(new PrintWriter("C:\\file.txt"), writer -> {
791+ * writer.println("text");
792+ * return "value";
793+ * });
760794 * }</pre>
761795 *
762796 * @param value the value
@@ -776,6 +810,34 @@ static <V extends AutoCloseable, R> R letWithResource(final V value,
776810 }).apply (value , block );
777811 }
778812
813+ /**
814+ * Performs given function block on specified by initializer {@link AutoCloseable}, close this value and returns
815+ * result.
816+ * <pre>{@code
817+ * String value = letWithResourceInit(() -> new PrintWriter("C:\\file.txt"), writer -> {
818+ * writer.println("text");
819+ * return "value";
820+ * });
821+ * }</pre>
822+ *
823+ * @param initializer the value initializer
824+ * @param block the function block
825+ * @param <V> the type of the value
826+ * @param <R> the type of the result
827+ * @return result
828+ * @throws NullPointerException if {@code initializer} or {@code block} arg is null
829+ */
830+ static <V extends AutoCloseable , R > R letWithResourceInit (final ThSupplier <? extends V , ?> initializer ,
831+ final ThFunction <? super V , ? extends R , ?> block ) {
832+ initializerArgNotNull (initializer );
833+ blockArgNotNull (block );
834+ return ThBiFunction .<ThSupplier <? extends V , ?>, ThFunction <? super V , ? extends R , ?>, R >unchecked ((i , b ) -> {
835+ try (final V resource = i .get ()) {
836+ return b .apply (resource );
837+ }
838+ }).apply (initializer , block );
839+ }
840+
779841 /**
780842 * Performs given function block on given value and returns result.
781843 * <pre>{@code
0 commit comments