All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javasimon.examples.ExampleUtils Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
package org.javasimon.examples;

import org.javasimon.Manager;
import org.javasimon.SimonManager;

import java.util.Random;

/**
 * Contains some supportive utils common for more examples.
 *
 * @author Richard "Virgo" Richter
 * @since 3.1
 */
public final class ExampleUtils {
	private ExampleUtils() {
	}

	/**
	 * Method that lasts randomly from ~0 to the square of the specified amount of maxMsRoot.
	 * This is just to avoid linear randomness.
	 *
	 * @param maxMsRoot square root of the maximal waiting time
	 */
	public static void waitRandomly(long maxMsRoot) {
		long random = (long) (Math.random() * maxMsRoot);
		try {
			Thread.sleep(random * random);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private static final String[] RANDOM_NAMES = ("Deadra Breakell Rod Herrero Genesis Boutilier Cliff Daus Carey Chevas" +
		" Loralee Rizvi Virgen Pahler Un Muscat Elwood Poeppel Jeffry Carlise Kuramoto Bibi Whatcott Lianne Tellefson" +
		" Ruthanne Stipes Elwood Kisselburg Raphael Maxam Pura Abrecht Rod Jernberg Bok Mehrtens Brittanie Palamino" +
		" Jeffry Wansing Delsie Palms Rob Doub Moises Minney Armand Khaleel").split(" ");

	private static final Random RANDOM = new Random();

	/**
	 * Fills the {@link org.javasimon.SimonManager} with specified number of Simons (or slightly more).
	 *
	 * @param roughCount how many Stopwatches to create (or a bit more)
	 */
	public static void fillManagerWithSimons(int roughCount) {
		System.out.print("Filling manager with ~" + roughCount + " Simons...");
		while (SimonManager.getSimonNames().size() < roughCount) {
			SimonManager.getStopwatch(generateRandomName(RANDOM.nextInt(10) + 1));
		}
		System.out.println(" " + SimonManager.getSimonNames().size() + " created.");
	}

	private static String generateRandomName(int depth) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < depth; i++) {
			if (sb.length() > 0) {
				sb.append(Manager.HIERARCHY_DELIMITER);
			}
			sb.append(RANDOM_NAMES[RANDOM.nextInt(RANDOM_NAMES.length)]);
		}
		return sb.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy