I have a List of Lists which I'm trying to map to an 2 dimensional array [][] using Dozer and Custom Converter.
public class Field {
List<String> items;
public void add(String s) {
items.add(s);
}
}
public class ClassA {
int anotherVariable;
List<Field> fields;
public void add(Field f) {
fields.add(f);
}
}
public class ClassB {
int anotherVariable;
String[][] itemValues;
}
@Test
public void convertListTo2DArray() {
Field field1 = new Field();
field1.add("m"); field1.add("n");
Field field2 = new Field();
field2.add("o"); field2.add("p");
ClassA classA = new ClassA();
classA.add(field1);
classA.add(field2);
classA.setAnotherVariable(99);
List<Converter> converters = new ArrayList<Converter>();
converters.add(new ListToArrayConverter());
ClassB classB = new DozerBeanMapper().setCustomConverters(converters).map(classA, ClassB.class);
/**
* Result:
* classB -> anotherVariable = 99
*
* classB -> itemValues[][] =
* ["m", "n"]
* ["o", "p"]
*/
}
The Converter should only be used for converting between List<List>
and String[][]
and not for other variables.
I took a look at the answer in to the following question, but how should I handle Array instead of Set/List in that Custom Converter? Dozer Mapping from HashSet to Arraylist
Any suggestions would be much appreciated. Thanks