https://github.com/nanolaba/sugar
A tiny, zero-dependency Java library of static syntactic-sugar helpers — one class, one purpose: cut the boilerplate out of everyday Java
https://github.com/nanolaba/sugar
checked-exceptions java java8 lambdas library maven-central memoization syntactic-sugar utility
Last synced: 10 days ago
JSON representation
A tiny, zero-dependency Java library of static syntactic-sugar helpers — one class, one purpose: cut the boilerplate out of everyday Java
- Host: GitHub
- URL: https://github.com/nanolaba/sugar
- Owner: nanolaba
- License: apache-2.0
- Created: 2024-04-01T22:55:43.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2026-04-23T06:54:07.000Z (2 months ago)
- Last Synced: 2026-06-14T07:35:03.666Z (10 days ago)
- Topics: checked-exceptions, java, java8, lambdas, library, maven-central, memoization, syntactic-sugar, utility
- Language: Java
- Homepage: https://github.com/nanolaba/sugar
- Size: 37.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[ **en** | [ru](README.ru.md) ]
# Sugar
[](https://github.com/nanolaba/sugar/actions/workflows/ci.yml)
[](https://central.sonatype.com/artifact/com.nanolaba/sugar)
[](LICENSE)

A tiny, zero-dependency Java library of static syntactic-sugar helpers — one class, one purpose: cut the boilerplate out of everyday Java.
The whole library is a single class, `com.nanolaba.sugar.Code`, with helpers for the most common friction points: running lambdas that throw checked exceptions, multi-value equality checks, and lazy memoization.
## Table of contents
1. [Requirements](#requirements)
2. [Installation](#installation)
1. [Snapshot versions](#snapshot-versions)
3. [Usage](#usage)
1. [`run` — checked exceptions out of the way](#run-checked-exceptions-out-of-the-way)
2. [`runQuietly` — swallow, or fall back to a default](#runquietly-swallow-or-fall-back-to-a-default)
3. [`equalsAny` — null-safe multi-value equality](#equalsany-null-safe-multi-value-equality)
4. [`memoize` — lazy, thread-safe, compute-at-most-once](#memoize-lazy-thread-safe-compute-at-most-once)
4. [Building from source](#building-from-source)
5. [Links](#links)
6. [License](#license)
## Requirements
- Java 8+
- Maven 3.x (to build from source)
## Installation
Released artifacts are published to **Maven Central** — no extra repository configuration is required.
**Maven**
```xml
com.nanolaba
sugar
1.0
```
**Gradle**
```groovy
dependencies {
implementation 'com.nanolaba:sugar:1.0'
}
```
### Snapshot versions
To use a development snapshot, add the Sonatype Central snapshot repository:
```xml
central.sonatype.com-snapshot
https://central.sonatype.com/repository/maven-snapshots
true
always
```
## Usage
All helpers are `static` methods on `Code`. A static import keeps call sites tidy:
```java
import static com.nanolaba.sugar.Code.*;
```
### `run` — checked exceptions out of the way
Wraps a lambda that may throw a checked `Exception`. Any exception is rethrown as a `RuntimeException`; unchecked exceptions and `Error`s pass through unchanged.
```java
// With a return value
byte[] content = run(() -> Files.readAllBytes(path));
// Side effect only
run(() -> Thread.sleep(100));
```
### `runQuietly` — swallow, or fall back to a default
Swallows any exception from the lambda. The overload with a default `Supplier` returns that default when an exception occurs — the supplier is only evaluated on failure.
```java
// Fall back to a default
int port = runQuietly(() -> Integer.parseInt(System.getenv("PORT")), () -> 8080);
// Fire-and-forget
runQuietly(() -> socket.close());
```
### `equalsAny` — null-safe multi-value equality
Returns `true` if the first argument equals any of the following ones, using `Objects.equals` (so `null` is compared safely).
```java
if (equalsAny(status, "OK","READY","IDLE")) {
// ...
}
```
### `memoize` — lazy, thread-safe, compute-at-most-once
Wraps a `Supplier` so its value is computed at most once. The first `get()` is `synchronized`; afterwards the delegate replaces itself and every subsequent call is a plain field read with no locking.
```java
Supplier config = memoize(() -> loadConfigFromDisk());
config.get(); // reads from disk
config.get(); // cached, no lock
```
## Building from source
```bash
mvn clean install
```
The build runs PIT mutation testing with a 100% threshold — any surviving mutant fails the build. When adding code, expect to cover every branch and boundary with assertions.
## Links
- Source code: [https://github.com/nanolaba/sugar](https://github.com/nanolaba/sugar)
- Issue tracker: [https://github.com/nanolaba/sugar/issues](https://github.com/nanolaba/sugar/issues)
- Maven Central: [central.sonatype.com/artifact/com.nanolaba/sugar](https://central.sonatype.com/artifact/com.nanolaba/sugar)
## License
Released under the [Apache License 2.0](LICENSE).
---
*Generated with [nanolaba/readme-generator](https://github.com/nanolaba/readme-generator) — 23.04.2026*