JavaSingleValue
Java objects that represent singe value. Using normal variable to store data and manipulating them is no fun. Why not let objects maintain the data for you? Check out the examples below.
Caching and Lazy-loading
Let's say we have User object, and we can ask him to give us his description. He may need to put some effort to compute his description. We want him to do computation only when we ask his description (lazy-loading). Lastly, we want him to cache his description instead of computing every time we ask him. Most likely, this is how we may implement the User object in tedious way:
public class User {
private String content;
private boolean cached;
/**
* The description will be computed.
* We execute the logic here, not in the constructor (lazy-loading).
* We also cache the computed value.
* @return User description.
*/
public String description() {
if (!this.cached) {
// Imagine we do some computation.
// For example, fetching data from server, or getting data from database.
// After the computation, we assign the value to the instance variable.
this.content = "Some description of the user.";
this.cached = true;
}
return this.content;
}
}
And this is how we may implement the User object with CachedValue object, which is from this library :)
public class User {
/**
* We give the computation logic to this object.
* It will execute the logic only once and cache the value for us.
*/
private final CachedValue<String> description = new CachedValue<>(() -> {
// Imagine we do some computation.
// For example, fetching data from server, or getting data from database.
// After the computation, we assign the value to the instance variable.
return "Some description of the user.";
});
/**
* The description will be computed.
* We execute the logic here, not in the constructor (lazy-loading).
* We also cache the computed value.
* @return User description.
*/
public String description() {
return this.description.get();
}
}
How to use?
You just need to add the dependency like so:
Gradle:
dependencies {
implementation 'com.levelrin:javasinglevalue:1.0.0'
}
Maven:
<dependency>
<groupId>com.levelrin</groupId>
<artifactId>javasinglevalue</artifactId>
<version>1.0.0</version>
</dependency>
Requirements:
- JDK 1.8+
How to contribute?
As of now, just fork this repository and send us a pull request. The detailed guideline will be created soon.