Skip to content

talsma-ict/lazy4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maven Version JavaDoc Quality Gate Status Coverage Status

Lazy4J

  • A Lazy class in java.
  • A LazyValueMap.
  • A LazyList

What is it?

Lazy

Wrapper for a standard java Supplier function that only evaluates the wrapped function when it is first needed, remembering the result so it does not need to get called again.

LazyValueMap

Map that stores values as Lazy values.

LazyList

List that stores values as Lazy values.

Why?

We feel this ought to be provided out of the box and should have been when lambda's were introduced, back in Java 8.

Fortunately, a Lazy class implementation is not very difficult to create, so that's what we did.
The LazyValueMap was added later in version 2.0.2 and LazyList in 2.0.3.

Example

A small example of how the Lazy class can be used:

public class Example {
    // Method reference to constructor new Expensive(), called only when needed and keep the result.
    private final Lazy<Expensive> lazyMethod = Lazy.of(Expensive::new);

    // Lambda called only once when needed for the first time.
    private final Lazy<Expensive> lazyLambda = Lazy.of(() -> new Expensive());
}

This declares a lazy variable without calling the expensive supplier yet.
Only when get() is called for the first time, the new Expensive() constructor is called.
All subsequent invocations will return the same instance of Expensive.

Lazy provides the following methods:

  • isAvailable returning whether the lazy value is already available.
  • map applies a function on the lazy result.
  • flatMap applies a function that itself returns a supplier.
  • ifAvailable runs a function only if the lazy value is already available.

Please refer to the Lazy class documentation for full descriptions.

LazyValueMap example

    // Map that stores lazy values that only get evaluated when they are needed for the first time.
    private final LazyValueMap<String, String> lazyMap = new LazyValueMap(HashMap::new);
    private final LazyValueMap<String, String> sortedLazyMap = new LazyValueMap(TreeMap::new);

    lazyMap.putLazy("key", () -> new Expensive());

The expensive value is only evaluated when the key is first accessed.

The lazy map offers the following lazy variants of standard map methods:

  • getLazy
  • putLazy
  • lazyPutIfAbsent
  • lazyCompute
  • lazyComputeIfAbsent
  • lazyComputeIfPresent
  • lazyReplace
  • lazyMerge
  • lazyValues

LazyList example

    // List that stores lazy values that only get evaluated when they are needed for the first time.
    LazyList<Expensive> lazyValues = LazyList.create();

    lazyValues.addLazy(() -> new Expensive("first"));
    lazyValues.addLazy(() -> new Expensive("second"));

The expensive values are only evaluated when the values are first accessed.

The lazy list offers the following lazy variants of standard list methods:

  • getLazy / getFirstLazy / getLastLazy
  • setLazy
  • addLazy / addFirstLazy / addLastLazy / addAllLazy
  • streamLazy / streamAvailable / parallelStreamLazy / parallelStreamAvailable
  • replaceAll is performed lazily per value

Please be aware that certain operations such as sort or hashCode will trigger eager evaluation of all values in the list.

Getting the classes

Add the following dependency to your project or download it directly from github:

Maven

<dependency>
    <groupId>nl.talsmasoftware</groupId>
    <artifactId>lazy4j</artifactId>
    <version>[see maven badge]</version>
</dependency>

Gradle

compile 'nl.talsmasoftware:lazy4j:[see maven-central badge]'

Scala

libraryDependencies += "nl.talsmasoftware" % "lazy4j" % "[see maven-central badge]"

License

Apache 2.0 license

About

Lazy value support for java

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 6

Languages