Skip to content
Snippets Groups Projects

Add a Pageable object that can be transferred

1 file
+ 101
0
Compare changes
  • Side-by-side
  • Inline
/*
* 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