Java tutorial
/*$Id: RandomGeneratorUtil.java 13605 2009-04-02 11:42:57Z jens $*/ /* **************************************************************************** * * * (c) Copyright 2009 ABM-utvikling * * * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * * Public License for more details. http://www.gnu.org/licenses/gpl.html * * * **************************************************************************** */ package no.abmu.test.util.randomgenerator; import java.util.Random; import no.abmu.util.test.Assert; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Provides useful helpers for generating random values. * * @author Jens Vindvad, Jens.Vindvad@abm-utvikling.no * @author $Author: jens $ * @version $Rev: 13605 $ * @date $Date: 2009-04-02 13:42:57 +0200 (Thu, 02 Apr 2009) $ * @copyright ABM-Utvikling */ public final class RandomGeneratorUtil { private static final Random randomGenerator = new Random(); private static final Log logger = (Log) LogFactory.getLog(RandomGeneratorUtil.class); private RandomGeneratorUtil() { } public static Integer createRandomInteger() { return randomGenerator.nextInt(); } public static Long createRandomLong() { return randomGenerator.nextLong(); } public static String createRandomString() { return String.valueOf(randomGenerator.nextLong()); } public static Integer createRandomADifferentInteger(Integer oldValue) { Assert.checkRequiredArgument("oldValue", oldValue); Integer newRandomValue = createRandomInteger(); // Just to be sure new value are different from old value. while (oldValue.equals(newRandomValue)) { String warningMessage = "[createRandomADifferentInteger] new and old integer has same value ='" + oldValue + "'."; System.out.println(warningMessage); logger.warn(warningMessage); newRandomValue = createRandomInteger(); } return newRandomValue; } public static Long createRandomADifferentLong(Long oldValue) { Assert.checkRequiredArgument("oldValue", oldValue); Long newRandomValue = createRandomLong(); // Just to be sure new value are different from old value. while (oldValue.equals(newRandomValue)) { String warningMessage = "[createRandomADifferentLong] new and old long has same value ='" + oldValue + "'."; System.out.println(warningMessage); logger.warn(warningMessage); newRandomValue = createRandomLong(); } return newRandomValue; } }