Skip to content

Instantly share code, notes, and snippets.

@Ramasubramanian
Created February 17, 2014 15:20
Show Gist options
  • Save Ramasubramanian/9052522 to your computer and use it in GitHub Desktop.
Save Ramasubramanian/9052522 to your computer and use it in GitHub Desktop.
Usage example of Lazy.java written earlier
package in.raam.lazy;
class IntensiveResource {
public IntensiveResource() {
// e.g.
// obtain a JNDI object may be a datasource or remote objects
// load values from file
// create datasource manually
// load a huge set of children objects for a parent collection
System.out.println("Wait! performing CPU intensive operations...");
}
public static IntensiveResource buildResource() {
System.out.println("Calling from method ref!");
return new IntensiveResource();
}
public String doSomeOperation() {
return "Some operation!";
}
}
public class LazyUsage {
public Lazy<IntensiveResource> r = Lazy.create(IntensiveResource.class, false);
public Lazy<IntensiveResource> r1 = Lazy.create(IntensiveResource::buildResource);
public Lazy<IntensiveResource> r2 = Lazy.create(() -> {
System.out.println("Calling from lambda!");
return new IntensiveResource();
});
public static void main(String[] args) {
LazyUsage l = new LazyUsage();
System.out.println("l.r.created :: " + l.r.created());
System.out.println(l.r.value().doSomeOperation());
System.out.println("l.r.created :: " + l.r.created());
System.out.println(l.r.value() == null);
System.out.println("l.r1.created :: " + l.r1.created());
System.out.println(l.r1.value() == null);
System.out.println("l.r1.created :: " + l.r1.created());
System.out.println(l.r2.value().doSomeOperation());
System.out.println(l.r2.value());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment