java - I need to get a List of generic lazy loaders that will instantiate instances of their inferred type -
sorry cryptic title, difficult explain. general rule need lazy loader give me n instances of bound wildcard type. i'm calling lazy loader storage unit.
import java.util.arraylist; import java.util.list; public class storageunit<t extends myinterface> implements storable<t> { private int count; public storageunit(int count) { this.count = count; } private list<t> storables = new arraylist<t>(); public list<t> getinstances(class<t> c) { try { if (storables.size() == 0) { (int = 0; < count; i++) { storables.add(c.newinstance()); } } else { return storables; } }catch (illegalaccessexception illegalaccessexception) { illegalaccessexception.printstacktrace(); } catch (instantiationexception instantiationexception) { instantiationexception.printstacktrace(); } return storables; } } elsewhere in application have class has reference several of these storage units. need instances of storage unit type, , type.
import java.util.arraylist; import java.util.list; public class mystorageunitcontainer { private list<storageunit<? extends myinterface>> storageunits; public mystorageunitcontainer(list<storageunit<? extends myinterface>> storageunits) { this.storageunits = storageunits; } public list<storageunit<? extends myinterface>> getinstances() { list<storageunit<? extends myinterface>> instances = new arraylist<storageunit<? extends myinterface>>(); (storageunit<? extends myinterface> storageunit : storageunits) { storageunit.getinstances(/* can't bound wildcard... */); // loop through instances , them... } return instances; } } that code sucks, best analogy can think of actual storage unit container. storage unit container has several individual storage units (think boxes). each 1 of boxes contains items of type (think baseball cards). know box contains 100 baseball cards, until open box don't know details of each baseball card. i'm trying treat each box lazy loader. opening box loads n implementations if don't exist already.
paulo correct, , (also per paulo's answer), pass class object constructor around problem. allows getinstances() method appear - ie without parameters. internally, instance keeps reference generic class can call newinstance() on it.
this code illustrates using example. have tested executes ok.
import java.util.arraylist; import java.util.list; public class sandbox { static interface myinterface { } static interface storable<t> { list<t> getinstances(); }; static abstract class mystorableimpl implements myinterface { @override public string tostring() { return "i'm " + getclass() + " hashcode " + hashcode(); } } static class mystorable1 extends mystorableimpl { } static class mystorable2 extends mystorableimpl { } static class storageunit<t extends myinterface> implements storable<t> { private final int count; private final class<t> clazz; public storageunit(class<t> clazz, int count) { this.count = count; this.clazz = clazz; } private list<t> storables = new arraylist<t>(); public list<t> getinstances() { try { if (storables.size() == 0) { (int = 0; < count; i++) { storables.add(clazz.newinstance()); } } else { return storables; } } catch (illegalaccessexception illegalaccessexception) { illegalaccessexception.printstacktrace(); } catch (instantiationexception instantiationexception) { instantiationexception.printstacktrace(); } return storables; } } static class mystorageunitcontainer { private list<storageunit<? extends myinterface>> storageunits; public mystorageunitcontainer(list<storageunit<? extends myinterface>> storageunits) { this.storageunits = storageunits; } public list<myinterface> getallinstances() { list<myinterface> instances = new arraylist<myinterface>(); (storageunit<? extends myinterface> storageunit : storageunits) { list<? extends myinterface> list = storageunit.getinstances(); instances.addall(list); } return instances; } } public static void main(string[] args) { storageunit<? extends myinterface> box1 = new storageunit<mystorable1>(mystorable1.class, 2); storageunit<? extends myinterface> box2 = new storageunit<mystorable2>(mystorable2.class, 3); list<storageunit<? extends myinterface>> boxes = new arraylist<sandbox.storageunit<? extends myinterface>>(); boxes.add(box1); boxes.add(box2); mystorageunitcontainer container = new mystorageunitcontainer(boxes); list<myinterface> allinstances = container.getallinstances(); (myinterface myinterface : allinstances) { system.out.println(myinterface.tostring()); } } }
Comments
Post a Comment