I have many html forms referring to many persistence classes.
All the html forms are generated by one single class HTMLForm by passing in the respective HTMLFields instances:
public class HTMLForm<T>{
HTMLForm(HTMLFields[] f, Class<T> classt){
this.stupidSunWontAllowTnewInstance = classt;
// ... whatever GWT jazz ....
}
public T getPersistenceHandlerClass(){
try{
return (T) stupidSunWontAllowTnewInstance.newInstance();
}
catch (InstantiationException e){}
catch (IllegalAccessException e){}
}
Class<T> stupidSunWontAllowTnewInstance;
}
HTMLForm looks at HTMLFields[] array to generate the respective html forms. Therefore, each set of HTMLFields[] array require a different persistence class. The persistence classes are like
PostalAddr, PersonInfo, ItemDescr, Preferences, etc, etc.
Now since I cannot do T.newInstance(), stupidSunWontAllowTnewInstance would be assigned (a silly necessity due to sun's architecture of generics) by the HTMLForm constructor and then getPersistenceHandlerClass is used later to get the appropriate persistence handling class.
Since cloud computing charges by the cpu hour, my question is, which would use less cpu, presuming I have about 25 persistence classes to wade thro. The first one above or the one following?
public T getPersistenceHandlerClass(){
if (stupidSunWontAllowTnewInstance == PostalAddress.class)
return new PostalAddress();
if (stupidSunWontAllowTnewInstance == PersonInfo.class)
return new PersonInfo();
if (stupidSunWontAllowTnewInstance == ....
....
....
if (stupidSunWontAllowTnewInstance == etc.class)
return new etc();
}
or a Map of factories
public static Map<PersistenceHandlerFactoryInterface> PHFactories;
public T getPersistenceHandlerClass(){
return
PHFactories.get(stupidSunWontAllowTnewInstance).createInstance();
}
or make a better suggestion (with cpu consumption consideration) on how I should overcome java generics shortcoming, for another way to instantiate from parameter T. (Or critique me why I'm being too heady criticising sun's generic parametric shortcoming).