diff --git a/src/main/java/com/plugatar/jkscope/JKScope.java b/src/main/java/com/plugatar/jkscope/JKScope.java index 8360987..df14410 100644 --- a/src/main/java/com/plugatar/jkscope/JKScope.java +++ b/src/main/java/com/plugatar/jkscope/JKScope.java @@ -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; @@ -95,6 +98,9 @@ *
  • {@link #letLongRec(long, ThLongObjToLongFunction)}
  • *
  • {@link #letDoubleRec(double, ThDoubleObjToDoubleFunction)}
  • *
  • {@link #letWith(Object, ThFunction)}
  • + *
  • {@link #letIntWith(Object, ThToIntFunction)}
  • + *
  • {@link #letLongWith(Object, ThToLongFunction)}
  • + *
  • {@link #letDoubleWith(Object, ThToDoubleFunction)}
  • *
  • {@link #letWithResource(AutoCloseable, ThFunction)}
  • *
  • {@link #letWith(Object, Object, ThBiFunction)}
  • *
  • {@link #letWith(Object, Object, Object, ThTriFunction)}
  • @@ -665,6 +671,66 @@ static R letWith(final V value, return block.asUnchecked().apply(value); } + /** + * Performs given function block on given value and returns result. + *
    {@code
    +   * int value = letWith("1234", it -> {
    +   *   System.out.println(it);
    +   *   return Integer.parseInt(it);
    +   * });
    +   * }
    + * + * @param value the value + * @param block the function block + * @param the type of the value + * @return result + */ + static int letIntWith(final V value, + final ThToIntFunction block) { + blockArgNotNull(block); + return block.asUnchecked().apply(value); + } + + /** + * Performs given function block on given value and returns result. + *
    {@code
    +   * long value = letWith("1234", it -> {
    +   *   System.out.println(it);
    +   *   return Long.parseLong(it);
    +   * });
    +   * }
    + * + * @param value the value + * @param block the function block + * @param the type of the value + * @return result + */ + static long letLongWith(final V value, + final ThToLongFunction block) { + blockArgNotNull(block); + return block.asUnchecked().apply(value); + } + + /** + * Performs given function block on given value and returns result. + *
    {@code
    +   * double value = letWith("1234.0", it -> {
    +   *   System.out.println(it);
    +   *   return Double.parseLong(it);
    +   * });
    +   * }
    + * + * @param value the value + * @param block the function block + * @param the type of the value + * @return result + */ + static double letDoubleWith(final V value, + final ThToDoubleFunction block) { + blockArgNotNull(block); + return block.asUnchecked().apply(value); + } + /** * Performs given function block on {@link AutoCloseable} value, close this value and returns result. *
    {@code
    diff --git a/src/main/java/com/plugatar/jkscope/function/ThToDoubleFunction.java b/src/main/java/com/plugatar/jkscope/function/ThToDoubleFunction.java
    new file mode 100644
    index 0000000..1bf7b44
    --- /dev/null
    +++ b/src/main/java/com/plugatar/jkscope/function/ThToDoubleFunction.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 double}-valued result and might throw
    + * an exception
    + *
    + * @param  the type of the input argument
    + * @param  the type of the throwing exception
    + * @see java.util.function.Function
    + */
    +@FunctionalInterface
    +public interface ThToDoubleFunction {
    +
    +  /**
    +   * 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 asUnchecked() {
    +    return (ThToDoubleFunction) this;
    +  }
    +}
    diff --git a/src/main/java/com/plugatar/jkscope/function/ThToIntFunction.java b/src/main/java/com/plugatar/jkscope/function/ThToIntFunction.java
    new file mode 100644
    index 0000000..772eca8
    --- /dev/null
    +++ b/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  the type of the input argument
    + * @param  the type of the throwing exception
    + * @see java.util.function.Function
    + */
    +@FunctionalInterface
    +public interface ThToIntFunction {
    +
    +  /**
    +   * 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 asUnchecked() {
    +    return (ThToIntFunction) this;
    +  }
    +}
    diff --git a/src/main/java/com/plugatar/jkscope/function/ThToLongFunction.java b/src/main/java/com/plugatar/jkscope/function/ThToLongFunction.java
    new file mode 100644
    index 0000000..65e1c01
    --- /dev/null
    +++ b/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  the type of the input argument
    + * @param  the type of the throwing exception
    + * @see java.util.function.Function
    + */
    +@FunctionalInterface
    +public interface ThToLongFunction {
    +
    +  /**
    +   * 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 asUnchecked() {
    +    return (ThToLongFunction) this;
    +  }
    +}
    diff --git a/src/test/java/com/plugatar/jkscope/JKScopeTest.java b/src/test/java/com/plugatar/jkscope/JKScopeTest.java
    index ebf769d..f758b64 100644
    --- a/src/test/java/com/plugatar/jkscope/JKScopeTest.java
    +++ b/src/test/java/com/plugatar/jkscope/JKScopeTest.java
    @@ -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;
    @@ -470,6 +473,33 @@ void letWithStaticMethodThrowsNPEForNullArg() {
           .isInstanceOf(NullPointerException.class);
       }
     
    +  @Test
    +  void letIntWithStaticMethodThrowsNPEForNullArg() {
    +    final Object value = new Object();
    +    final ThToIntFunction block = null;
    +
    +    assertThatThrownBy(() -> JKScope.letIntWith(value, block))
    +      .isInstanceOf(NullPointerException.class);
    +  }
    +
    +  @Test
    +  void letLongWithStaticMethodThrowsNPEForNullArg() {
    +    final Object value = new Object();
    +    final ThToLongFunction block = null;
    +
    +    assertThatThrownBy(() -> JKScope.letLongWith(value, block))
    +      .isInstanceOf(NullPointerException.class);
    +  }
    +
    +  @Test
    +  void letDoubleWithStaticMethodThrowsNPEForNullArg() {
    +    final Object value = new Object();
    +    final ThToDoubleFunction block = null;
    +
    +    assertThatThrownBy(() -> JKScope.letDoubleWith(value, block))
    +      .isInstanceOf(NullPointerException.class);
    +  }
    +
       @Test
       void letWithResourceMethodThrowsNPEForNullArg() {
         final AutoCloseable value = new AutoCloseableImpl();
    @@ -1105,6 +1135,84 @@ void letWithStaticMethodThrowsException() {
           .isSameAs(throwable);
       }
     
    +  @Test
    +  void letIntWithStaticMethod() {
    +    final Object value = new Object();
    +    final int result = 111;
    +    final AtomicReference valueRef = new AtomicReference<>();
    +    final ThToIntFunction 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 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 valueRef = new AtomicReference<>();
    +    final ThToLongFunction 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 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 valueRef = new AtomicReference<>();
    +    final ThToDoubleFunction 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 block = arg -> { throw throwable; };
    +
    +    assertThatThrownBy(() -> JKScope.letDoubleWith(value, block))
    +      .isSameAs(throwable);
    +  }
    +
       @Test
       void letWithResourceMethod() {
         final AutoCloseableImpl value = new AutoCloseableImpl();
    diff --git a/src/test/java/com/plugatar/jkscope/function/ThToDoubleFunctionTest.java b/src/test/java/com/plugatar/jkscope/function/ThToDoubleFunctionTest.java
    new file mode 100644
    index 0000000..8681fda
    --- /dev/null
    +++ b/src/test/java/com/plugatar/jkscope/function/ThToDoubleFunctionTest.java
    @@ -0,0 +1,57 @@
    +/*
    + * 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;
    +
    +import org.junit.jupiter.api.Test;
    +
    +import java.util.concurrent.atomic.AtomicReference;
    +
    +import static org.assertj.core.api.Assertions.assertThat;
    +import static org.assertj.core.api.Assertions.assertThatThrownBy;
    +
    +/**
    + * Tests for {@link ThToDoubleFunction}.
    + */
    +final class ThToDoubleFunctionTest {
    +
    +  @Test
    +  void asUncheckedMethod() {
    +    final Object value = new Object();
    +    final AtomicReference valueRef = new AtomicReference<>();
    +    final double result = 111.0;
    +    final ThToDoubleFunction origin = arg -> {
    +      valueRef.set(arg);
    +      return result;
    +    };
    +
    +    final ThToDoubleFunction unchecked = origin.asUnchecked();
    +    assertThat(unchecked.apply(value))
    +      .isEqualTo(result);
    +    assertThat(valueRef.get())
    +      .isSameAs(value);
    +  }
    +
    +  @Test
    +  void asUncheckedMethodThrowsException() {
    +    final Object value = new Object();
    +    final Throwable throwable = new Throwable();
    +    final ThToDoubleFunction origin = arg -> { throw throwable; };
    +
    +    final ThToDoubleFunction unchecked = origin.asUnchecked();
    +    assertThatThrownBy(() -> unchecked.apply(value))
    +      .isSameAs(throwable);
    +  }
    +}
    diff --git a/src/test/java/com/plugatar/jkscope/function/ThToIntFunctionTest.java b/src/test/java/com/plugatar/jkscope/function/ThToIntFunctionTest.java
    new file mode 100644
    index 0000000..03566a9
    --- /dev/null
    +++ b/src/test/java/com/plugatar/jkscope/function/ThToIntFunctionTest.java
    @@ -0,0 +1,57 @@
    +/*
    + * 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;
    +
    +import org.junit.jupiter.api.Test;
    +
    +import java.util.concurrent.atomic.AtomicReference;
    +
    +import static org.assertj.core.api.Assertions.assertThat;
    +import static org.assertj.core.api.Assertions.assertThatThrownBy;
    +
    +/**
    + * Tests for {@link ThToIntFunction}.
    + */
    +final class ThToIntFunctionTest {
    +
    +  @Test
    +  void asUncheckedMethod() {
    +    final Object value = new Object();
    +    final AtomicReference valueRef = new AtomicReference<>();
    +    final int result = 111;
    +    final ThToIntFunction origin = arg -> {
    +      valueRef.set(arg);
    +      return result;
    +    };
    +
    +    final ThToIntFunction unchecked = origin.asUnchecked();
    +    assertThat(unchecked.apply(value))
    +      .isEqualTo(result);
    +    assertThat(valueRef.get())
    +      .isSameAs(value);
    +  }
    +
    +  @Test
    +  void asUncheckedMethodThrowsException() {
    +    final Object value = new Object();
    +    final Throwable throwable = new Throwable();
    +    final ThToIntFunction origin = arg -> { throw throwable; };
    +
    +    final ThToIntFunction unchecked = origin.asUnchecked();
    +    assertThatThrownBy(() -> unchecked.apply(value))
    +      .isSameAs(throwable);
    +  }
    +}
    diff --git a/src/test/java/com/plugatar/jkscope/function/ThToLongFunctionTest.java b/src/test/java/com/plugatar/jkscope/function/ThToLongFunctionTest.java
    new file mode 100644
    index 0000000..2eb6770
    --- /dev/null
    +++ b/src/test/java/com/plugatar/jkscope/function/ThToLongFunctionTest.java
    @@ -0,0 +1,57 @@
    +/*
    + * 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;
    +
    +import org.junit.jupiter.api.Test;
    +
    +import java.util.concurrent.atomic.AtomicReference;
    +
    +import static org.assertj.core.api.Assertions.assertThat;
    +import static org.assertj.core.api.Assertions.assertThatThrownBy;
    +
    +/**
    + * Tests for {@link ThToLongFunction}.
    + */
    +final class ThToLongFunctionTest {
    +
    +  @Test
    +  void asUncheckedMethod() {
    +    final Object value = new Object();
    +    final AtomicReference valueRef = new AtomicReference<>();
    +    final long result = 111L;
    +    final ThToLongFunction origin = arg -> {
    +      valueRef.set(arg);
    +      return result;
    +    };
    +
    +    final ThToLongFunction unchecked = origin.asUnchecked();
    +    assertThat(unchecked.apply(value))
    +      .isEqualTo(result);
    +    assertThat(valueRef.get())
    +      .isSameAs(value);
    +  }
    +
    +  @Test
    +  void asUncheckedMethodThrowsException() {
    +    final Object value = new Object();
    +    final Throwable throwable = new Throwable();
    +    final ThToLongFunction origin = arg -> { throw throwable; };
    +
    +    final ThToLongFunction unchecked = origin.asUnchecked();
    +    assertThatThrownBy(() -> unchecked.apply(value))
    +      .isSameAs(throwable);
    +  }
    +}