Skip to content

Commit d0fb57d

Browse files
committed
added withResourceInit and letWithResourceInit methods
1 parent ef8e30a commit d0fb57d

File tree

3 files changed

+141
-4
lines changed

3 files changed

+141
-4
lines changed

src/main/java/com/plugatar/jkscope/JKScope.java

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.concurrent.atomic.AtomicReference;
4444

4545
import static com.plugatar.jkscope.Utils.blockArgNotNull;
46+
import static com.plugatar.jkscope.Utils.initializerArgNotNull;
4647
import static com.plugatar.jkscope.Utils.uncheckedCast;
4748

4849
/**
@@ -82,6 +83,7 @@
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>
@@ -101,6 +103,7 @@
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

src/main/java/com/plugatar/jkscope/Utils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ final class Utils {
2727
private Utils() {
2828
}
2929

30+
static void initializerArgNotNull(final Object block) {
31+
if (block == null) {
32+
throw new NullPointerException("initializer arg is null");
33+
}
34+
}
35+
3036
static void blockArgNotNull(final Object block) {
3137
if (block == null) {
3238
throw new NullPointerException("block arg is null");

src/test/java/com/plugatar/jkscope/JKScopeTest.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,25 @@ void withDoubleStaticMethodThrowsNPEForNullArg() {
362362
}
363363

364364
@Test
365-
void withResourceMethodThrowsNPEForNullArg() {
365+
void withResourceMethodThrowsNPEForNullInitializerArg() {
366+
final ThSupplier<AutoCloseable, Throwable> initializer = null;
367+
final ThConsumer<Object, Throwable> block = v -> { };
368+
369+
assertThatThrownBy(() -> JKScope.withResourceInit(initializer, block))
370+
.isInstanceOf(NullPointerException.class);
371+
}
372+
373+
@Test
374+
void withResourceMethodThrowsNPEForNullBlockArg() {
375+
final ThSupplier<AutoCloseable, Throwable> initializer = () -> new AutoCloseableImpl();
376+
final ThConsumer<Object, Throwable> block = null;
377+
378+
assertThatThrownBy(() -> JKScope.withResourceInit(initializer, block))
379+
.isInstanceOf(NullPointerException.class);
380+
}
381+
382+
@Test
383+
void withResourceInitMethodThrowsNPEForNullArg() {
366384
final AutoCloseable value = new AutoCloseableImpl();
367385
final ThConsumer<Object, Throwable> block = null;
368386

@@ -540,6 +558,24 @@ void letWithResourceMethodThrowsNPEForNullArg() {
540558
.isInstanceOf(NullPointerException.class);
541559
}
542560

561+
@Test
562+
void letWithResourceInitMethodThrowsNPEForNullInitializerArg() {
563+
final ThSupplier<AutoCloseable, Throwable> initializer = null;
564+
final ThFunction<Object, Object, Throwable> block = v -> new Object();
565+
566+
assertThatThrownBy(() -> JKScope.letWithResourceInit(initializer, block))
567+
.isInstanceOf(NullPointerException.class);
568+
}
569+
570+
@Test
571+
void letWithResourceInitMethodThrowsNPEForNullBlockArg() {
572+
final ThSupplier<AutoCloseable, Throwable> initializer = () -> new AutoCloseableImpl();
573+
final ThFunction<Object, Object, Throwable> block = null;
574+
575+
assertThatThrownBy(() -> JKScope.letWithResourceInit(initializer, block))
576+
.isInstanceOf(NullPointerException.class);
577+
}
578+
543579
@Test
544580
void letWith2ArgsStaticMethodThrowsNPEForNullArg() {
545581
final Object value1 = new Object();
@@ -802,6 +838,20 @@ void withResourceMethodThrowsException() {
802838
.isTrue();
803839
}
804840

841+
@Test
842+
void withResourceInitMethod() {
843+
final AutoCloseableImpl value = new AutoCloseableImpl();
844+
final AtomicReference<Object> valueRef = new AtomicReference<>();
845+
final ThSupplier<AutoCloseable, Throwable> initializer = () -> value;
846+
final ThConsumer<Object, Throwable> block = arg -> valueRef.set(arg);
847+
848+
JKScope.withResourceInit(initializer, block);
849+
assertThat(valueRef.get())
850+
.isSameAs(value);
851+
assertThat(value.isClosed())
852+
.isTrue();
853+
}
854+
805855
@Test
806856
void with2ArgsStaticMethod() {
807857
final Object value1 = new Object();
@@ -1312,6 +1362,25 @@ void letWithResourceMethodThrowsException() {
13121362
.isTrue();
13131363
}
13141364

1365+
@Test
1366+
void letWithResourceInitMethod() {
1367+
final AutoCloseableImpl value = new AutoCloseableImpl();
1368+
final Object result = new Object();
1369+
final AtomicReference<Object> valueRef = new AtomicReference<>();
1370+
final ThSupplier<AutoCloseable, Throwable> initializer = () -> value;
1371+
final ThFunction<Object, Object, Throwable> block = arg -> {
1372+
valueRef.set(arg);
1373+
return result;
1374+
};
1375+
1376+
assertThat(JKScope.letWithResourceInit(initializer, block))
1377+
.isSameAs(result);
1378+
assertThat(valueRef.get())
1379+
.isSameAs(value);
1380+
assertThat(value.isClosed())
1381+
.isTrue();
1382+
}
1383+
13151384
@Test
13161385
void letWith2ArgsStaticMethod() {
13171386
final Object value1 = new Object();

0 commit comments

Comments
 (0)