Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
Librador
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
EIP
Labrador
Librador
Merge requests
!38
Add a Pageable object that can be transferred
Code
Review changes
Check out branch
Open in Workspace
Download
Patches
Plain diff
Expand sidebar
Merged
Add a Pageable object that can be transferred
q364-queue-2-0-pagination
into
development
Overview
0
Commits
1
Pipelines
5
Changes
1
Merged
Add a Pageable object that can be transferred
Chris Lemaire
requested to merge
q364-queue-2-0-pagination
into
development
Jan 8, 2021
Overview
0
Commits
1
Pipelines
5
Changes
1
0
0
Merge request reports
Compare
development
version 1
5f733358
Jan 8, 2021
development (base)
and
latest version
latest version
08bff152
1 commit,
Jan 8, 2021
version 1
5f733358
1 commit,
Jan 8, 2021
1 file
+
101
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
src/main/java/nl/tudelft/librador/dto/helper/LCPageable.java
0 → 100644
+
101
−
0
View file @ 08bff152
Edit in single-file editor
Open in Web IDE
/*
* Librador - A utilities library for Labrador
* Copyright (C) 2020- Delft University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package
nl.tudelft.librador.dto.helper
;
import
java.util.Arrays
;
import
java.util.stream.Collectors
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.domain.Sort
;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public
class
LCPageable
{
private
int
page
;
private
int
size
;
private
String
sort
;
public
LCPageable
(
Pageable
pageable
)
{
this
.
page
=
pageable
.
getPageNumber
();
this
.
size
=
pageable
.
getPageSize
();
this
.
sort
=
sortToString
(
pageable
.
getSort
());
}
/**
* Converts this LCPageable object into a Spring Pageable to be used for Spring Data queries.
*
* @return The Pageable object representing the fully serialized version of this data.
*/
public
Pageable
toPageable
()
{
return
PageRequest
.
of
(
page
,
size
,
sortFromString
(
sort
));
}
/**
* Turns a Sort object into a String that fully represents the Sort object. This method can be used for
* serializing a Sort object, usually part of a Pageable over an HTTP request.
*
* @param sort The Sort object to serialize into a String.
* @return The serialized Sort object as a String.
*/
public
static
String
sortToString
(
Sort
sort
)
{
return
sort
.
stream
()
.
map
(
o
->
o
.
getProperty
()
+
" "
+
o
.
getDirection
())
.
collect
(
Collectors
.
joining
(
","
));
}
/**
* Reads a String into the Order object that the String represents. This method can be used for
* deserializing an Order object sent over an HTTP request as part of a Sort object.
*
* @param order The String that represents the Order sent.
* @return The resulting Order object.
*/
public
static
Sort
.
Order
orderFromString
(
String
order
)
{
String
[]
split
=
order
.
split
(
" "
);
if
(
split
.
length
<
2
)
{
return
Sort
.
Order
.
by
(
split
[
0
]);
}
else
{
return
new
Sort
.
Order
(
Sort
.
Direction
.
fromString
(
split
[
1
]),
split
[
0
]);
}
}
/**
* Reads a String into the Sort object that the String represents. This method can be used for
* deserializing a Sort object sent over an HTTP request.
*
* @param sort The String to convert back into the Sort object.
* @return The resulting Sort object as represented by the given String.
*/
public
static
Sort
sortFromString
(
String
sort
)
{
if
(
sort
.
isEmpty
())
{
return
Sort
.
unsorted
();
}
return
Sort
.
by
(
Arrays
.
stream
(
sort
.
split
(
","
))
.
map
(
LCPageable:
:
orderFromString
)
.
collect
(
Collectors
.
toList
()));
}
}
Loading