From 94f74de0d1b6e324ca5cfff4b983a82d4e72956f Mon Sep 17 00:00:00 2001 From: Mariia Kryvokhata Date: Thu, 19 Jun 2025 02:00:53 +0300 Subject: [PATCH 1/4] Add Result implementation in Java --- Java/Result/Result.java | 39 ++++++++++++++++++++++++++++++++++++ Java/Result/ResultUsage.java | 15 ++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 Java/Result/Result.java create mode 100644 Java/Result/ResultUsage.java diff --git a/Java/Result/Result.java b/Java/Result/Result.java new file mode 100644 index 0000000..e8a1023 --- /dev/null +++ b/Java/Result/Result.java @@ -0,0 +1,39 @@ +public class Result { + private final T value; + private final E error; + + public Result(T value, E error) { + this.value = value; + this.error = error; + } + + public static Result create(Object input) { + return (input instanceof Exception) + ? new Result<>(null, (E) input) + : new Result<>((T) input, null); + } + + public static Result fromValue(T value) { + return new Result<>(value, null); + } + + public static Result fromError(E error) { + return new Result<>(null, error); + } + + public boolean isSuccess() { + return error == null; + } + + public boolean isError() { + return error != null; + } + + public T getValue() { + return value; + } + + public E getError() { + return error; + } +} \ No newline at end of file diff --git a/Java/Result/ResultUsage.java b/Java/Result/ResultUsage.java new file mode 100644 index 0000000..ec5dfdf --- /dev/null +++ b/Java/Result/ResultUsage.java @@ -0,0 +1,15 @@ +public class ResultUsage { + public static void main(String[] args) { + // Usage + Result success = Result.create("Successfully received data"); + Result failure = Result.create(new Exception("Network error")); + + if (success.isSuccess()) { + System.out.println("Success: " + success.getValue()); + } + + if (failure.isError()) { + System.out.println("Failure: " + failure.getError().getMessage()); + } + } +} \ No newline at end of file From 3b5a0df5259d30e0e3c4ba6a09fe8d66aedc5d8e Mon Sep 17 00:00:00 2001 From: Mariia Kryvokhata Date: Thu, 19 Jun 2025 02:01:09 +0300 Subject: [PATCH 2/4] Add Either implementation in Java --- Java/Either/Either.java | 44 ++++++++++++++++++++++++++++++++++++ Java/Either/EitherUsage.java | 15 ++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 Java/Either/Either.java create mode 100644 Java/Either/EitherUsage.java diff --git a/Java/Either/Either.java b/Java/Either/Either.java new file mode 100644 index 0000000..6115680 --- /dev/null +++ b/Java/Either/Either.java @@ -0,0 +1,44 @@ +import java.util.function.Function; + +public class Either { + private final L left; + private final R right; + + private Either(L left, R right) { + this.left = left; + this.right = right; + } + + public static Either left(L value) { + return new Either<>(value, null); + } + + public static Either right(R value) { + return new Either<>(null, value); + } + + public L getLeft() { + return left; + } + + public R getRight() { + return right; + } + + public boolean isLeft() { + return left != null; + } + + public boolean isRight() { + return right != null; + } + + public Either map(Function fn) { + if (right == null) return Either.left(left); + return Either.right(fn.apply(right)); + } + + public T match(Function leftFn, Function rightFn) { + return isRight() ? rightFn.apply(right) : leftFn.apply(left); + } +} \ No newline at end of file diff --git a/Java/Either/EitherUsage.java b/Java/Either/EitherUsage.java new file mode 100644 index 0000000..f487cb9 --- /dev/null +++ b/Java/Either/EitherUsage.java @@ -0,0 +1,15 @@ +public class EitherUsage { + public static void main(String[] args) { + Either success = Either.right(42); + Either failure = Either.left(500); + + Either doubled = success.map(x -> x * 2); + System.out.println("doubled: " + doubled.getRight()); + + String result = failure.match( + error -> "Failure: " + error, + value -> "Success: " + value + ); + System.out.println("result: " + result); + } +} \ No newline at end of file From 47274212392dcb0b4747a12546ae4cf86b135902 Mon Sep 17 00:00:00 2001 From: Mariia Kryvokhata Date: Thu, 19 Jun 2025 02:01:42 +0300 Subject: [PATCH 3/4] Add Maybe implementation in Java --- Java/Maybe/Maybe.java | 26 ++++++++++++++++++++++++++ Java/Maybe/MaybeUsage.java | 27 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 Java/Maybe/Maybe.java create mode 100644 Java/Maybe/MaybeUsage.java diff --git a/Java/Maybe/Maybe.java b/Java/Maybe/Maybe.java new file mode 100644 index 0000000..fadd833 --- /dev/null +++ b/Java/Maybe/Maybe.java @@ -0,0 +1,26 @@ +import java.util.function.Function; +import java.util.function.Supplier; + +public class Maybe { + private final T value; + + public Maybe() { + this.value = null; + } + + public Maybe(T value) { + this.value = value; + } + + public T getValue() { + return value; + } + + public boolean isEmpty() { + return value == null; + } + + public R match(Function someFn, Supplier noneFn) { + return isEmpty() ? noneFn.get() : someFn.apply(value); + } +} \ No newline at end of file diff --git a/Java/Maybe/MaybeUsage.java b/Java/Maybe/MaybeUsage.java new file mode 100644 index 0000000..df03f48 --- /dev/null +++ b/Java/Maybe/MaybeUsage.java @@ -0,0 +1,27 @@ +public class MaybeUsage { + public static void main(String[] args) { + Maybe some = new Maybe<>(42); + Maybe none = new Maybe<>(); + + System.out.println(some); + System.out.println(none); + + System.out.println(some.isEmpty()); + System.out.println(none.isEmpty()); + + System.out.println(some.getValue()); + System.out.println(none.getValue()); + + String res1 = some.match( + v -> "Got value " + v, + () -> "No value" + ); + System.out.println(res1); + + String res2 = none.match( + v -> "Got value " + v, + () -> "No value" + ); + System.out.println(res2); + } +} \ No newline at end of file From 9254e0e2c6d8887c0fed401089cc1410399165dc Mon Sep 17 00:00:00 2001 From: Mariia Kryvokhata Date: Thu, 19 Jun 2025 02:57:29 +0300 Subject: [PATCH 4/4] Add one more usage example of Result --- Java/Result/Result.java | 4 ++-- Java/Result/ResultUsage.java | 30 ++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Java/Result/Result.java b/Java/Result/Result.java index e8a1023..73d2b24 100644 --- a/Java/Result/Result.java +++ b/Java/Result/Result.java @@ -17,7 +17,7 @@ public static Result fromValue(T value) { return new Result<>(value, null); } - public static Result fromError(E error) { + public static Result fromError(E error) { return new Result<>(null, error); } @@ -36,4 +36,4 @@ public T getValue() { public E getError() { return error; } -} \ No newline at end of file +} diff --git a/Java/Result/ResultUsage.java b/Java/Result/ResultUsage.java index ec5dfdf..f8d164c 100644 --- a/Java/Result/ResultUsage.java +++ b/Java/Result/ResultUsage.java @@ -1,6 +1,6 @@ public class ResultUsage { public static void main(String[] args) { - // Usage + // Basic usage Result success = Result.create("Successfully received data"); Result failure = Result.create(new Exception("Network error")); @@ -11,5 +11,31 @@ public static void main(String[] args) { if (failure.isError()) { System.out.println("Failure: " + failure.getError().getMessage()); } + + // Additional usage example + // Handling a method that may throw and wrapping the result + Result result1 = parseIntSafe("123"); + Result result2 = parseIntSafe("abc"); + + if (result1.isSuccess()) { + System.out.println("Parsed value: " + result1.getValue()); + } else { + System.out.println("Parse error: " + result1.getError().getMessage()); + } + + if (result2.isSuccess()) { + System.out.println("Parsed value: " + result2.getValue()); + } else { + System.out.println("Parse error: " + result2.getError().getMessage()); + } + } + + public static Result parseIntSafe(String input) { + try { + int value = Integer.parseInt(input); + return Result.fromValue(value); + } catch (Exception e) { + return Result.fromError(e); + } } -} \ No newline at end of file +}