lazylet

Extending the functionality of Optional

Лицензия

Лицензия

Группа

Группа

com.javax0
Идентификатор

Идентификатор

lazylet
Последняя версия

Последняя версия

1.0.1
Дата

Дата

Тип

Тип

jar
Описание

Описание

lazylet
Extending the functionality of Optional
Ссылка на сайт

Ссылка на сайт

https://github.com/verhas/lazylet/tree/master
Система контроля версий

Система контроля версий

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

Скачать lazylet

Как подключить последнюю версию

<!-- 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"]

Зависимости

test (3)

Идентификатор библиотеки Тип Версия
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

Модули Проекта

Данный проект не имеет модулей.

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.

Версии библиотеки

Версия
1.0.1
1.0.0