Search code examples
javadozer

Dozer Map DTO Mapping


I have a little "Object":

Map<Integer, Map<WeekDay, Map<String, Data>>> obj

and I want to Map it to:

Map<Integer, Map<WeekDay, Map<String, DataDto>>> returnObj

how can I achive this?

The way I wanted to use was this one:

map(schedule, Map<Integer.class, Map<WeekDay.class, Map<String.class, DataDto.class>>>);

but at the "Map" I am stuck, becuase I can't add a .class behind them and in this state it doesn't work...


Solution

  • I would suggest to simplify your Map if possible:

    class A {
        WeekDay weekDay;
        String str;
        Data obj;
    }
    
    Map<Integer, A> map = ...;
    Iterables.transform(map.values(), new Function<Data, DataDto>() {
                @Override
                public Object apply(String input) {
                    return ...;
                }
            });
    

    or you can put it inside your class:

    class Dictionary {
        Map<Integer, Map<WeekDay, Map<String, Data>>> obj;
    
        getDataDto(Integer key, Weekday weekDay, String str) {
            final Data data = obj.get(key).get(weekDay).get(str);
            return (new Function<Data, DataDto>() {
                ...
            }).apply(data);
        }
    }
    

    Think about operations you are going to use over your data structure and come up with the proper class. Your nested map doesn't look okay.