001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.hbtop.screen.top;
019
020import org.apache.yetus.audience.InterfaceAudience;
021
022
023/**
024 * Utility class for paging for the metrics.
025 */
026@InterfaceAudience.Private
027public class Paging {
028  private int currentPosition;
029  private int pageStartPosition;
030  private int pageEndPosition;
031
032  private int pageSize;
033  private int recordsSize;
034
035  public void init() {
036    currentPosition = 0;
037    pageStartPosition = 0;
038    pageEndPosition = Math.min(pageSize, recordsSize);
039  }
040
041  public void updatePageSize(int pageSize) {
042    this.pageSize = pageSize;
043
044    if (pageSize == 0) {
045      pageStartPosition = 0;
046      pageEndPosition = 0;
047    } else {
048      pageEndPosition = pageStartPosition + pageSize;
049      keepConsistent();
050    }
051  }
052
053  public void updateRecordsSize(int recordsSize) {
054    if (this.recordsSize == 0) {
055      currentPosition = 0;
056      pageStartPosition = 0;
057      pageEndPosition = Math.min(pageSize, recordsSize);
058      this.recordsSize = recordsSize;
059    } else if (recordsSize == 0) {
060      currentPosition = 0;
061      pageStartPosition = 0;
062      pageEndPosition = 0;
063      this.recordsSize = recordsSize;
064    } else {
065      this.recordsSize = recordsSize;
066      if (pageSize > 0) {
067        pageEndPosition = pageStartPosition + pageSize;
068        keepConsistent();
069      }
070    }
071  }
072
073  public void arrowUp() {
074    if (currentPosition > 0) {
075      currentPosition -= 1;
076      if (pageSize > 0) {
077        keepConsistent();
078      }
079    }
080  }
081
082  public void arrowDown() {
083    if (currentPosition < recordsSize - 1) {
084      currentPosition += 1;
085      if (pageSize > 0) {
086        keepConsistent();
087      }
088    }
089  }
090
091  public void pageUp() {
092    if (pageSize > 0 && currentPosition > 0) {
093      currentPosition -= pageSize;
094      if (currentPosition < 0) {
095        currentPosition = 0;
096      }
097      keepConsistent();
098    }
099  }
100
101  public void pageDown() {
102    if (pageSize > 0 && currentPosition < recordsSize - 1) {
103
104      currentPosition = currentPosition + pageSize;
105      if (currentPosition >= recordsSize) {
106        currentPosition = recordsSize - 1;
107      }
108
109      pageStartPosition = currentPosition;
110      pageEndPosition = pageStartPosition + pageSize;
111      keepConsistent();
112    }
113  }
114
115  private void keepConsistent() {
116    if (currentPosition < pageStartPosition) {
117      pageStartPosition = currentPosition;
118      pageEndPosition = pageStartPosition + pageSize;
119    } else if (currentPosition > recordsSize - 1) {
120      currentPosition = recordsSize - 1;
121      pageEndPosition = recordsSize;
122      pageStartPosition = pageEndPosition - pageSize;
123    } else if (currentPosition > pageEndPosition - 1) {
124      pageEndPosition = currentPosition + 1;
125      pageStartPosition = pageEndPosition - pageSize;
126    }
127
128    if (pageStartPosition < 0) {
129      pageStartPosition = 0;
130    }
131
132    if (pageEndPosition > recordsSize) {
133      pageEndPosition = recordsSize;
134      pageStartPosition = pageEndPosition - pageSize;
135      if (pageStartPosition < 0) {
136        pageStartPosition = 0;
137      }
138    }
139  }
140
141  public int getCurrentPosition() {
142    return currentPosition;
143  }
144
145  public int getPageStartPosition() {
146    return pageStartPosition;
147  }
148
149  public int getPageEndPosition() {
150    return pageEndPosition;
151  }
152}