diff --git a/lib/src/main/java/com/github/tonivade/vavr/effect/Fiber.java b/lib/src/main/java/com/github/tonivade/vavr/effect/Fiber.java index 4270cab..d934c88 100644 --- a/lib/src/main/java/com/github/tonivade/vavr/effect/Fiber.java +++ b/lib/src/main/java/com/github/tonivade/vavr/effect/Fiber.java @@ -1,25 +1,18 @@ /* - * Copyright (c) 2018-2021, Antonio Gabriel Muñoz Conejo + * Copyright (c) 2022, Antonio Gabriel Muñoz Conejo * Distributed under the terms of the MIT License */ package com.github.tonivade.vavr.effect; -public interface Fiber { +public sealed interface Fiber { IO join(); IO cancel(); static Fiber of(IO join, IO cancel) { - return new Fiber() { - @Override - public IO join() { - return join; - } - @Override - public IO cancel() { - return cancel; - } - }; + return new FiberImpl<>(join, cancel); } + + record FiberImpl(IO join, IO cancel) implements Fiber {} } \ No newline at end of file diff --git a/lib/src/main/java/com/github/tonivade/vavr/effect/IO.java b/lib/src/main/java/com/github/tonivade/vavr/effect/IO.java index ce9ad3c..aa179af 100644 --- a/lib/src/main/java/com/github/tonivade/vavr/effect/IO.java +++ b/lib/src/main/java/com/github/tonivade/vavr/effect/IO.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2021, Antonio Gabriel Muñoz Conejo + * Copyright (c) 2022, Antonio Gabriel Muñoz Conejo * Distributed under the terms of the MIT License */ package com.github.tonivade.vavr.effect; diff --git a/lib/src/main/java/com/github/tonivade/vavr/effect/Ref.java b/lib/src/main/java/com/github/tonivade/vavr/effect/Ref.java index 175ece3..5dd2abf 100644 --- a/lib/src/main/java/com/github/tonivade/vavr/effect/Ref.java +++ b/lib/src/main/java/com/github/tonivade/vavr/effect/Ref.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2021, Antonio Gabriel Muñoz Conejo + * Copyright (c) 2022, Antonio Gabriel Muñoz Conejo * Distributed under the terms of the MIT License */ package com.github.tonivade.vavr.effect; diff --git a/lib/src/main/java/com/github/tonivade/vavr/effect/Unit.java b/lib/src/main/java/com/github/tonivade/vavr/effect/Unit.java index 0f7c8c6..11b3583 100644 --- a/lib/src/main/java/com/github/tonivade/vavr/effect/Unit.java +++ b/lib/src/main/java/com/github/tonivade/vavr/effect/Unit.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2021, Antonio Gabriel Muñoz Conejo + * Copyright (c) 2022, Antonio Gabriel Muñoz Conejo * Distributed under the terms of the MIT License */ package com.github.tonivade.vavr.effect; diff --git a/lib/src/test/java/com/github/tonivade/vavr/effect/IOTest.java b/lib/src/test/java/com/github/tonivade/vavr/effect/IOTest.java index 94f0931..59d9477 100644 --- a/lib/src/test/java/com/github/tonivade/vavr/effect/IOTest.java +++ b/lib/src/test/java/com/github/tonivade/vavr/effect/IOTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2021, Antonio Gabriel Muñoz Conejo + * Copyright (c) 2022, Antonio Gabriel Muñoz Conejo * Distributed under the terms of the MIT License */ package com.github.tonivade.vavr.effect; @@ -37,10 +37,10 @@ import io.vavr.control.Try; @ExtendWith(MockitoExtension.class) -public class IOTest { +class IOTest { @Test - public void pure() { + void pure() { IO pure = IO.pure("hola mundo"); assertAll( @@ -52,7 +52,7 @@ } @Test - public void asyncSuccess() { + void asyncSuccess() { IO async = IO.async(callback -> { System.out.println(Thread.currentThread().getName()); Thread.sleep(100); @@ -65,7 +65,7 @@ } @Test - public void asyncFailure() { + void asyncFailure() { IO async = IO.async(callback -> { Thread.sleep(100); callback.accept(Try.failure(new UnsupportedOperationException())); @@ -77,7 +77,7 @@ } // @Test -// public void echo() { +// void echo() { // IO echo = narrowK(console.println("write your name")) // .andThen(narrowK(console.readln())) // .flatMap(name -> narrowK(console.println("Hello " + name))) @@ -91,7 +91,7 @@ // } @Test - public void safeRunAsync() { + void safeRunAsync() { IO> program = currentThreadIO(); Try> result = program.runAsync().await(1, TimeUnit.SECONDS).toTry(); @@ -100,7 +100,7 @@ } @Test - public void bracket() throws SQLException { + void bracket() throws SQLException { ResultSet resultSet = mock(ResultSet.class); when(resultSet.getString("id")).thenReturn("value"); @@ -111,14 +111,14 @@ } @Test - public void safeRunAsyncSuccess(@Mock Consumer> callback) { + void safeRunAsyncSuccess(@Mock Consumer> callback) { IO.pure("hola").safeRunAsync(callback); verify(callback, timeout(1000)).accept(Try.success("hola")); } @Test - public void unsafeRunAsyncFailure(@Mock Consumer> callback) { + void unsafeRunAsyncFailure(@Mock Consumer> callback) { RuntimeException error = new RuntimeException(); IO.raiseError(error).safeRunAsync(callback); @@ -127,14 +127,14 @@ } @Test - public void recover() { + void recover() { IO recover = IO.raiseError(new RuntimeException()).recover(error -> "hola mundo"); assertEquals("hola mundo", recover.unsafeRunSync()); } @Test - public void recoverWith() { + void recoverWith() { IO recover = IO.raiseError(new IllegalArgumentException()) .recover(IllegalArgumentException.class, error -> "hola mundo"); @@ -142,7 +142,7 @@ } @Test - public void recoverWithNotMatch() { + void recoverWithNotMatch() { IO recover = IO.raiseError(new IllegalArgumentException()) .recover(NoSuchElementException.class, error -> "hola mundo"); @@ -150,7 +150,7 @@ } @Test - public void retry(@Mock Supplier computation) { + void retry(@Mock Supplier computation) { when(computation.get()).thenThrow(UnsupportedOperationException.class); Try retry = IO.task(computation).retry().safeRunSync(); @@ -160,7 +160,7 @@ } @Test - public void retryFailure(@Mock Supplier computation) { + void retryFailure(@Mock Supplier computation) { when(computation.get()).thenThrow(UnsupportedOperationException.class); Try retry = IO.task(computation).retry(Duration.ofMillis(100), 3).safeRunSync(); @@ -170,7 +170,7 @@ } @Test - public void retrySuccess(@Mock Supplier computation) { + void retrySuccess(@Mock Supplier computation) { when(computation.get()) .thenThrow(UnsupportedOperationException.class) .thenThrow(UnsupportedOperationException.class) @@ -184,7 +184,7 @@ } @Test - public void repeatSuccess(@Mock Supplier computation) { + void repeatSuccess(@Mock Supplier computation) { when(computation.get()).thenReturn("hola"); Try repeat = IO.task(computation).repeat(Duration.ofMillis(100), 3).safeRunSync(); @@ -194,7 +194,7 @@ } @Test - public void repeatFailure(@Mock Supplier computation) { + void repeatFailure(@Mock Supplier computation) { when(computation.get()).thenReturn("hola").thenThrow(UnsupportedOperationException.class); Try repeat = IO.task(computation).repeat(Duration.ofMillis(100), 3).safeRunSync(); @@ -204,7 +204,7 @@ } @Test - public void repeat(@Mock Supplier computation) { + void repeat(@Mock Supplier computation) { when(computation.get()).thenReturn("hola"); Try repeat = IO.task(computation).repeat().safeRunSync(); @@ -214,7 +214,7 @@ } @Test - public void flatMapped() { + void flatMapped() { IO io = IO.unit() .map(ignore -> "hola") .map(ignore -> "hola") @@ -225,7 +225,7 @@ } @Test - public void stackSafety() { + void stackSafety() { IO sum = sum(100000, 0); Future futureSum = sum.runAsync(); @@ -235,7 +235,7 @@ } @Test - public void timed() { + void timed() { IO> sum = sum(100000, 0).timed(); Tuple2 result = sum.unsafeRunSync(); @@ -245,17 +245,17 @@ } @Test - public void timeoutFail() { + void timeoutFail() { assertThrows(TimeoutException.class, IO.never().timeout(Duration.ofSeconds(1))::unsafeRunSync); } @Test - public void timeoutSuccess() { + void timeoutSuccess() { assertEquals(1, IO.pure(1).timeout(Duration.ofSeconds(1)).unsafeRunSync()); } @Test - public void traverse() { + void traverse() { IO left = IO.task(() -> "left"); IO right = IO.task(() -> "right"); @@ -265,7 +265,7 @@ } @Test - public void raceA() { + void raceA() { IO> race = IO.race( IO.delay(Duration.ofMillis(10), () -> 10), IO.delay(Duration.ofMillis(100), () -> "b")); @@ -276,7 +276,7 @@ } @Test - public void raceB() { + void raceB() { IO> race = IO.race( IO.delay(Duration.ofMillis(100), () -> 10), IO.delay(Duration.ofMillis(10), () -> "b")); @@ -287,7 +287,7 @@ } @Test - public void fork() { + void fork() { IO result = IO.pure("hola") .flatMap(hello -> IO.delay(Duration.ofSeconds(1), () -> hello + " toni").fork()) .flatMap(Fiber::join); @@ -298,7 +298,7 @@ } @Test - public void memoize(@Mock Function1 toUpperCase) { + void memoize(@Mock Function1 toUpperCase) { when(toUpperCase.apply(any())) .thenAnswer(args -> args.getArgument(0, String.class).toUpperCase()); @@ -314,7 +314,7 @@ } @Test - public void fibSyncTest() { + void fibSyncTest() { assertAll( () -> assertEquals(1, fibSync(1).unsafeRunSync()), () -> assertEquals(1, fibSync(2).unsafeRunSync()), @@ -330,7 +330,7 @@ } @Test - public void fibAsyncTest() { + void fibAsyncTest() { assertAll( () -> assertEquals(1, fibAsync(1).unsafeRunSync()), () -> assertEquals(1, fibAsync(2).unsafeRunSync()),