From bc7ee2539bf270a700a7c9deaea82b9a53f4e659 Mon Sep 17 00:00:00 2001 From: Semyon Kirekov Date: Tue, 3 Aug 2021 20:24:06 +0300 Subject: [PATCH] [#142] - Delete 'Lazy' monad --- src/main/java/com/kirekov/juu/monad/Lazy.java | 93 ------------------- .../java/com/kirekov/juu/monad/LazyTest.java | 73 --------------- 2 files changed, 166 deletions(-) delete mode 100644 src/main/java/com/kirekov/juu/monad/Lazy.java delete mode 100644 src/test/java/com/kirekov/juu/monad/LazyTest.java diff --git a/src/main/java/com/kirekov/juu/monad/Lazy.java b/src/main/java/com/kirekov/juu/monad/Lazy.java deleted file mode 100644 index b01c8de..0000000 --- a/src/main/java/com/kirekov/juu/monad/Lazy.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.kirekov.juu.monad; - -import com.kirekov.juu.collection.Streaming; -import java.util.Objects; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.Stream; - -/** - * Monad for lazy calculations. Allows to build a chain of operations with deferred execution. - * Operations will NOT be executed until {@link Lazy#calculate()} method will be called. - * - * @param the type of the return parameter - * @since 1.0 - */ -public final class Lazy implements Streaming { - - private final Supplier supplier; - - private Lazy(Supplier supplier) { - this.supplier = supplier; - } - - /** - * Instantiates new Lazy object. - * - * @param supplier supplier which will be called by {@link Lazy#calculate()} method - * @param the type of the return parameter - * @return lazy object - * @throws NullPointerException if supplier is null - */ - public static Lazy of(Supplier supplier) { - return new Lazy<>(Objects.requireNonNull(supplier)); - } - - /** - * Makes the chain with input parameter of previous execution.
- * NB: map does not trigger execution. - * - * @param mapper mapping function - * @param the return type - * @return lazy object - * @throws NullPointerException if mapper is null - */ - public Lazy map(Function mapper) { - Objects.requireNonNull(mapper); - return Lazy.of(() -> mapper.apply(supplier.get())); - } - - /** - * Makes the chain with input parameter of previous execution. - *
- * NB: flatMap does not trigger the execution. - *
- * After triggering the execution flatMap returns not the lazy object, but the result of its inner - * calculation. - *
 {@code
-   * Lazy.of(() -> 1)
-   *     .flatMap(v -> Lazy.of(() -> v + 1))
-   *     .calculate()
-   * }
- * returns 2 - * - * @param mapper mapping function - * @param the return type - * @return lazy object - * @throws NullPointerException if mapper is null - */ - public Lazy flatMap(Function> mapper) { - Objects.requireNonNull(mapper); - return Lazy.of(() -> mapper.apply(supplier.get()).calculate()); - } - - /** - * Triggers calculation and returns the result. - * - * @return the result of calculation - */ - public T calculate() { - return supplier.get(); - } - - /** - * Triggers calculation and returns the result as {@link Stream}. - * - * @return the stream of calculation result - * @see Lazy#calculate() - */ - @Override - public Stream stream() { - return Stream.of(calculate()); - } -} diff --git a/src/test/java/com/kirekov/juu/monad/LazyTest.java b/src/test/java/com/kirekov/juu/monad/LazyTest.java deleted file mode 100644 index dc64237..0000000 --- a/src/test/java/com/kirekov/juu/monad/LazyTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.kirekov.juu.monad; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import java.util.Collection; -import java.util.Optional; -import org.junit.jupiter.api.Test; - -/** - * Test cases for {@linkplain Lazy}. - * - * @see Lazy - */ -class LazyTest { - - @Test - void ofThrowsNullPointerExceptionIfSupplierIsNull() { - assertThrows(NullPointerException.class, () -> Lazy.of(null)); - } - - @Test - void testLazyReallyActsLazy() { - @SuppressWarnings("unchecked") - Collection collection = (Collection) mock(Collection.class); - Lazy lazy = - Lazy.of(() -> { - int val = 1; - collection.add(val); - return val; - }).map(v -> { - int val = v + 1; - collection.add(val); - return val; - }).map(v -> { - int val = v + 1; - collection.add(val); - return val; - }).flatMap(v -> { - int val = v + 1; - collection.add(val); - return Lazy.of(() -> val); - }); - verify(collection, times(0)).add(1); - verify(collection, times(0)).add(2); - verify(collection, times(0)).add(3); - verify(collection, times(0)).add(4); - - int result = lazy.calculate(); - - assertEquals(4, result); - verify(collection, times(1)).add(1); - verify(collection, times(1)).add(2); - verify(collection, times(1)).add(3); - verify(collection, times(1)).add(4); - } - - @Test - void testStream() { - Optional opt = - Lazy.of(() -> "1") - .map(v -> v + "2") - .map(v -> v + "3") - .stream() - .findFirst(); - assertTrue(opt.isPresent()); - assertEquals("123", opt.get()); - } -} \ No newline at end of file