Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

zleonov/maybe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

20 Commits

Repository files navigation

Maybe

A null aware monadic maybe type in idiomatic Java.

Overview

Managing code that handles optional values is often complex and cumbersome.

Prior to Java 8, optional values were usually represented using null values. For example Map.get(K key) returns a null if there is no mapping for the requested key. But null values can also be valid return types. For example from maps which support null values.

As we all know writing code that handles (or forgets to handle) null cases is an incredibly common source of errors.

To address the issue Java 8 introduced the Optional class which can hold either a single non-null value or nothing at all. So what's the problem?

Using Optional effectively removes null from a language where null values can explicitly represent valid parameters and return types.

Maybe is a null aware monad. Meaning unlike Optional it does not use null to signify the absence of a value.

Do yourself a favor and start writing code like this:

 final Maybe<String> maybe = ...
 
 ...
 
 final String value = maybe.ifNull(() -> logger.fine("value is null"))
 .ifNotNull(t -> logger.fine("value is %s", t))
 .ifAbsent(() -> logger.fine("value is absent: using default value"))
 .orElse(DEFAULT_VALUE);

Documentation

The latest API documentation can be accessed here.

AltStyle によって変換されたページ (->オリジナル) /