lazylet

Extending the functionality of Optional

License

License

GroupId

GroupId

com.javax0
ArtifactId

ArtifactId

lazylet
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

lazylet
Extending the functionality of Optional
Project URL

Project URL

https://github.com/verhas/lazylet/tree/master
Source Code Management

Source Code Management

https://github.com/verhas/lazylet/tree/master

Download lazylet

How to add to project

<!-- https://jarcasting.com/artifacts/com.javax0/lazylet/ -->
<dependency>
    <groupId>com.javax0</groupId>
    <artifactId>lazylet</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.javax0/lazylet/
implementation 'com.javax0:lazylet:1.0.1'
// https://jarcasting.com/artifacts/com.javax0/lazylet/
implementation ("com.javax0:lazylet:1.0.1")
'com.javax0:lazylet:jar:1.0.1'
<dependency org="com.javax0" name="lazylet" rev="1.0.1">
  <artifact name="lazylet" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.javax0', module='lazylet', version='1.0.1')
)
libraryDependencies += "com.javax0" % "lazylet" % "1.0.1"
[com.javax0/lazylet "1.0.1"]

Dependencies

test (3)

Group / Artifact Type Version
org.mockito : mockito-all jar 1.10.19
org.junit.jupiter : junit-jupiter-api jar 5.3.1
org.junit.jupiter : junit-jupiter-engine jar 5.3.1

Project Modules

There are no modules declared in this project.

Lazy

If you are programming in Scala you can write

lazy val z = "Hello"

and the expression will only be evaluated when z is accessed the first time.

If you progam in Kotlin you can write something like

val z: String by lazy { "Hello" }

and the expression will only be evaluated when z is accessed the first time.

What can you write in Java?

Using

    <groupId>com.javax0</groupId>
    <artifactId>lazylet</artifactId>
    <version>1.0.0</version>

you can write

var z = Lazy.let(()->"Hello");

and then you can write z.get() to access to the value. The first time get() is invoked the supplier will be evaluated and any later time the already calculated value will be returned.

This is not part of the Java language but it is an extremely simple class. The use is simple as demonstrated in the unit tests:

    private static class TestSupport {
        int count = 0;

        boolean callMe() {
            count++;
            return true;
        }
    }

    ...

final var ts = new TestSupport();
var z = Lazy.let(ts::callMe);
if (false && z.get()) {
    Assertions.fail();
}
Assertions.assertEquals(0, ts.count);
z.get();
Assertions.assertEquals(1, ts.count);
z.get();
Assertions.assertEquals(1, ts.count);

What is a bit more, even if it may not be useful that you can say

final var ts = new TestSupport();
var z = Lazy.sync(ts::callMe);
if (false && z.get()) {
    Assertions.fail();
}
Assertions.assertEquals(0, ts.count);
z.get();
Assertions.assertEquals(1, ts.count);
z.get();
Assertions.assertEquals(1, ts.count);

to get a Lazy supplier that can be used by multiple threads and it is still guaranted that the supplier passed as argument is passed only once.

Versions

Version
1.0.1
1.0.0