An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

[ **en** | [ru](README.ru.md) ]

# Sugar

[![CI](https://github.com/nanolaba/sugar/actions/workflows/ci.yml/badge.svg)](https://github.com/nanolaba/sugar/actions/workflows/ci.yml)
[![Maven Central](https://img.shields.io/maven-central/v/com.nanolaba/sugar?label=Maven%20Central)](https://central.sonatype.com/artifact/com.nanolaba/sugar)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
![Java](https://img.shields.io/badge/Java-8%2B-blue)

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*