title: Java - Generics vs Casting Objects
body: <p>I have a class <code>Data&lt;T&gt;</code></p>
  
  <p>with a generic attribute</p>
  
  <pre><code>private T value;
  </code></pre>
  
  <p>is there nicer way to do the following?<br>
  ie returning the generic type in different forms?</p>
  
  <pre><code>public List&lt;String&gt; getValues() {
      if (value.getClass() != ArrayList.class)
          throw new Exception("Wrong Enum value '%s'", value);
      return (ArrayList&lt;String&gt;) value;
      //ugly
  }
  
  
  public String getStringValue() {
      if (value.getClass() != String.class)
          throw new Exception("Wrong value type '%s'", value);
      return (String) value;
      //ugly
  }
  
  public Float getFloatValue() {
      if (value.getClass() != Double.class)
          throw new Exception("Wrong value type '%s'", value);
      return (Float) value;
      //ugly
  }
  
  public Long getLongValue() {
      if (value.getClass() != Double.class)
          throw new Exception("Wrong value type '%s'", value);
      return (Long) value;
      //ugly
  }
  public T getValue() {
      return value;
  }
  </code></pre>
  
  <p>Precision, I'm using Gson as deserializer, to get a List, each Data objects can then be heterogeous<br>
  Could also register adapters for float and long detection, but it wouldn't be faster or nicer</p>
  
  <p><strong>edit</strong>: gson fails to retrieve longs:</p>
  
  <p>either:     </p>
  
  <pre><code> ((Long) d.getValue())
  </code></pre>
  
  <p>java.lang.Double cannot be cast to java.lang.Long</p>
  
  <p>or  </p>
  
  <pre><code>Long.parseLong( d.getValue().toString())
  </code></pre>
  
  <p>java.lang.NumberFormatException: For input string: "212231.0"</p>
  
  <p>I tried to register a LongAdpater</p>
  
  <pre><code>gsonBuilder.registerTypeAdapter(Long.class, new LongAdapter());
  
  private static class LongAdapter implements 
      JsonSerializer&lt;Long&gt;, JsonDeserializer&lt;Long&gt; 
  {
  
      @Override public Long deserialize(
              JsonElement json, 
              Type type,
              JsonDeserializationContext arg2) throws JsonParseException 
      {
          return json.getAsLong();
      }
  
      @Override
      public JsonElement serialize(Long l, Type arg1,
              JsonSerializationContext arg2) {
          return new JsonPrimitive(new Double(l));
      }
  }
  </code></pre>
  
  <p>java.lang.IllegalArgumentException: Cannot register type adapters for class java.lang.Long</p>
  
  <p><strong>edit2</strong> for tsOverflow:</p>
  
  <pre><code>Data&lt;Float&gt; d1 = new Data&lt;Float&gt;( new Float(6.32));
  List&lt;String&gt; l = new ArrayList&lt;String&gt;();
      l.add("fr");
      l.add("it");
      l.add("en");
  Data&lt;List&lt;String&gt;&gt; d2 = new Data&lt;List&lt;String&gt;&gt;( l);
  Data&lt;Long&gt; d3 = new Data&lt;Long&gt;(new Long(212231));
  
  List&lt;Data&gt; data = new ArrayList&lt;Data&gt;();
      data.add(d1);
      data.add(d2);
      data.add(d3)
  
  new Gson().toJson(data);
  </code></pre>