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

pl.chilldev.commons.data.ConvertUtils Maven / Gradle / Ivy

The newest version!
/*
 * This file is part of the ChillDev-Commons.
 *
 * @license http://mit-license.org/ The MIT license
 * @copyright 2017 © by Rafał Wrzeszcz - Wrzasq.pl.
 */

package pl.chilldev.commons.data;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.hateoas.PagedResources;

/**
 * Conversion utils for Spring Data collections.
 */
public class ConvertUtils
{
    /**
     * Extracts URL string values from sorting model.
     *
     * @param sort Spring Data sort model.
     * @return URL values.
     */
    public static Collection extractSort(Sort sort)
    {
        if (sort == null) {
            return null;
        }

        Collection values = new ArrayList<>();

        for (Sort.Order criteria : sort) {
            values.add(criteria.getProperty() + "," + criteria.getDirection().name());
        }

        return values;
    }

    /**
     * Converts HATEOAS resources into Spring Data page.
     *
     * @param resources Paged resources.
     * @param request Pagination specification.
     * @param  Collection element type.
     * @return Paged result.
     */
    public static  Page buildPageFromResources(
        PagedResources resources,
        Pageable request
    )
    {
        return new PageImpl<>(
            new ArrayList<>(resources.getContent()),
            request,
            resources.getMetadata().getTotalElements()
        );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy