All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.beanmapper.spring.PageableMapper Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
/*
 * (C) 2014 42 bv (www.42.nl). All rights reserved.
 */
package io.beanmapper.spring;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.beanmapper.BeanMapper;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

/**
 * Mapping utilities for pages.
 *
 * @author Jeroen van Schagen
 * @since Nov 13, 2015
 */
public class PageableMapper {
    
    /**
     * Converts a page into the desired target type.
     * 
     * @param source the source page
     * @param targetClass the target type
     * @param beanMapper the bean mapper used to perform mappings
     * @return the same page, but with result type
     */
    public static  Page map(Page source, Class targetClass, BeanMapper beanMapper) {
        List transformed;
        if (source.hasContent()) {
            List content = new ArrayList(source.getContent());
            transformed = (List) beanMapper.map(content, targetClass);
        } else {
            transformed = Collections.emptyList();
        }
        Pageable pageable = new PageRequest(source.getNumber(), source.getSize(), source.getSort());
        return new PageImpl(transformed, pageable, source.getTotalElements());
    }

}