単純改ページアルゴリズム

2151 ワード

ほとんどのプログラムには改ページアルゴリズムが必要で、明らかに改ページアルゴリズムを独立させなければなりません.業務ロジックも入り混じっていれば、修正するのは苦痛なことです.実は改ページアルゴリズムの設計は解方程式のようで、いくつかの変数を入力して、他のいくつかの変数を求めます.以下に簡単な設計があります.総記録数を入力し、現在のページからページ数をジャンプします.要求の索引を出力します.もちろん、この簡単なアルゴリズムに基づいて他の情報を修正して出力してもいいです.
       
 
package paging;

public class Page {
	/**
	 *         ,        
	 */
	public static final int PAGESIZE = 20;
	/**
	 *     
	 */
	private int records;

	/**
	 *    , 1    
	 */
	private int now_page;

	/**
	 *    
	 */
	public int pages;

	/**
	 *     ,             
	 */
	public int request_index;
	/**
	 *       Page  
	 */
	public boolean ok = true;

	public Page() {
	}

	/**
	 *             ,         
	 */
	public Page(int now_page) {
		this(PAGESIZE, now_page, 0);
	}

	public Page(int now_page, int go_count) {
		this(PAGESIZE, now_page, go_count);
	}

	/**
	 * 
	 * @param records
	 *                
	 * @param now_page
	 *                   
	 * @param go_count
	 *                (    )
	 */
	public Page(int records, int now_page, int go_count) {
		if ((now_page - 1) * PAGESIZE >= records || now_page <= 0) {
			System.out.println("  Page  -1!");
			ok = false;
		}
		if (now_page + go_count <= 0 || (now_page + go_count - 1) * PAGESIZE >= records) {
			System.out.println("  Page  -2!");
			ok = false;
		}

		this.now_page = now_page;
		this.records = records;
		this.request_index = (this.now_page + go_count - 1) * PAGESIZE;

		if (this.records % PAGESIZE == 0) {
			this.pages = this.records / PAGESIZE;
		} else {
			this.pages = this.records / PAGESIZE + 1;
		}
	}

	@Override
	public String toString() {
		return "pages:" + pages + "  " + "request_index:" + request_index;
	}

}