Skip to content

Non-empty lists and streams

Compare
Choose a tag to compare
@runeflobakk runeflobakk released this 27 Jan 00:35
· 81 commits to main since this release
a649eb3

The JDK provides Collections and Stream for expressing a multitude of zero to arbitrary many elements, while many actual domains deals with one to many elements. While using the general case of e.g. a List to model this works all fine, it would be even better to have a generic data structure, with first-class support from the type system, to eliminate the illegal state of zero elements as early as possible, and in many cases also to offer some operations which are guarantied to be safe having one or more elements.

The 0.26 release adds two concepts: the NonEmptyList<T> and NonEmptyStream<T>. The stream implementation is independent of the list, but the NonEmptyList knows how to transform into a NonEmptyStream through its regular .stream() method, overridden to return the more specific NonEmptyStream type.

A Collector concept is also introduced for a more sensible transition from a non-empty stream back to a non-empty container, e.g. a non-empty list, by including an interface for the specific "result" type parameter of the Collector being an Optional. This allows to explicitly "flag" a collector of result-type Optional to produce its result based on if the source stream is empty or not, and the NonEmptyStream can detect on a type level when such collector is used, and bypass the redundant Optional and instead yield the result directly.

Please refer to the updated readme for more details and code examples.

Compatibility

This release breaks backwards binary compatibility because it changes the return types of a few static methods in DiggCollectors. Source compatibility is however not affected by these changes. In practice, this means that if you depend on this new v0.26 release while also depending on another library which depends on an earlier version of Digg, things may break at runtime. However, the admittedly limited prevalence of Digg makes this highly unlikely, and has been considered to warrant yet another minor pre-1.0 release.