abstractsettest3.java

来自「高质量Java程序设计 源代码」· Java 代码 · 共 76 行

JAVA
76
字号
package net.betterjava.design.refactor;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

public abstract class AbstractSetTest3 extends BatchTest3 {
	private final int indexOfRoundRpts = 3; //the index of inner loop in rpts
	private int factor = 1; //the mutliple factor od inner loop
	private int rpts[][] = { { 1000, 1000, 1000, 100 }, {
			100, 100, 100, 2000 }, {
			100, 100, 100, 4000 }, {
			100, 100, 100, 10000 }, {
			100, 100, 100, 20000 }
	}; //the number of outer and inner loop

	public AbstractSetTest3(String file) {
		super(5, 3, file);
	}

	protected String round(int currentTurn, int currentRound) {
		Object outerSet = null;

		switch (currentRound) {
			case 0 :
				outerSet = new HashSet();
				break;
			case 1 :
				outerSet = new LinkedHashSet();
				break;
			case 2 :
				outerSet = new TreeSet();
				break;
			default :
				throw new IllegalArgumentException(
					"currentRound:" + String.valueOf(currentRound));
		}

		log(
			"outer repeats "
				+ rpts[currentTurn][currentRound]
				+ " inner repeats "
				+ factor * rpts[currentTurn][indexOfRoundRpts]);
		long elapse = 0;
		for (int i = 0; i < rpts[currentTurn][currentRound]; i++) {
			Set innerSet;
			try {
				innerSet = (Set) outerSet.getClass().newInstance();
			} catch (InstantiationException e1) {
				log(e1);
				throw new IllegalStateException("cast error");
			} catch (IllegalAccessException e2) {
				log(e2);
				throw new IllegalStateException("cast error");
			}
			elapse += runTestBody(factor * rpts[currentTurn][indexOfRoundRpts], innerSet);
			innerSet = null;
		}
		outerSet = null;

		return String.valueOf(
			elapse
				* 1.0
				/ (factor
					* rpts[currentTurn][currentRound]
					* rpts[currentTurn][indexOfRoundRpts]));
	}

	protected abstract long runTestBody(int rpts, Set innerSet);

	protected void setFactor(int factor) {
		this.factor = factor;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?