Orthodox OOP constructions of extensive use that we need badly in all our projects.
Currently contains java.util.function
extensions CachingFunction
and CachingSupplier
who look like Function
and Supplier
correspondingly, but encapsulate caching behaviour.
The library is published on jcenter
. To apply to, say, gradle build, use
repositories {
jcenter()
}
dependencies {
implementation group: 'ru.arsysop.lang', name: 'lang', version: '0.1'
}
or have a hint for your build automation system.
Here is the sample of CachingFunction usage. It
- does not perform any calculations in ctor
- does not contain not-final fields
- calculates lazily
- and do it only ones for the first need
public final class UserFromEmail {
private final CachingFunction<String, String> name;
/**
* Get the first part of an email address (before '@'-symbol) as a user name.
*
* @param email valid email address
*/
public UserFromEmail(String email) {
name = new CachingFunction<>(email, input -> input.substring(0, input.indexOf('@')));
}
public String name() {
return name.get();
}
}