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 * Utility class for paging for the metrics.
024 */
025@InterfaceAudience.Private
026public class Paging {
027  private int currentPosition;
028  private int pageStartPosition;
029  private int pageEndPosition;
030
031  private int pageSize;
032  private int recordsSize;
033
034  public void init() {
035    currentPosition = 0;
036    pageStartPosition = 0;
037    pageEndPosition = Math.min(pageSize, recordsSize);
038  }
039
040  public void updatePageSize(int pageSize) {
041    this.pageSize = pageSize;
042
043    if (pageSize == 0) {
044      pageStartPosition = 0;
045      pageEndPosition = 0;
046    } else {
047      pageEndPosition = pageStartPosition + pageSize;
048      keepConsistent();
049    }
050  }
051
052  public void updateRecordsSize(int recordsSize) {
053    if (this.recordsSize == 0) {
054      currentPosition = 0;
055      pageStartPosition = 0;
056      pageEndPosition = Math.min(pageSize, recordsSize);
057      this.recordsSize = recordsSize;
058    } else if (recordsSize == 0) {
059      currentPosition = 0;
060      pageStartPosition = 0;
061      pageEndPosition = 0;
062      this.recordsSize = recordsSize;
063    } else {
064      this.recordsSize = recordsSize;
065      if (pageSize > 0) {
066        pageEndPosition = pageStartPosition + pageSize;
067        keepConsistent();
068      }
069    }
070  }
071
072  public void arrowUp() {
073    if (currentPosition > 0) {
074      currentPosition -= 1;
075      if (pageSize > 0) {
076        keepConsistent();
077      }
078    }
079  }
080
081  public void arrowDown() {
082    if (currentPosition < recordsSize - 1) {
083      currentPosition += 1;
084      if (pageSize > 0) {
085        keepConsistent();
086      }
087    }
088  }
089
090  public void pageUp() {
091    if (pageSize > 0 && currentPosition > 0) {
092      currentPosition -= pageSize;
093      if (currentPosition < 0) {
094        currentPosition = 0;
095      }
096      keepConsistent();
097    }
098  }
099
100  public void pageDown() {
101    if (pageSize > 0 && currentPosition < recordsSize - 1) {
102
103      currentPosition = currentPosition + pageSize;
104      if (currentPosition >= recordsSize) {
105        currentPosition = recordsSize - 1;
106      }
107
108      pageStartPosition = currentPosition;
109      pageEndPosition = pageStartPosition + pageSize;
110      keepConsistent();
111    }
112  }
113
114  private void keepConsistent() {
115    if (currentPosition < pageStartPosition) {
116      pageStartPosition = currentPosition;
117      pageEndPosition = pageStartPosition + pageSize;
118    } else if (currentPosition > recordsSize - 1) {
119      currentPosition = recordsSize - 1;
120      pageEndPosition = recordsSize;
121      pageStartPosition = pageEndPosition - pageSize;
122    } else if (currentPosition > pageEndPosition - 1) {
123      pageEndPosition = currentPosition + 1;
124      pageStartPosition = pageEndPosition - pageSize;
125    }
126
127    if (pageStartPosition < 0) {
128      pageStartPosition = 0;
129    }
130
131    if (pageEndPosition > recordsSize) {
132      pageEndPosition = recordsSize;
133      pageStartPosition = pageEndPosition - pageSize;
134      if (pageStartPosition < 0) {
135        pageStartPosition = 0;
136      }
137    }
138  }
139
140  public int getCurrentPosition() {
141    return currentPosition;
142  }
143
144  public int getPageStartPosition() {
145    return pageStartPosition;
146  }
147
148  public int getPageEndPosition() {
149    return pageEndPosition;
150  }
151}