github.com/dubbogo/gost@v1.14.0/hash/page/page.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxpage
    19  
    20  // Page is the default implementation of Page interface
    21  type Page struct {
    22  	requestOffset int
    23  	pageSize      int
    24  	totalSize     int
    25  	data          []interface{}
    26  	totalPages    int
    27  	hasNext       bool
    28  }
    29  
    30  // GetOffSet will return the offset
    31  func (d *Page) GetOffset() int {
    32  	return d.requestOffset
    33  }
    34  
    35  // GetPageSize will return the page size
    36  func (d *Page) GetPageSize() int {
    37  	return d.pageSize
    38  }
    39  
    40  // GetTotalPages will return the number of total pages
    41  func (d *Page) GetTotalPages() int {
    42  	return d.totalPages
    43  }
    44  
    45  // GetData will return the data
    46  func (d *Page) GetData() []interface{} {
    47  	return d.data
    48  }
    49  
    50  // GetDataSize will return the size of data.
    51  // it's len(GetData())
    52  func (d *Page) GetDataSize() int {
    53  	return len(d.GetData())
    54  }
    55  
    56  // HasNext will return whether has next page
    57  func (d *Page) HasNext() bool {
    58  	return d.hasNext
    59  }
    60  
    61  // HasData will return whether this page has data.
    62  func (d *Page) HasData() bool {
    63  	return d.GetDataSize() > 0
    64  }
    65  
    66  // NewPage will create an instance
    67  func NewPage(requestOffset int, pageSize int,
    68  	data []interface{}, totalSize int) *Page {
    69  
    70  	remain := totalSize % pageSize
    71  	totalPages := totalSize / pageSize
    72  	if remain > 0 {
    73  		totalPages++
    74  	}
    75  
    76  	hasNext := totalSize-requestOffset-pageSize > 0
    77  
    78  	return &Page{
    79  		requestOffset: requestOffset,
    80  		pageSize:      pageSize,
    81  		data:          data,
    82  		totalSize:     totalSize,
    83  		totalPages:    totalPages,
    84  		hasNext:       hasNext,
    85  	}
    86  }