简体   繁体   中英

Dozer, Java: How to convert from List<List> to 2D Array?

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

My java is a little rusty, bear with me.

If you want the converter to be used only for converting List to String array and not anything else, one of the ways you could restrict it is by specifying the custom converter for only those two fields in the xml:

<mapping>
<class-a>beans6.ClassA</class-a>
<class-b>beans6.ClassB</class-b>
<field custom-converter="converter.ConvertListToArray">
    <a>fields</a>
    <b>itemValues</b>
</field>
</mapping>

Next, the items attribute in your Field class and the fields attribute in your ClassA class needed to be initialized to arraylists to prevent them from throwing NullPointerException .

List<String> items = new ArrayList<String>();
List<Field> fields = new ArrayList<Field>();

Finally, here is the CustomConverter, assuming that the number of elements added to fields is always constant:

public class ConvertListToArray implements CustomConverter{
    public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, 
    Class<?> destinationClass, Class<?> sourceClass) {
        if(sourceFieldValue==null)
            return null;

        if(sourceFieldValue instanceof List && ((List<?>) sourceFieldValue).size()>0){
            List<Field> listOfFields = (List<Field>)sourceFieldValue;

            String[][] destinationValue = new String[2][2];
            for (int i = 0; i<2;i++){
                 Field f = listOfFields.get(i);
                 for (int j = 0;j<f.getItems().size();j++){
                     destinationValue[i][j] = f.getItems().get(j);
                 }
             }
             return destinationValue;

         }
        return null;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM