Method Of Retrieving Random Value Depending on Type I was wondering what would be the most appealing way of achieving this goal.

I have a blackbox. I give it a supported type and it returns a random value base off of the type.

For my supported types, I have an enum defined as follows:

public enum Types 
{
    INTEGER,
    DOUBLE,
    BIGINTEGER,
    BIGDECIMAL,
    STRING,
    BOOLEAN,
    CHAR
}


I thought the easiest way would be to create a static function which returns an Object based off of the Type

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();
    }
}


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.

I've looked into the design patterns abstract factory and factory, 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. 

Assume that all my random methods are defined.
1) I don't think you need to define an enum since you can use Boolean.class, Integer.class, etc.

2) 

public static &lt;T&gt; T getRandom(Class&lt;T&gt; classs) {
  if (classs.equals(Boolean.class)) {
      return (T) randomBool();
  } else if (classs.equals(Integer.class) {
      return (T) randomInt();
  // etc.
  } else
      // Edit: original "else" was: "return null;" changed to:
      throw new IllegalStateException("Cannot generate a random " + classs.toString());
}


EDIT
There is also the solution below, but that would still require casting:

   public enum Type {
    INTEGER {
        @Override public Integer getRandom() {
            return new randomInt();
        }
    }; // BOOLEAN, etc. 

    public abstract Object getRandom();
}


That is basically the same as the original OP code, but where the random method call is within the enum instead of being in a separate method with a switch-statement.