Last active
March 22, 2022 09:05
-
-
Save bambuchaAdm/5965051 to your computer and use it in GitHub Desktop.
Spring data page converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package eu.devhe.testng.beans; | |
| public class BasicBean { | |
| private String name; | |
| private String code; | |
| public BasicBean() { | |
| super(); | |
| } | |
| public BasicBean(String name, String code) { | |
| super(); | |
| this.name = name; | |
| this.code = code; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public String getCode() { | |
| return code; | |
| } | |
| public void setCode(String code) { | |
| this.code = code; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package eu.devhe.testng.constroller; | |
| import org.springframework.data.jpa.repository.JpaRepository; | |
| import eu.devhe.testng.beans.BasicBean; | |
| public interface BasicRepository extends JpaRepository<BasicBean, Long> { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package eu.devhe.testng.constroller; | |
| import org.springframework.data.domain.Page; | |
| import org.springframework.data.domain.PageRequest; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import com.google.common.base.Function; | |
| import eu.devhe.testng.beans.BasicBean; | |
| import eu.devhe.testng.beans.PresenterBean; | |
| @RequestMapping("/") | |
| public class ExampleController { | |
| BasicRepository repository; | |
| public Page<PresenterBean> testPath(){ | |
| Page<BasicBean> result = repository.findAll(new PageRequest(10, 40)); | |
| return PageConverter.convert(result).using(new Function<BasicBean, PresenterBean>() { | |
| @Override | |
| public PresenterBean apply(BasicBean source) { | |
| return new PresenterBean(source); | |
| } | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package eu.devhe.testng.constroller; | |
| import java.util.ArrayList; | |
| import java.util.Iterator; | |
| import java.util.List; | |
| import org.springframework.data.domain.Page; | |
| import org.springframework.data.domain.Sort; | |
| import com.google.common.base.Function; | |
| public class PageConverter <T,V> implements Page<V> { | |
| static public class Builder<T> { | |
| private Page<T> source; | |
| private Builder(Page<T> source) { | |
| this.source = source; | |
| } | |
| public <V> Page<V> using(Function<T, V> converter){ | |
| return new PageConverter<>(source,converter); | |
| } | |
| } | |
| public static <T> Builder<T> convert(Page<T> page){ | |
| return new Builder<>(page); | |
| } | |
| private final Page<T> base; | |
| private final List<V> result; | |
| private PageConverter(Page<T> base,Function<T,V> converter) { | |
| this.base = base; | |
| this.result = new ArrayList<>(); | |
| for(T object : base) { | |
| result.add(converter.apply(object)); | |
| } | |
| } | |
| public List<V> getContent() { | |
| return result; | |
| } | |
| public int getNumber() { | |
| return base.getNumber(); | |
| } | |
| public int getNumberOfElements() { | |
| return base.getNumberOfElements(); | |
| } | |
| public int getSize() { | |
| return base.getSize(); | |
| } | |
| public Sort getSort() { | |
| return base.getSort(); | |
| } | |
| public long getTotalElements() { | |
| return base.getTotalElements(); | |
| } | |
| public int getTotalPages() { | |
| return base.getTotalPages(); | |
| } | |
| public boolean hasContent() { | |
| return base.hasContent(); | |
| } | |
| public boolean hasNextPage() { | |
| return base.hasNextPage(); | |
| } | |
| public boolean hasPreviousPage() { | |
| return base.hasPreviousPage(); | |
| } | |
| public boolean isFirstPage() { | |
| return base.isFirstPage(); | |
| } | |
| public boolean isLastPage() { | |
| return base.isLastPage(); | |
| } | |
| public Iterator<V> iterator() { | |
| return result.iterator(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package eu.devhe.testng.beans; | |
| public class PresenterBean { | |
| public PresenterBean(BasicBean source) { | |
| // TODO Auto-generated constructor stub | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment