title: Method Of Retrieving Random Value Depending on Type
body: <p>I was wondering what would be the most appealing way of achieving this goal.</p>
  
  <p>I have a blackbox. I give it a <code>supported type</code> and it returns a random value base off of the type.</p>
  
  <p>For my supported types, I have an <code>enum</code> defined as follows:</p>
  
  <pre><code>public enum Types 
  {
      INTEGER,
      DOUBLE,
      BIGINTEGER,
      BIGDECIMAL,
      STRING,
      BOOLEAN,
      CHAR
  }
  </code></pre>
  
  <p>I thought the easiest way would be to create a static function which returns an Object based off of the <code>Type</code></p>
  
  <pre><code>public static Object getRandomValue(Types type)
  {
      switch(type)
      {
          case INTEGER:
              return randomInt();
          case DOUBLE:
              return randomDouble();
          case BIGINTEGER:
              return randomBigInt();
          case BIGDECIMAL:
              return randomBigDecimal();
          case STRING:
              return randomString();
          case BOOLEAN:
              return randomBool();
          case CHAR:
              return randomChar();
      }
  }
  </code></pre>
  
  <p>The problem is that an additional cast would have to be made each time that I want to retrieve a random value based off of this method.</p>
  
  <p>I've looked into the design patterns <code>abstract factory</code> and <code>factory</code>, and I can't decide if there's an advantage into implementing either of those. Or if there's a different design pattern that seems to be more appropriate. </p>
  
  <p>Assume that all my <code>random</code> methods are defined. </p>
  
  <p>Here's an example of the client:</p>
  
  <pre><code>public abstract class MyClass&lt;T&gt;
  {
      protected T t;
  }
  
  public class MyIntClass extends MyClass&lt;Integer&gt;
  {
      public MyIntClass()
      {
          t = getRandomValue(Types.Integer);
      }
  }
  </code></pre>