Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/noties/lazy
Utility to postpone initialisation of a value
https://github.com/noties/lazy
android lazy
Last synced: 2 months ago
JSON representation
Utility to postpone initialisation of a value
- Host: GitHub
- URL: https://github.com/noties/lazy
- Owner: noties
- License: apache-2.0
- Created: 2017-01-10T12:29:06.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-31T08:41:08.000Z (almost 7 years ago)
- Last Synced: 2023-07-07T14:17:25.895Z (over 1 year ago)
- Topics: android, lazy
- Language: Java
- Homepage:
- Size: 97.7 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Lazy
Utility to postpone initialisation of a value
[![listeners](https://img.shields.io/maven-central/v/ru.noties/lazy.svg?label=lazy)](http://search.maven.org/#search|ga|1|g%3A%22ru.noties%22%20AND%20a%3A%22lazy%22)
```gradle
implementation 'ru.noties:lazy:1.1.0'
```---
```java
final Lazy lazy = Lazy.of(() -> "I'm so lazy!");
``````java
final Lazy lazy = Lazy.of(Calendar::getInstance);
final Calendar calendar = lazy.get();
``````java
final Lazy lazy = Lazy.ofSynchronized(this::query);
``````java
final Lazy lazy = Lazy.of(() -> 42);
final Lazy sync = Lazy.ofSynchronized(lazy);
```## Hide
Starting with `1.1.0` Lazy provides a way to _hide_ wrapped type:
```java
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](https://github.com/noties/Lifebus)):
```java
public class MainActivity extends Activity {
private Lifebus 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 athttp://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.
```