Skip to content

Instantly share code, notes, and snippets.

@Ramasubramanian
Created February 17, 2014 15:32
Show Gist options
  • Save Ramasubramanian/9052697 to your computer and use it in GitHub Desktop.
Save Ramasubramanian/9052697 to your computer and use it in GitHub Desktop.
Second usage example for Lazy type.
package in.raam.lazy;
import java.util.ArrayList;
import java.util.List;
class Parent {
private String key;
private Lazy<List<Child>> children = Lazy.create(() -> new ParentHelper().loadChildren(this));
public Lazy<List<AnotherChild>> childList = Lazy.create(this::loadChildList);
public Parent(String key) {
this.key = key;
}
public String getKey() {
return key;
}
public List<Child> getChildren() {
System.out.println("children.created :: " + children.created());
return children.value();
}
private List<AnotherChild> loadChildList() {
System.out.println("Loading all another child list!!!");
// fetch 1000s of records from DB OR fetch from a Webservice etc.
return new ArrayList<>();
}
}
class Child {
// numerous attributes
}
class AnotherChild {
// numerous attributes
}
class ParentHelper {
public List<Child> loadChildren(Parent parent) {
System.out.println("Loading all children!!!");
// fetch 1000s of records from DB OR fetch from a Webservice etc.
return new ArrayList<>();
}
}
public class LazyUsage2 {
public static void main(String[] args) {
Parent p = new Parent("KEY");
System.out.println(p.getKey());
System.out.println(p.getChildren().size());
System.out.println(p.getChildren().isEmpty());
System.out.println(p.getChildren());
System.out.println("p.childList.created() :: " + p.childList.created());
System.out.println(p.childList.value().size());
System.out.println("p.childList.created() :: " + p.childList.created());
System.out.println(p.childList.value().isEmpty());
System.out.println(p.childList.value());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment