Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to map top level collections in Dozer #5

Closed
ghost opened this issue Jun 22, 2012 · 1 comment
Closed

How to map top level collections in Dozer #5

ghost opened this issue Jun 22, 2012 · 1 comment

Comments

@ghost
Copy link

ghost commented Jun 22, 2012

ArrayList objects = new ArrayList();
...
DozerBeanMapper MAPPER = new DozerBeanMapper();
...
ArrayList newObjects = MAPPER.map(objects, ...);
like this
<S, D> Collection map(Collection srcCollection, Class targetClass);
not iterate over list

@Spikhalskiy
Copy link
Member

Dozer should not do it because it copy bean to bean, so it is not it's task.
But it is quite simple to make a wrap class. I use this one in our project for example:

public class EntityConverter {
    private Mapper mapper;

    @Inject
    public EntityConverter(Mapper mapper) {
        this.mapper = mapper;
    }

    public <F, T> T fill(F source, T destination) {
        if (source == null || destination == null) return null;
        mapper.map(source, destination);
        return destination;
    }

    public <F, T> List<T> convert(List<F> fromList, final Class<T> toClass) {
        return Lists.transform(fromList, new Function<F, T>() {
            @Override
            public T apply(F from) {
                return convert(from, toClass);
            }
        });
    }

    public <F, T> T convert(F from, final Class<T> toClass) {
        if (from == null) return null;
        return mapper.map(from, toClass);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant