Lazy


License

License

GroupId

GroupId

ru.noties
ArtifactId

ArtifactId

lazy
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Lazy
Lazy
Project URL

Project URL

https://github.com/noties/Lazy
Source Code Management

Source Code Management

https://github.com/noties/Lazy

Download lazy

How to add to project

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

Dependencies

compile (1)

Group / Artifact Type Version
com.android.support » support-annotations jar 27.1.0

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Lazy

Utility to postpone initialisation of a value

listeners

implementation 'ru.noties:lazy:1.1.0'

final Lazy<String> lazy = Lazy.of(() -> "I'm so lazy!");
final Lazy<Calendar> lazy = Lazy.of(Calendar::getInstance);
final Calendar calendar = lazy.get();
final Lazy<Cursor> lazy = Lazy.ofSynchronized(this::query);
final Lazy<Integer> lazy = Lazy.of(() -> 42);
final Lazy<Integer> sync = Lazy.ofSynchronized(lazy);

Hide

Starting with 1.1.0 Lazy provides a way to hide wrapped type:

final CharSequence cs = Lazy.ofHidden(CharSequence.class, () -> "Yeah, it's me");

// or

final CharSequence cs = Lazy.of(() -> "We are the lazy!").hide(CharSequence.class);

NB This works ONLY for interface types as internally java.lang.reflect.Proxy is used.


With some lifecycle notification (using lifebus):

public class MainActivity extends Activity {
    
    private Lifebus<ActivityEvent> lifebus;
    
    private CharSequence hiddenLazy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        lifebus = ActivityLifebus.create(this);
        
        hiddenLazy = Lazy.of(() -> "Really resource consuming thing here")
                .accept(lazy -> lifebus.on(ActivityEvent.DESTROY, () -> {
                    if (lazy.hasValue()) {
                        // release it here
                        lazy.get();
                    }
                }))
                .hide(CharSequence.class);
    }
}
  Copyright 2018 Dimitry Ivanov ([email protected])

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

Versions

Version
1.1.0
1.0.0