Generic inside a generic

first of all i want to show you what I want to achieve.I want to Generate a XML file out of classes that will look like the following:\[code\]<config> <criteria name="criteriaName" defaultValue="http://stackoverflow.com/questions/13706993/0"> <item value="http://stackoverflow.com/questions/13706993/1"> <criteria name="criteriaName2" defaultValue=""> <item value="http://stackoverflow.com/questions/13706993/foo"> </item> <item value="http://stackoverflow.com/questions/13706993/bar"> </item> </criteria> </item> <item value="http://stackoverflow.com/questions/13706993/2"> </item> </criteria> <criteria name="criteriaName3" defaultValue=""> <item value="http://stackoverflow.com/questions/13706993/foo"> </item> </criteria></config>\[/code\]Summary:
  • Criteria has a name and a default value (wich can be String, int ordouble) and x items as childs.
  • An Item has a value, which must be of the same type of the parents criteria defaultValue.
  • An Item can also hold another criteria. The type of the defaultValue in the criteria is independent from the parents (item) type
e.g.:\[code\]criteria (type int) item (type int) criteria(type String) item(type String)\[/code\]I also need this model in form of classes because I want to do some validation with it later on.Now I struggle at a specific point, let me show you my classes first:Config:\[code\]@XmlAccessorType(value=http://stackoverflow.com/questions/13706993/XmlAccessType.FIELD)@XmlRootElement(name="config")public class Config { @XmlElement(name = "criteria") private List<Criteria> criteriaList = new ArrayList<Criteria>(); /*getters and setters*/}\[/code\]Criteria:\[code\]@XmlAccessorType(value=http://stackoverflow.com/questions/13706993/XmlAccessType.FIELD)@XmlRootElement(name="criteria")public class Criteria<T> { @XmlAttribute private T key; @XmlAttribute private String display; @XmlAttribute private T defaultValue; private List<Item<T>> items = new ArrayList<Item<T>>(); public T getKey() { return key; } /*getters and setters*/}\[/code\]Item:\[code\]@XmlAccessorType(value=http://stackoverflow.com/questions/13706993/XmlAccessType.FIELD)@XmlRootElement(name="item")public class Item<T>{ @XmlAttribute protected T key; @XmlAttribute protected String display; @XmlElement protected Criteria<T> criteria = new ArrayList<Criteria<T>>(); /*getters and setters*/}\[/code\]With that I can make a Criteria of a specific type, which has items of the same type.Now I want to Insert a Criteria under the Item, that has a different type than the Item, but that is not Possible :\[code\] Config config = new Config(); Criteria<Integer> criteria = new Criteria<Integer>(); Item<Integer> item = new Item<Integer>(); Criteria<String> subCriteria = new Criteria<String>(); //this obviously doesn't work item.getCriteria().add(subCriteria); //<-- criteria.getItems().add(item); config.addCriteria(criteria);\[/code\]I also tried something like this declaration(in Item):\[code\]@XmlElementprotected <U> List<Criteria<U>> criteria;\[/code\]But that is only working for methods, not properties.Can anyone help?Thank you in advance!
 
Back
Top