JavaSingleValue

Java objects that represent singe value.

License

License

GroupId

GroupId

com.levelrin
ArtifactId

ArtifactId

javasinglevalue
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

JavaSingleValue
Java objects that represent singe value.
Project URL

Project URL

https://github.com/levelrin/JavaSingleValue
Source Code Management

Source Code Management

https://github.com/levelrin/JavaSingleValue

Download javasinglevalue

How to add to project

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

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Build Status Test Coverage Maven Central License

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:

  1. 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.

Versions

Version
1.0.0