Skip to content

Commit

Permalink
added letIntWith, letLongWith, letDoubleWith methods
Browse files Browse the repository at this point in the history
  • Loading branch information
evpl committed Feb 26, 2024
1 parent e374769 commit 170b1f5
Show file tree
Hide file tree
Showing 8 changed files with 486 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/main/java/com/plugatar/jkscope/JKScope.java
Expand Up @@ -34,6 +34,9 @@
import com.plugatar.jkscope.function.ThPredicate;
import com.plugatar.jkscope.function.ThRunnable;
import com.plugatar.jkscope.function.ThSupplier;
import com.plugatar.jkscope.function.ThToDoubleFunction;
import com.plugatar.jkscope.function.ThToIntFunction;
import com.plugatar.jkscope.function.ThToLongFunction;
import com.plugatar.jkscope.function.ThTriConsumer;
import com.plugatar.jkscope.function.ThTriFunction;

Expand Down Expand Up @@ -95,6 +98,9 @@
* <li>{@link #letLongRec(long, ThLongObjToLongFunction)}</li>
* <li>{@link #letDoubleRec(double, ThDoubleObjToDoubleFunction)}</li>
* <li>{@link #letWith(Object, ThFunction)}</li>
* <li>{@link #letIntWith(Object, ThToIntFunction)}</li>
* <li>{@link #letLongWith(Object, ThToLongFunction)}</li>
* <li>{@link #letDoubleWith(Object, ThToDoubleFunction)}</li>
* <li>{@link #letWithResource(AutoCloseable, ThFunction)}</li>
* <li>{@link #letWith(Object, Object, ThBiFunction)}</li>
* <li>{@link #letWith(Object, Object, Object, ThTriFunction)}</li>
Expand Down Expand Up @@ -665,6 +671,66 @@ static <V, R> R letWith(final V value,
return block.asUnchecked().apply(value);
}

/**
* Performs given function block on given value and returns result.
* <pre>{@code
* int value = letWith("1234", it -> {
* System.out.println(it);
* return Integer.parseInt(it);
* });
* }</pre>
*
* @param value the value
* @param block the function block
* @param <V> the type of the value
* @return result
*/
static <V> int letIntWith(final V value,
final ThToIntFunction<? super V, ?> block) {
blockArgNotNull(block);
return block.asUnchecked().apply(value);
}

/**
* Performs given function block on given value and returns result.
* <pre>{@code
* long value = letWith("1234", it -> {
* System.out.println(it);
* return Long.parseLong(it);
* });
* }</pre>
*
* @param value the value
* @param block the function block
* @param <V> the type of the value
* @return result
*/
static <V> long letLongWith(final V value,
final ThToLongFunction<? super V, ?> block) {
blockArgNotNull(block);
return block.asUnchecked().apply(value);
}

/**
* Performs given function block on given value and returns result.
* <pre>{@code
* double value = letWith("1234.0", it -> {
* System.out.println(it);
* return Double.parseLong(it);
* });
* }</pre>
*
* @param value the value
* @param block the function block
* @param <V> the type of the value
* @return result
*/
static <V> double letDoubleWith(final V value,
final ThToDoubleFunction<? super V, ?> block) {
blockArgNotNull(block);
return block.asUnchecked().apply(value);
}

/**
* Performs given function block on {@link AutoCloseable} value, close this value and returns result.
* <pre>{@code
Expand Down
@@ -0,0 +1,47 @@
/*
* Copyright 2024 Evgenii Plugatar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.plugatar.jkscope.function;

/**
* The {@link java.util.function.Function} specialization that produces a {@code double}-valued result and might throw
* an exception
*
* @param <T> the type of the input argument
* @param <E> the type of the throwing exception
* @see java.util.function.Function
*/
@FunctionalInterface
public interface ThToDoubleFunction<T, E extends Throwable> {

/**
* Applies this function to the given argument.
*
* @param t the input argument
* @return result
* @throws E if function threw exception
*/
double apply(T t) throws E;

/**
* Returns this functions as an unchecked functions.
*
* @return unchecked function
*/
@SuppressWarnings("unchecked")
default ThToDoubleFunction<T, RuntimeException> asUnchecked() {
return (ThToDoubleFunction<T, RuntimeException>) this;
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/plugatar/jkscope/function/ThToIntFunction.java
@@ -0,0 +1,47 @@
/*
* Copyright 2024 Evgenii Plugatar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.plugatar.jkscope.function;

/**
* The {@link java.util.function.Function} specialization that produces an {@code int}-valued result and might throw an
* exception
*
* @param <T> the type of the input argument
* @param <E> the type of the throwing exception
* @see java.util.function.Function
*/
@FunctionalInterface
public interface ThToIntFunction<T, E extends Throwable> {

/**
* Applies this function to the given argument.
*
* @param t the input argument
* @return result
* @throws E if function threw exception
*/
int apply(T t) throws E;

/**
* Returns this functions as an unchecked functions.
*
* @return unchecked function
*/
@SuppressWarnings("unchecked")
default ThToIntFunction<T, RuntimeException> asUnchecked() {
return (ThToIntFunction<T, RuntimeException>) this;
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/plugatar/jkscope/function/ThToLongFunction.java
@@ -0,0 +1,47 @@
/*
* Copyright 2024 Evgenii Plugatar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.plugatar.jkscope.function;

/**
* The {@link java.util.function.Function} specialization that produces a {@code long}-valued result and might throw an
* exception
*
* @param <T> the type of the input argument
* @param <E> the type of the throwing exception
* @see java.util.function.Function
*/
@FunctionalInterface
public interface ThToLongFunction<T, E extends Throwable> {

/**
* Applies this function to the given argument.
*
* @param t the input argument
* @return result
* @throws E if function threw exception
*/
long apply(T t) throws E;

/**
* Returns this functions as an unchecked functions.
*
* @return unchecked function
*/
@SuppressWarnings("unchecked")
default ThToLongFunction<T, RuntimeException> asUnchecked() {
return (ThToLongFunction<T, RuntimeException>) this;
}
}
108 changes: 108 additions & 0 deletions src/test/java/com/plugatar/jkscope/JKScopeTest.java
Expand Up @@ -34,6 +34,9 @@
import com.plugatar.jkscope.function.ThPredicate;
import com.plugatar.jkscope.function.ThRunnable;
import com.plugatar.jkscope.function.ThSupplier;
import com.plugatar.jkscope.function.ThToDoubleFunction;
import com.plugatar.jkscope.function.ThToIntFunction;
import com.plugatar.jkscope.function.ThToLongFunction;
import com.plugatar.jkscope.function.ThTriConsumer;
import com.plugatar.jkscope.function.ThTriFunction;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -470,6 +473,33 @@ void letWithStaticMethodThrowsNPEForNullArg() {
.isInstanceOf(NullPointerException.class);
}

@Test
void letIntWithStaticMethodThrowsNPEForNullArg() {
final Object value = new Object();
final ThToIntFunction<Object, Throwable> block = null;

assertThatThrownBy(() -> JKScope.letIntWith(value, block))
.isInstanceOf(NullPointerException.class);
}

@Test
void letLongWithStaticMethodThrowsNPEForNullArg() {
final Object value = new Object();
final ThToLongFunction<Object, Throwable> block = null;

assertThatThrownBy(() -> JKScope.letLongWith(value, block))
.isInstanceOf(NullPointerException.class);
}

@Test
void letDoubleWithStaticMethodThrowsNPEForNullArg() {
final Object value = new Object();
final ThToDoubleFunction<Object, Throwable> block = null;

assertThatThrownBy(() -> JKScope.letDoubleWith(value, block))
.isInstanceOf(NullPointerException.class);
}

@Test
void letWithResourceMethodThrowsNPEForNullArg() {
final AutoCloseable value = new AutoCloseableImpl();
Expand Down Expand Up @@ -1105,6 +1135,84 @@ void letWithStaticMethodThrowsException() {
.isSameAs(throwable);
}

@Test
void letIntWithStaticMethod() {
final Object value = new Object();
final int result = 111;
final AtomicReference<Object> valueRef = new AtomicReference<>();
final ThToIntFunction<Object, Throwable> block = arg -> {
valueRef.set(arg);
return result;
};

assertThat(JKScope.letIntWith(value, block))
.isEqualTo(result);
assertThat(valueRef.get())
.isSameAs(value);
}

@Test
void letIntWithStaticMethodThrowsException() {
final Object value = new Object();
final Throwable throwable = new Throwable();
final ThToIntFunction<Object, Throwable> block = arg -> { throw throwable; };

assertThatThrownBy(() -> JKScope.letIntWith(value, block))
.isSameAs(throwable);
}

@Test
void letLongWithStaticMethod() {
final Object value = new Object();
final long result = 111L;
final AtomicReference<Object> valueRef = new AtomicReference<>();
final ThToLongFunction<Object, Throwable> block = arg -> {
valueRef.set(arg);
return result;
};

assertThat(JKScope.letLongWith(value, block))
.isEqualTo(result);
assertThat(valueRef.get())
.isSameAs(value);
}

@Test
void letLongWithStaticMethodThrowsException() {
final Object value = new Object();
final Throwable throwable = new Throwable();
final ThToLongFunction<Object, Throwable> block = arg -> { throw throwable; };

assertThatThrownBy(() -> JKScope.letLongWith(value, block))
.isSameAs(throwable);
}

@Test
void letDoubleWithStaticMethod() {
final Object value = new Object();
final double result = 111.0;
final AtomicReference<Object> valueRef = new AtomicReference<>();
final ThToDoubleFunction<Object, Throwable> block = arg -> {
valueRef.set(arg);
return result;
};

assertThat(JKScope.letDoubleWith(value, block))
.isEqualTo(result);
assertThat(valueRef.get())
.isSameAs(value);
}

@Test
void letDoubleWithStaticMethodThrowsException() {
final Object value = new Object();
final Throwable throwable = new Throwable();
final ThToDoubleFunction<Object, Throwable> block = arg -> { throw throwable; };

assertThatThrownBy(() -> JKScope.letDoubleWith(value, block))
.isSameAs(throwable);
}

@Test
void letWithResourceMethod() {
final AutoCloseableImpl value = new AutoCloseableImpl();
Expand Down

0 comments on commit 170b1f5

Please sign in to comment.