[ en | ru ]
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.
- Java 8+
- Maven 3.x (to build from source)
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'
}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>All helpers are static methods on Code. A static import keeps call sites tidy:
import static com.nanolaba.sugar.Code.*;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));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());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")) {
// ...
}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 lockmvn clean installThe 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.
- Source code: https://github.com/nanolaba/sugar
- Issue tracker: https://github.com/nanolaba/sugar/issues
- Maven Central: central.sonatype.com/artifact/com.nanolaba/sugar
Released under the Apache License 2.0.
Generated with nanolaba/readme-generator — 23.04.2026