From 6cf7a62e08cbb2ac9f06b5c069ac84349bbbdfdc Mon Sep 17 00:00:00 2001 From: Hakky54 Date: Sun, 10 Nov 2024 11:07:29 +0100 Subject: [PATCH] Refactored to lazy mapping --- .../main/java/nl/altindag/ssl/SSLFactory.java | 2 +- .../main/java/nl/altindag/ssl/util/Box.java | 47 ++++++++++++++----- .../java/nl/altindag/ssl/util/BoxShould.java | 10 ++-- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/sslcontext-kickstart/src/main/java/nl/altindag/ssl/SSLFactory.java b/sslcontext-kickstart/src/main/java/nl/altindag/ssl/SSLFactory.java index 8916f256..aa9c2780 100644 --- a/sslcontext-kickstart/src/main/java/nl/altindag/ssl/SSLFactory.java +++ b/sslcontext-kickstart/src/main/java/nl/altindag/ssl/SSLFactory.java @@ -167,7 +167,7 @@ public SSLEngine getSSLEngine(String peerHost, Integer peerPort) { * source in a functional way. */ public Box map(Function mapper) { - return new Box<>(this).map(mapper); + return Box.of(this).map(mapper); } public static Builder builder() { diff --git a/sslcontext-kickstart/src/main/java/nl/altindag/ssl/util/Box.java b/sslcontext-kickstart/src/main/java/nl/altindag/ssl/util/Box.java index 3a55f6e4..2a7249f9 100644 --- a/sslcontext-kickstart/src/main/java/nl/altindag/ssl/util/Box.java +++ b/sslcontext-kickstart/src/main/java/nl/altindag/ssl/util/Box.java @@ -17,27 +17,48 @@ import nl.altindag.ssl.exception.GenericException; +import java.util.function.Supplier; + /** * @author Hakan Altindag */ -public final class Box { +@FunctionalInterface +public interface Box { - private final T value; + ValueHolder container(); - public Box(T value) { - this.value = value; + static Box of(T value) { + return () -> ValueHolder.wrap(() -> value); } - public Box map(Function mapper) { - try { - return new Box<>(mapper.apply(value)); - } catch (Exception e) { - throw new GenericException(e); - } + default Box map(Function mapper) { + return () -> ValueHolder.wrap(() -> { + final T value = container().get(); + + try { + return mapper.apply(value); + } catch (Exception e) { + throw new GenericException(e); + } + }); + } + + default T get() { + return container().get(); } - public T get() { - return value; + @FunctionalInterface + interface ValueHolder { + + Supplier valueSupplier(); + + default T get() { + return valueSupplier().get(); + } + + static ValueHolder wrap(Supplier supplier) { + return () -> supplier; + } } -} +} \ No newline at end of file diff --git a/sslcontext-kickstart/src/test/java/nl/altindag/ssl/util/BoxShould.java b/sslcontext-kickstart/src/test/java/nl/altindag/ssl/util/BoxShould.java index df2a0450..a89025be 100644 --- a/sslcontext-kickstart/src/test/java/nl/altindag/ssl/util/BoxShould.java +++ b/sslcontext-kickstart/src/test/java/nl/altindag/ssl/util/BoxShould.java @@ -27,10 +27,12 @@ class BoxShould { @Test void wrapAnyExceptionInGenericException() { - Box stringBox = new Box<>(""); - assertThatThrownBy(() -> stringBox.map(value -> { - throw new RuntimeException("KABOOM"); - })) + Box stringBox = Box.of("") + .map(value -> { + throw new RuntimeException("KABOOM"); + }); + + assertThatThrownBy(stringBox::get) .isInstanceOf(GenericException.class) .hasRootCauseMessage("KABOOM"); }