title: Java how to manage user-defined variables
body: <p>I'm developing some software where I'd like to allow the user to create and manage their own variables.  I'd like to give the user the ability to create doubles, integers, and strings, and then manually set their values (all of this will be done through a UI).  Right now I'm trying to decide on a model that would manage all of these user-defined variables.</p>
  
  <p>My first thought was that it would be nice to have a Map that fetches variables by their name and use a "one-size-fits-all" 'Variable' class...</p>
  
  <pre><code>Map&lt;String, Variable&gt; myVars;
  </code></pre>
  
  <p>I thought to define the Variable class something like this...</p>
  
  <pre><code>public class Variable&lt;T&gt; 
  {
  
  private T value;
  
  public void setValue(T value)
  {
      this.value = value;
  }
  
  public T getValue()
  {
      return value;
  }
  
  }
  </code></pre>
  
  <p>However, I think this approach requires me to define the map as <code>Map&lt;String, Variable&lt;T&gt; &gt;</code> but I don't want to choose a single type <code>T</code> for the entire map, but rather allow the map to contain a combination of integers, doubles, and strings.</p>
  
  <p>A second thought was to create a container class that maintains three different maps, one each for strings, integers, and doubles.  The three maps are insulated from the caller, and the caller merely needs to provide a String (the variable name) to a getter and the container would get the variable's value from the appropriate map.  The container could also take care of issues like maintaining uniqueness of keys across all three maps.</p>
  
  <p>A stub might look something like this...</p>
  
  <pre><code>public class VariableContainer {
  
  private Map&lt;String, Double&gt; doubleVars = new HashMap();
  private Map&lt;String, String&gt; stringVars = new HashMap();
  private Map&lt;String, Integer&gt; intVars = new HashMap();
  
  public Object get(String varName)
  {
      if (doubleVars.containsKey(varName)) {
          return doubleVars.get(varName);
      } else if (stringVars.containsKey(varName)) {
          return stringVars.get(varName);
      } else if (intVars.containsKey(varName)) {
          return intVars.get(varName)
      } else {
          // varName not found
      }
  }
  }
  </code></pre>
  
  <p>However, now the caller of the <code>get</code> method must do inspection using <code>instanceof</code> to determine what kind of Object has been returned.</p>
  
  <p>On that note, if inspection on the part of the caller is required, maybe it would be easiest to just stick with </p>
  
  <pre><code>Map&lt;String, Object&gt;
  </code></pre>
  
  <p>and forget about an abstract <code>Variable</code> class or a container class.</p>
  
  <p>Perhaps yet another option is to return a wildcard as described in the answer to this question:
  <a href="https://stackoverflow.com/questions/10507466/avoiding-returning-wildcard-types">Avoiding Returning Wildcard Types</a></p>
  
  <p>Does anyone have any thoughts or experience on a good way to approach this?  Thanks for any input.</p>