Class RandomHelper


  • public class RandomHelper
    extends java.lang.Object
    Assists in the generation of random instances of a given object model.
    Version:
    $Revision: 6785 $ $Date: 2006-04-13 06:47:36 -0600 (Thu, 13 Apr 2006) $
    Author:
    Sebastien Gignoux
    • Constructor Summary

      Constructors 
      Constructor Description
      RandomHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean flip()
      Returns true or false randomly with equal probability.
      static boolean flip​(double p)
      Returns true randomly with the probability p.
      static boolean getRandom​(boolean b)
      Returns a random boolean.
      static byte getRandom​(byte b)
      Returns a random byte.
      static byte[] getRandom​(byte[] array, java.lang.Class c)
      Returns a populated array of byte of random length.
      static char getRandom​(char c)
      Returns a random printable char.
      static double getRandom​(double d)
      Returns a random double.
      static float getRandom​(float f)
      Returns a random float.
      static int getRandom​(int i)
      Returns a random int.
      static int[] getRandom​(int[] array, java.lang.Class c)
      Returns a populated array of int of random length.
      static long getRandom​(long l)
      Returns a random long.
      static short getRandom​(short l)
      Returns a random short.
      static java.lang.Object[] getRandom​(java.lang.Object[] array, java.lang.Class c)
      Creates a populated array of type c of random length.
      static java.lang.Object getRandom​(java.lang.Object object, java.lang.Class c)
      Returns a random Object of the type provided by class c.
      static java.lang.String[] getRandom​(java.lang.String[] array, java.lang.Class c)
      Returns a populated array of String of random length.
      static java.lang.String getRandom​(java.lang.String s, java.lang.Class c)
      Returns a random String that will not have leading or trailing whitespace and that will not have consecutive internal whitespace.
      static java.lang.String getRandom​(java.lang.String s, java.lang.Class c, boolean collapseWhitespace)
      Returns a random String, optionally with leading and trailing whitespace removed and internal consecutive whitespace collapsed.
      static java.math.BigDecimal getRandom​(java.math.BigDecimal bg, java.lang.Class c)
      Returns a random BigDecimal.
      static java.util.ArrayList getRandom​(java.util.ArrayList al, java.lang.Class c)
      Returns a populated ArrayList of random length.
      static java.util.Collection getRandom​(java.util.Collection al, java.lang.Class c)
      Returns a populated Collection of random length.
      static java.util.Date getRandom​(java.util.Date date, java.lang.Class c)
      Returns a random java.util.Date.
      static java.util.List getRandom​(java.util.List al, java.lang.Class c)
      Returns a populated List of random length.
      static java.util.Set getRandom​(java.util.Set al, java.lang.Class c)
      Returns a populated Set of random length.
      static java.util.SortedSet getRandom​(java.util.SortedSet al, java.lang.Class c)
      Returns a populated SortedSet of random length.
      static java.util.Vector getRandom​(java.util.Vector vect, java.lang.Class c)
      Returns a populated vector of random length.
      static RecurringDuration getRandom​(RecurringDuration recurring, java.lang.Class c)
      Returns a random Castor RecurringDuration.
      static TimeDuration getRandom​(TimeDuration date, java.lang.Class c)
      Returns a random Castor TimeDuration.
      static long getSeed()
      Returns the seed which was used to initialize the pseudo-random number generator.
      static char rndPrintableChar()
      Returns a random printable character from the PRINTABLE_CHAR string.
      static void setSeed​(long seed)
      Re-initializes the random number generator with the given seed.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RandomHelper

        public RandomHelper()
    • Method Detail

      • getRandom

        public static int[] getRandom​(int[] array,
                                      java.lang.Class c)
        Returns a populated array of int of random length.
        Parameters:
        array - An unused parameter, used only for polymorphism.
        c - An unused parameter that indicates we are making a random Object, not a random primitive
        Returns:
        a populated array of int of random length.
      • getRandom

        public static byte[] getRandom​(byte[] array,
                                       java.lang.Class c)
        Returns a populated array of byte of random length.
        Parameters:
        array - An unused parameter, used only for polymorphism.
        c - An unused parameter that indicates we are making a random Object, not a random primitive
        Returns:
        a populated array of byte of random length.
      • getRandom

        public static java.lang.String[] getRandom​(java.lang.String[] array,
                                                   java.lang.Class c)
        Returns a populated array of String of random length.
        Parameters:
        array - An unused parameter, used only for polymorphism.
        c - An unused parameter that indicates we are making a random Object, not a random primitive
        Returns:
        a populated array of String of random length.
      • getRandom

        public static java.lang.Object[] getRandom​(java.lang.Object[] array,
                                                   java.lang.Class c)
                                            throws java.lang.InstantiationException,
                                                   java.lang.IllegalAccessException
        Creates a populated array of type c of random length. If the class to put into the vector implements CastorTestable, randomizeFields() will be called on the objects.
        Parameters:
        array - An unused parameter, used only for polymorphism.
        c - the type of object to put in the array
        Returns:
        a populated array of random length.
        Throws:
        java.lang.InstantiationException - if the class cannot be instantiated.
        java.lang.IllegalAccessException - if the class cannot be accessed.
      • getRandom

        public static java.util.Vector getRandom​(java.util.Vector vect,
                                                 java.lang.Class c)
                                          throws java.lang.InstantiationException,
                                                 java.lang.IllegalAccessException
        Returns a populated vector of random length. If the class to put into the vector implements CastorTestable, randomizeFields() will be called on the objects.
        Parameters:
        vect - the vector to populate, if null a new Vector will be created.
        c - the type of object to put in the vector.
        Returns:
        a populated Vector of random length.
        Throws:
        java.lang.InstantiationException - if the class cannot be instantiated.
        java.lang.IllegalAccessException - if the class cannot be accessed.
      • getRandom

        public static java.util.ArrayList getRandom​(java.util.ArrayList al,
                                                    java.lang.Class c)
                                             throws java.lang.InstantiationException,
                                                    java.lang.IllegalAccessException
        Returns a populated ArrayList of random length. If the class of the object contained into the vector implements CastorTestable, randomizeFields() will be called on the objects.
        Parameters:
        al - the ArrayList to populate
        c - the type of object to put in the vector
        Returns:
        a populated ArrayList of random length.
        Throws:
        java.lang.InstantiationException - if the class cannot be instantiated
        java.lang.IllegalAccessException - if the class cannot be accessed
      • getRandom

        public static java.util.Collection getRandom​(java.util.Collection al,
                                                     java.lang.Class c)
                                              throws java.lang.InstantiationException,
                                                     java.lang.IllegalAccessException
        Returns a populated Collection of random length. If the class of the object contained into the vector implements CastorTestable, randomizeFields() will be called on the objects.
        Parameters:
        al - the ArrayList to populate
        c - the type of object to put in the vector
        Returns:
        a populated ArrayList of random length.
        Throws:
        java.lang.InstantiationException - if the class cannot be instantiated
        java.lang.IllegalAccessException - if the class cannot be accessed
      • getRandom

        public static java.util.List getRandom​(java.util.List al,
                                               java.lang.Class c)
                                        throws java.lang.InstantiationException,
                                               java.lang.IllegalAccessException
        Returns a populated List of random length. If the class of the object contained into the vector implements CastorTestable, randomizeFields() will be called on the objects.
        Parameters:
        al - the ArrayList to populate
        c - the type of object to put in the vector
        Returns:
        a populated ArrayList of random length.
        Throws:
        java.lang.InstantiationException - if the class cannot be instantiated
        java.lang.IllegalAccessException - if the class cannot be accessed
      • getRandom

        public static java.util.Set getRandom​(java.util.Set al,
                                              java.lang.Class c)
                                       throws java.lang.InstantiationException,
                                              java.lang.IllegalAccessException
        Returns a populated Set of random length. If the class of the object contained into the vector implements CastorTestable, randomizeFields() will be called on the objects.
        Parameters:
        al - the Set to populate
        c - the type of object to put in the vector
        Returns:
        a populated Set of random length.
        Throws:
        java.lang.InstantiationException - if the class cannot be instantiated
        java.lang.IllegalAccessException - if the class cannot be accessed
      • getRandom

        public static java.util.SortedSet getRandom​(java.util.SortedSet al,
                                                    java.lang.Class c)
                                             throws java.lang.InstantiationException,
                                                    java.lang.IllegalAccessException
        Returns a populated SortedSet of random length. If the class of the object contained into the vector implements CastorTestable, randomizeFields() will be called on the objects.
        Parameters:
        al - the SortedSet to populate
        c - the type of object to put in the vector
        Returns:
        a populated SortedSet of random length.
        Throws:
        java.lang.InstantiationException - if the class cannot be instantiated
        java.lang.IllegalAccessException - if the class cannot be accessed
      • getRandom

        public static java.lang.String getRandom​(java.lang.String s,
                                                 java.lang.Class c)
        Returns a random String that will not have leading or trailing whitespace and that will not have consecutive internal whitespace. To get a random string without these restrictions, use getRandom(String, Class, boolean) with the boolean argument false.
        Parameters:
        s - An unused parameter, used only for polymorphism.
        c - An unused parameter that indicates we are making a random Object, not a random primitive
        Returns:
        a random string
      • getRandom

        public static java.lang.String getRandom​(java.lang.String s,
                                                 java.lang.Class c,
                                                 boolean collapseWhitespace)
        Returns a random String, optionally with leading and trailing whitespace removed and internal consecutive whitespace collapsed.
        Parameters:
        s - An unused parameter, used only for polymorphism.
        c - An unused parameter that indicates we are making a random Object, not a random primitive
        collapseWhitespace - if true, removes leading and trailing whitespace and collapses multiple consecutive spaces.
        Returns:
        a random string
        See Also:
        the XML schema definition of whitespace
      • getRandom

        public static java.util.Date getRandom​(java.util.Date date,
                                               java.lang.Class c)
        Returns a random java.util.Date.
        Parameters:
        date - An unused parameter, used only for polymorphism.
        c - An unused parameter that indicates we are making a random Object, not a random primitive
        Returns:
        a random java.util.Date.
      • getRandom

        public static TimeDuration getRandom​(TimeDuration date,
                                             java.lang.Class c)
        Returns a random Castor TimeDuration.
        Parameters:
        date - An unused parameter, used only for polymorphism.
        c - An unused parameter that indicates we are making a random Object, not a random primitive
        Returns:
        a random Castor timeDuration.
      • getRandom

        public static RecurringDuration getRandom​(RecurringDuration recurring,
                                                  java.lang.Class c)
        Returns a random Castor RecurringDuration.
        Parameters:
        recurring - An unused parameter, used only for polymorphism.
        c - An unused parameter that indicates we are making a random Object, not a random primitive
        Returns:
        a random Castor recurringDuration.
      • getRandom

        public static java.lang.Object getRandom​(java.lang.Object object,
                                                 java.lang.Class c)
        Returns a random Object of the type provided by class c.
        Parameters:
        object - An unused parameter, used only for polymorphism.
        c - the type of object we will create a randomized instance of. This class must implement CastorTestable.
        Returns:
        a random Object.
      • getRandom

        public static java.math.BigDecimal getRandom​(java.math.BigDecimal bg,
                                                     java.lang.Class c)
        Returns a random BigDecimal.
        Parameters:
        bg - An unused parameter, used only for polymorphism.
        c - An unused parameter that indicates we are making a random Object, not a random primitive
        Returns:
        a random BigDecimal.
      • getRandom

        public static int getRandom​(int i)
        Returns a random int.
        Parameters:
        i - An unused parameter, used only for polymorphism.
        Returns:
        a random int.
      • getRandom

        public static float getRandom​(float f)
        Returns a random float.
        Parameters:
        f - An unused parameter, used only for polymorphism.
        Returns:
        a random float.
      • getRandom

        public static boolean getRandom​(boolean b)
        Returns a random boolean.
        Parameters:
        b - An unused parameter, used only for polymorphism.
        Returns:
        a random boolean.
      • getRandom

        public static long getRandom​(long l)
        Returns a random long.
        Parameters:
        l - An unused parameter, used only for polymorphism.
        Returns:
        a random long.
      • getRandom

        public static short getRandom​(short l)
        Returns a random short.
        Parameters:
        l - An unused parameter, used only for polymorphism.
        Returns:
        a random short.
      • getRandom

        public static double getRandom​(double d)
        Returns a random double.
        Parameters:
        d - An unused parameter, used only for polymorphism.
        Returns:
        a random double.
      • getRandom

        public static char getRandom​(char c)
        Returns a random printable char.
        Parameters:
        c - An unused parameter, used only for polymorphism.
        Returns:
        a random printable char.
      • getRandom

        public static byte getRandom​(byte b)
        Returns a random byte.
        Parameters:
        b - An unused parameter, used only for polymorphism.
        Returns:
        a random byte.
      • flip

        public static boolean flip()
        Returns true or false randomly with equal probability.
        Returns:
        true or false randomly with equal probability.
      • flip

        public static boolean flip​(double p)
        Returns true randomly with the probability p.
        Parameters:
        p - A probability for returning true
        Returns:
        true p% of the time
      • rndPrintableChar

        public static char rndPrintableChar()
        Returns a random printable character from the PRINTABLE_CHAR string.
        Returns:
        a random printable character from the PRINTABLE_CHAR string.
      • getSeed

        public static long getSeed()
        Returns the seed which was used to initialize the pseudo-random number generator.
        Returns:
        the seed which was used to initialize the pseudo-random number generator
      • setSeed

        public static void setSeed​(long seed)
        Re-initializes the random number generator with the given seed.
        Parameters:
        seed - the new seed for the random number generator.