Skip to content

Commit

Permalink
Update getter for SpringQueryFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
Zorin95670 committed Nov 19, 2024
1 parent a022ca0 commit a634daf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,12 @@ public class SpringQueryFilter implements ISpringQueryFilter {
private String sort;

@Override
public int getPage() {
public int getComputedPage() {
return Math.max(Optional.ofNullable(page).orElse(0), 0);
}

@Override
public int getPageSize() {
public int getComputedSize() {
return Math.clamp(Optional.ofNullable(pageSize).orElse(DEFAULT_PAGE_SIZE), MIN_PAGE_SIZE, MAX_PAGE_SIZE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public interface ISpringQueryFilter {
*
* @return the page number (0-based index).
*/
int getPage();
int getComputedPage();

/**
* Retrieves the page size for pagination.
*
* @return the number of records per page.
*/
int getPageSize();
int getComputedPageSize();

/**
* Retrieves the field by which results should be ordered.
Expand Down Expand Up @@ -81,9 +81,9 @@ default Pageable getPageable(String defaultOrder) {
Sort sort = this.getOrderBy(defaultOrder);

if (sort != null) {
return PageRequest.of(this.getPage(), this.getPageSize(), sort);
return PageRequest.of(this.getComputedPage(), this.getComputedPageSize(), sort);
}

return PageRequest.of(this.getPage(), this.getPageSize());
return PageRequest.of(this.getComputedPage(), this.getComputedPageSize());
}
}
70 changes: 50 additions & 20 deletions src/main/java/io/github/zorin95670/query/SpringQueryFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ public class SpringQueryFilter implements ISpringQueryFilter {
/**
* Page number for pagination (0-based index). Defaults to 0 if not specified.
*/
protected Integer page = 0;
private Integer page = 0;

/**
* Number of records per page. Defaults to 1 if not specified.
*/
protected Integer pageSize = 1;
private Integer pageSize = 1;

/**
* Field name by which results should be ordered. Can be set to null.
*/
protected String order;
private String order = null;

/**
* Sorting direction. Should be "asc" for ascending order; any other value is treated as descending.
*/
protected String sort;
private String sort = null;

/**
* Default no-argument constructor.
Expand All @@ -57,42 +57,72 @@ public SpringQueryFilter() {
* @param order the field name by which results should be ordered.
* @param sort the sorting direction (ascending or descending).
*/
public SpringQueryFilter(Integer page, Integer pageSize, String order, String sort) {
public SpringQueryFilter(final Integer page, final Integer pageSize, final String order, final String sort) {
this.page = page;
this.pageSize = pageSize;
this.order = order;
this.sort = sort;
}

/**
* Retrieves the page number for pagination, defaulting to 0 if the page is null or less than 0.
* Returns the page number for pagination (0-based index).
* Defaults to 0 if not specified.
*
* @return the page number, constrained to be non-negative.
* @return the page number (0-based index)
*/
@Override
public int getPage() {
return Math.max(Optional.ofNullable(page).orElse(0), 0);
public Integer getPage() {
return page;
}

/**
* Retrieves the page size for pagination, constrained between 1 and 10.
* Defaults to 1 if page size is null or less than 1.
* Returns the number of records per page.
* Defaults to 1 if not specified.
*
* @return the page size, bounded by a minimum of 1 and a maximum of 10.
* @return the number of records per page
*/
public Integer getPageSize() {
return pageSize;
}

/**
* Returns the field name by which results should be ordered.
* Can be set to null.
*
* @return the field name for ordering, or null if not specified
*/
public String getOrder() {
return order;
}

/**
* Returns the sorting direction.
* Should be "asc" for ascending order; any other value is treated as descending.
*
* @return the sorting direction, either "asc" for ascending or any other value for descending
*/
public String getSort() {
return sort;
}

/**
* Retrieves the page number for pagination, defaulting to 0 if the page is null or less than 0.
*
* @return the page number, constrained to be non-negative.
*/
@Override
public int getPageSize() {
return Math.clamp(Optional.ofNullable(pageSize).orElse(DEFAULT_PAGE_SIZE), MIN_PAGE_SIZE, MAX_PAGE_SIZE);
public int getComputedPage() {
return Math.max(Optional.ofNullable(getPage()).orElse(0), 0);
}

/**
* Retrieves the field name by which results should be ordered.
* Retrieves the page size for pagination, constrained between 1 and 10.
* Defaults to 1 if page size is null or less than 1.
*
* @return the order field, or null if not set.
* @return the page size, bounded by a minimum of 1 and a maximum of 10.
*/
@Override
public String getOrder() {
return this.order;
public int getComputedPageSize() {
return Math.clamp(Optional.ofNullable(getPageSize()).orElse(DEFAULT_PAGE_SIZE), MIN_PAGE_SIZE, MAX_PAGE_SIZE);
}

/**
Expand All @@ -102,6 +132,6 @@ public String getOrder() {
*/
@Override
public boolean isAscendantSort() {
return "asc".equalsIgnoreCase(sort);
return "asc".equalsIgnoreCase(getSort());
}
}

0 comments on commit a634daf

Please sign in to comment.