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

win.doyto.query.core.PageQuery Maven / Gradle / Ivy

There is a newer version: 0.2.2.1-RELEASE
Show newest version
package win.doyto.query.core;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import win.doyto.query.validation.PageGroup;

import javax.validation.constraints.Pattern;

import static java.lang.Math.max;

/**
 * PageQuery
 *
 * @author f0rb
 */
@Setter
@Accessors(chain = true)
public class PageQuery {

    private Integer pageNumber;

    private Integer pageSize;

    @ApiModelProperty(value = "Sorting field, format: field1,desc;field2,asc")
    @Pattern(regexp = "([_\\w]+,(asc|desc);)*[_\\w]+,(asc|desc)", message = "Sorting field format error", groups = PageGroup.class)
    @Getter
    private String sort;

    public Integer getPageNumber() {
        return getDefault(pageNumber, 0, pageSize == null);
    }

    public Integer getPageSize() {
        return getDefault(pageSize, 10, pageNumber == null);
    }

    private Integer getDefault(Integer number, int defaultValue, boolean canBeNull) {
        if (number == null) {
            if (canBeNull) {
                return null;
            } else {
                number = defaultValue;
            }
        }
        return max(0, number);
    }

    public int getOffset() {
        return needPaging() ? getPageSize() * getPageNumber() : 0;
    }

    public boolean needPaging() {
        return getPageNumber() != null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy