Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/noties/nullsafe

Null safe flow for Java & Android
https://github.com/noties/nullsafe

android java null

Last synced: 2 months ago
JSON representation

Null safe flow for Java & Android

Awesome Lists containing this project

README

        

# NullSafe

[![Maven Central](https://img.shields.io/maven-central/v/ru.noties/nullsafe.svg)](http://search.maven.org/#search|ga|1|g%3A%22ru.noties%22%20AND%20a%3A%22nullsafe%22)

Null safe flow for Java.

Java8:

```java
// `country` can be null, meaning that no mapping will occur and "Unknown" will be returned
final String name = NullSafe.create(country)
.map(Country::city) // map function will never call Supplier with null argument
.map(City::street)
.map(Street::name)
.get("Unknown");
```

```java

final City capitalCity = capitalCity();

final String name = NullSafe.create(country)
.map(capitalCity, Country::city) // allows to specify def values (cannot be null)
.map(City::street)
.map(Street::name)
.get("Unknown");
```

```java

// NullSafe instance can be used to split the flow (safe to reference intermediate state)

final NullSafe nullSafe = NullSafe.create(country)
.map(capitalCity, Country::city);

final String name = nullSafe
.map(City::street)
.map(Street::name)
.get(); // <- returns nullable if no def value was specified

final House house = nullSafe
.map(City::street)
.map(Street::house)
.get();
```

This utility class is **not** thread-safe