Skip to content

Commit

Permalink
added withResourceInit and letWithResourceInit methods
Browse files Browse the repository at this point in the history
  • Loading branch information
evpl committed Mar 17, 2024
1 parent ef8e30a commit d0fb57d
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 4 deletions.
68 changes: 65 additions & 3 deletions src/main/java/com/plugatar/jkscope/JKScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.concurrent.atomic.AtomicReference;

import static com.plugatar.jkscope.Utils.blockArgNotNull;
import static com.plugatar.jkscope.Utils.initializerArgNotNull;
import static com.plugatar.jkscope.Utils.uncheckedCast;

/**
Expand Down Expand Up @@ -82,6 +83,7 @@
* <li>{@link #withLong(long, ThLongConsumer)}</li>
* <li>{@link #withDouble(double, ThDoubleConsumer)}</li>
* <li>{@link #withResource(AutoCloseable, ThConsumer)}</li>
* <li>{@link #withResourceInit(ThSupplier, ThConsumer)}</li>
* <li>{@link #with(Object, Object, ThBiConsumer)}</li>
* <li>{@link #with(Object, Object, Object, ThTriConsumer)}</li>
* <li>{@link #let(ThSupplier)}</li>
Expand All @@ -101,6 +103,7 @@
* <li>{@link #letLongWith(Object, ThToLongFunction)}</li>
* <li>{@link #letDoubleWith(Object, ThToDoubleFunction)}</li>
* <li>{@link #letWithResource(AutoCloseable, ThFunction)}</li>
* <li>{@link #letWithResourceInit(ThSupplier, ThFunction)}</li>
* <li>{@link #letWith(Object, Object, ThBiFunction)}</li>
* <li>{@link #letWith(Object, Object, Object, ThTriFunction)}</li>
* <li>{@link #opt(Object)}</li>
Expand Down Expand Up @@ -235,7 +238,9 @@ static void runCatching(final ThRunnable<?> block) {
static void runCatching(final ThRunnable<?> block,
final Class<? extends Throwable>... exceptionTypes) {
blockArgNotNull(block);
if (exceptionTypes == null) { throw new NullPointerException("exceptionTypes arg is null"); }
if (exceptionTypes == null) {
throw new NullPointerException("exceptionTypes arg is null");
}
for (int idx = 0; idx < exceptionTypes.length; idx++) {
if (exceptionTypes[idx] == null) {
throw new NullPointerException("exceptionTypes arg array contains null element at index " + idx);
Expand Down Expand Up @@ -351,7 +356,9 @@ static void withDouble(final double value,
/**
* Performs given function block on given {@link AutoCloseable} value and close this value.
* <pre>{@code
* with(new MyResource(), it -> System.out.println(it.getValue()));
* withResource(new PrintWriter("C:\\file.txt"), writer -> {
* writer.println("text");
* });
* }</pre>
*
* @param value the value
Expand All @@ -369,6 +376,30 @@ static <V extends AutoCloseable> void withResource(final V value,
}).accept(value, block);
}

/**
* Performs given function block on specified by initializer {@link AutoCloseable} value and close this value.
* <pre>{@code
* withResourceInit(() -> new PrintWriter("C:\\file.txt"), writer -> {
* writer.println("text");
* });
* }</pre>
*
* @param initializer the value initializer
* @param block the function block
* @param <V> the type of the value
* @throws NullPointerException if {@code initializer} or {@code block} arg is null
*/
static <V extends AutoCloseable> void withResourceInit(final ThSupplier<? extends V, ?> initializer,
final ThConsumer<? super V, ?> block) {
initializerArgNotNull(initializer);
blockArgNotNull(block);
ThBiConsumer.<ThSupplier<? extends V, ?>, ThConsumer<? super V, ?>>unchecked((i, b) -> {
try (final V resource = i.get()) {
b.accept(resource);
}
}).accept(initializer, block);
}

/**
* Performs given function block on given values.
* <pre>{@code
Expand Down Expand Up @@ -756,7 +787,10 @@ static <V> double letDoubleWith(final V value,
/**
* Performs given function block on {@link AutoCloseable} value, close this value and returns result.
* <pre>{@code
* String value = letWith(new MyResource(), it -> it.getValue());
* String value = letWithResource(new PrintWriter("C:\\file.txt"), writer -> {
* writer.println("text");
* return "value";
* });
* }</pre>
*
* @param value the value
Expand All @@ -776,6 +810,34 @@ static <V extends AutoCloseable, R> R letWithResource(final V value,
}).apply(value, block);
}

/**
* Performs given function block on specified by initializer {@link AutoCloseable}, close this value and returns
* result.
* <pre>{@code
* String value = letWithResourceInit(() -> new PrintWriter("C:\\file.txt"), writer -> {
* writer.println("text");
* return "value";
* });
* }</pre>
*
* @param initializer the value initializer
* @param block the function block
* @param <V> the type of the value
* @param <R> the type of the result
* @return result
* @throws NullPointerException if {@code initializer} or {@code block} arg is null
*/
static <V extends AutoCloseable, R> R letWithResourceInit(final ThSupplier<? extends V, ?> initializer,
final ThFunction<? super V, ? extends R, ?> block) {
initializerArgNotNull(initializer);
blockArgNotNull(block);
return ThBiFunction.<ThSupplier<? extends V, ?>, ThFunction<? super V, ? extends R, ?>, R>unchecked((i, b) -> {
try (final V resource = i.get()) {
return b.apply(resource);
}
}).apply(initializer, block);
}

/**
* Performs given function block on given value and returns result.
* <pre>{@code
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/plugatar/jkscope/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ final class Utils {
private Utils() {
}

static void initializerArgNotNull(final Object block) {
if (block == null) {
throw new NullPointerException("initializer arg is null");
}
}

static void blockArgNotNull(final Object block) {
if (block == null) {
throw new NullPointerException("block arg is null");
Expand Down
71 changes: 70 additions & 1 deletion src/test/java/com/plugatar/jkscope/JKScopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,25 @@ void withDoubleStaticMethodThrowsNPEForNullArg() {
}

@Test
void withResourceMethodThrowsNPEForNullArg() {
void withResourceMethodThrowsNPEForNullInitializerArg() {
final ThSupplier<AutoCloseable, Throwable> initializer = null;
final ThConsumer<Object, Throwable> block = v -> { };

assertThatThrownBy(() -> JKScope.withResourceInit(initializer, block))
.isInstanceOf(NullPointerException.class);
}

@Test
void withResourceMethodThrowsNPEForNullBlockArg() {
final ThSupplier<AutoCloseable, Throwable> initializer = () -> new AutoCloseableImpl();
final ThConsumer<Object, Throwable> block = null;

assertThatThrownBy(() -> JKScope.withResourceInit(initializer, block))
.isInstanceOf(NullPointerException.class);
}

@Test
void withResourceInitMethodThrowsNPEForNullArg() {
final AutoCloseable value = new AutoCloseableImpl();
final ThConsumer<Object, Throwable> block = null;

Expand Down Expand Up @@ -540,6 +558,24 @@ void letWithResourceMethodThrowsNPEForNullArg() {
.isInstanceOf(NullPointerException.class);
}

@Test
void letWithResourceInitMethodThrowsNPEForNullInitializerArg() {
final ThSupplier<AutoCloseable, Throwable> initializer = null;
final ThFunction<Object, Object, Throwable> block = v -> new Object();

assertThatThrownBy(() -> JKScope.letWithResourceInit(initializer, block))
.isInstanceOf(NullPointerException.class);
}

@Test
void letWithResourceInitMethodThrowsNPEForNullBlockArg() {
final ThSupplier<AutoCloseable, Throwable> initializer = () -> new AutoCloseableImpl();
final ThFunction<Object, Object, Throwable> block = null;

assertThatThrownBy(() -> JKScope.letWithResourceInit(initializer, block))
.isInstanceOf(NullPointerException.class);
}

@Test
void letWith2ArgsStaticMethodThrowsNPEForNullArg() {
final Object value1 = new Object();
Expand Down Expand Up @@ -802,6 +838,20 @@ void withResourceMethodThrowsException() {
.isTrue();
}

@Test
void withResourceInitMethod() {
final AutoCloseableImpl value = new AutoCloseableImpl();
final AtomicReference<Object> valueRef = new AtomicReference<>();
final ThSupplier<AutoCloseable, Throwable> initializer = () -> value;
final ThConsumer<Object, Throwable> block = arg -> valueRef.set(arg);

JKScope.withResourceInit(initializer, block);
assertThat(valueRef.get())
.isSameAs(value);
assertThat(value.isClosed())
.isTrue();
}

@Test
void with2ArgsStaticMethod() {
final Object value1 = new Object();
Expand Down Expand Up @@ -1312,6 +1362,25 @@ void letWithResourceMethodThrowsException() {
.isTrue();
}

@Test
void letWithResourceInitMethod() {
final AutoCloseableImpl value = new AutoCloseableImpl();
final Object result = new Object();
final AtomicReference<Object> valueRef = new AtomicReference<>();
final ThSupplier<AutoCloseable, Throwable> initializer = () -> value;
final ThFunction<Object, Object, Throwable> block = arg -> {
valueRef.set(arg);
return result;
};

assertThat(JKScope.letWithResourceInit(initializer, block))
.isSameAs(result);
assertThat(valueRef.get())
.isSameAs(value);
assertThat(value.isClosed())
.isTrue();
}

@Test
void letWith2ArgsStaticMethod() {
final Object value1 = new Object();
Expand Down

0 comments on commit d0fb57d

Please sign in to comment.