|
| 1 | +# Safe Java Streams |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +[](https://adudko.github.io/safe-java-streams/) |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +`safe-java-streams` is a lightweight Java library that adds functional utilities for exception handling and `Either`-style value processing, enabling cleaner, safer and more predictable code. |
| 18 | + |
| 19 | +It helps: |
| 20 | + |
| 21 | +- Eliminate duplicate `try/catch` blocks in `Stream API` |
| 22 | +- Explicitly handle errors without exceptions via `Either` |
| 23 | +- Use `Optional` and `Either` instead of `null` and `throw` |
| 24 | +- Make Java a bit more functional |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## 📦 Project Setup (Maven) |
| 29 | + |
| 30 | +### 1. Add GitHub Packages repository |
| 31 | + |
| 32 | +```xml |
| 33 | +<repositories> |
| 34 | + <repository> |
| 35 | + <id>github</id> |
| 36 | + <name>GitHub Packages</name> |
| 37 | + <url>https://maven.pkg.github.com/aDudko/safe-java-streams</url> |
| 38 | + </repository> |
| 39 | +</repositories> |
| 40 | +``` |
| 41 | + |
| 42 | +### 2. Add dependency |
| 43 | + |
| 44 | +```xml |
| 45 | +<dependency> |
| 46 | + <groupId>io.github.aDudko</groupId> |
| 47 | + <artifactId>safe-java-streams</artifactId> |
| 48 | + <version>1.0.0</version> |
| 49 | +</dependency> |
| 50 | +``` |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## 🔍 Key Features |
| 55 | + |
| 56 | +### `Either<L, R>` |
| 57 | + |
| 58 | +A container that can hold either a value (`Right`) or an error (`Left`). Supports: |
| 59 | + |
| 60 | +- `map`, `flatMap`, `fold` |
| 61 | +- `recover`, `recoverWith`, `ifLeft`, `ifRight` |
| 62 | +- `peek`, `toOptional` |
| 63 | + |
| 64 | +### `StreamExceptionUtils` |
| 65 | + |
| 66 | +Set of adapters for working with checked exceptions in `Stream API`: |
| 67 | + |
| 68 | +- `wrapFunction`, `wrapConsumer`, `wrapSupplier` etc. |
| 69 | +- `safeFunctionOptional`, `safeFunctionEither` |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## 💡 Usage Examples |
| 74 | + |
| 75 | +### 1. Value processing via `map` |
| 76 | + |
| 77 | +``` |
| 78 | +Either<String, Integer> result = Either.right(10) |
| 79 | + .map(i -> i * 2); |
| 80 | +
|
| 81 | +System.out.println(result); // Right(20) |
| 82 | +``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +### 2. Error recovery via `recover` |
| 87 | + |
| 88 | +``` |
| 89 | +Either<String, Integer> result = Either.left("Error") |
| 90 | + .recover(err -> 0); |
| 91 | +
|
| 92 | +System.out.println(result); // Right(0) |
| 93 | +``` |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +### 3. Universal transformation via `fold` |
| 98 | + |
| 99 | +``` |
| 100 | +String message = result.fold( |
| 101 | + err -> "Error: " + err, |
| 102 | + val -> "Success: " + val |
| 103 | +); |
| 104 | +
|
| 105 | +System.out.println(message); |
| 106 | +``` |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +### 4. Using `safeFunctionOptional` in Stream API |
| 111 | + |
| 112 | +``` |
| 113 | +List<Integer> result = List.of("1", "2", "bad", "3").stream() |
| 114 | + .map(StreamExceptionUtils.safeFunctionOptional(Integer::parseInt)) |
| 115 | + .flatMap(Optional::stream) |
| 116 | + .collect(Collectors.toList()); |
| 117 | +
|
| 118 | +// [1, 2, 3] |
| 119 | +``` |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +### 5. Using `safeFunctionEither` with error logging |
| 124 | + |
| 125 | +``` |
| 126 | +List<String> inputs = List.of("123", "fail", "456"); |
| 127 | +
|
| 128 | +inputs.stream() |
| 129 | + .map(StreamExceptionUtils.safeFunctionEither(s -> { |
| 130 | + if (s.equals("fail")) throw new RuntimeException("boom"); |
| 131 | + return Integer.parseInt(s); |
| 132 | + })) |
| 133 | + .forEach(e -> e |
| 134 | + .ifRight(val -> System.out.println("OK: " + val)) |
| 135 | + .ifLeft(err -> System.err.println("ERR: " + err.getMessage())) |
| 136 | + ); |
| 137 | +``` |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +### 6. `flatMap` support and nested logic |
| 142 | + |
| 143 | +``` |
| 144 | +Either<String, Integer> result = Either.right("5") |
| 145 | + .flatMap(s -> { |
| 146 | + try { |
| 147 | + return Either.right(Integer.parseInt(s)); |
| 148 | + } catch (NumberFormatException e) { |
| 149 | + return Either.left("Not a number"); |
| 150 | + } |
| 151 | + }); |
| 152 | +``` |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +### 7. Wrapping `ThrowingSupplier` into safe `Supplier` |
| 157 | + |
| 158 | +``` |
| 159 | +Supplier<String> supplier = StreamExceptionUtils.wrapSupplier(() -> { |
| 160 | + if (Math.random() > 0.5) throw new IOException("Unstable!"); |
| 161 | + return "OK"; |
| 162 | +}); |
| 163 | +
|
| 164 | +try { |
| 165 | + System.out.println(supplier.get()); |
| 166 | +} catch (RuntimeException e) { |
| 167 | + System.err.println("Failed: " + e.getMessage()); |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +## 🧪 Test Coverage |
| 174 | + |
| 175 | +- Tests written with `JUnit 5` |
| 176 | +- Covers `Either`, adapters, transformations, `Stream` pipelines |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +## 📘 JavaDoc |
| 181 | + |
| 182 | +👉 Available on [GitHub Pages](https://aDudko.github.io/safe-java-streams) if enabled |
| 183 | + |
| 184 | +--- |
| 185 | + |
| 186 | +## 🚀 Roadmap |
| 187 | + |
| 188 | +- Add `Try<T>` and `Result<T, E>` |
| 189 | +- Support for `mapLeft`, `biMap`, `filter` |
| 190 | +- Enhanced integration with `Optional`, `CompletableFuture`, `record` |
| 191 | +- Support for `Kotlin`, `Gradle`, `Functional style DSL` |
| 192 | + |
| 193 | +--- |
| 194 | + |
| 195 | +## ✍️ Author |
| 196 | + |
| 197 | +**Anatoly Dudko** |
| 198 | +[GitHub @aDudko](https://github.com/aDudko) • [LinkedIn](https://www.linkedin.com/in/dudko-anatol/) |
| 199 | + |
| 200 | +--- |
0 commit comments