Skip to content

nanolaba/sugar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

[ en | ru ]

Sugar

CI Maven Central License Java

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
  2. Installation
    1. Snapshot versions
  3. Usage
    1. run — checked exceptions out of the way
    2. runQuietly — swallow, or fall back to a default
    3. equalsAny — null-safe multi-value equality
    4. memoize — lazy, thread-safe, compute-at-most-once
  4. Building from source
  5. Links
  6. 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

<dependency>
    <groupId>com.nanolaba</groupId>
    <artifactId>sugar</artifactId>
    <version>1.0</version>
</dependency>

Gradle

dependencies {
    implementation 'com.nanolaba:sugar:1.0'
}

Snapshot versions

To use a development snapshot, add the Sonatype Central snapshot repository:

<repositories>
    <repository>
        <id>central.sonatype.com-snapshot</id>
        <url>https://central.sonatype.com/repository/maven-snapshots</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>
    </repository>
</repositories>

Usage

All helpers are static methods on Code. A static import keeps call sites tidy:

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 Errors pass through unchanged.

// 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.

// 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).

if (equalsAny(status, "OK","READY","IDLE")) {
    // ...
}

memoize — lazy, thread-safe, compute-at-most-once

Wraps a Supplier<T> 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.

Supplier<Config> config = memoize(() -> loadConfigFromDisk());
config.get(); // reads from disk
config.get(); // cached, no lock

Building from source

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

License

Released under the Apache License 2.0.


Generated with nanolaba/readme-generator — 23.04.2026

About

A tiny, zero-dependency Java library of static syntactic-sugar helpers — one class, one purpose: cut the boilerplate out of everyday Java

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages