
com.github.leeonky.map.ViewListConverter Maven / Gradle / Ivy
package com.github.leeonky.map;
import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.metadata.Type;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
class ViewListConverter extends ViewConverter {
protected final String propertyName;
ViewListConverter(Mapper mapper, Class> view, String propertyName) {
super(mapper, view);
this.propertyName = propertyName;
}
@SuppressWarnings("unchecked")
@Override
public Object convert(Object source, Type destinationType, MappingContext mappingContext) {
Class> rawType = destinationType.getRawType();
Iterable collection = source instanceof Map ? wrapperEntry((Map) source) : (Iterable) source;
if (Iterable.class.isAssignableFrom(rawType))
return mapCollection(collection, createCollection(rawType), mappingContext);
else if (rawType.isArray())
return mapCollection(collection, new ArrayList<>(), mappingContext).toArray((Object[]) Array.newInstance(rawType.getComponentType(), 0));
throw new IllegalStateException("Only support map " + propertyName + " to list or array, but target type is " + rawType.getName());
}
private Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy