Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8198977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:04:38+00:00 2026-06-07T06:04:38+00:00

I have a class Data<T> with a generic attribute private T value; is there

  • 0

I have a class Data<T>

with a generic attribute

private T value;

is there nicer way to do the following?
ie returning the generic type in different forms?

public List<String> getValues() {
    if (value.getClass() != ArrayList.class)
        throw new Exception("Wrong Enum value '%s'", value);
    return (ArrayList<String>) 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;
}

Precision, I’m using Gson as deserializer, to get a List, each Data objects can then be heterogeous
Could also register adapters for float and long detection, but it wouldn’t be faster or nicer

edit: gson fails to retrieve longs:

either:

 ((Long) d.getValue())

java.lang.Double cannot be cast to java.lang.Long

or

Long.parseLong( d.getValue().toString())

java.lang.NumberFormatException: For input string: “212231.0”

I tried to register a LongAdpater

gsonBuilder.registerTypeAdapter(Long.class, new LongAdapter());

private static class LongAdapter implements 
    JsonSerializer<Long>, JsonDeserializer<Long> 
{

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

java.lang.IllegalArgumentException: Cannot register type adapters for class java.lang.Long

edit2 for tsOverflow:

Data<Float> d1 = new Data<Float>( new Float(6.32));
List<String> l = new ArrayList<String>();
    l.add("fr");
    l.add("it");
    l.add("en");
Data<List<String>> d2 = new Data<List<String>>( l);
Data<Long> d3 = new Data<Long>(new Long(212231));

List<Data> data = new ArrayList<Data>();
    data.add(d1);
    data.add(d2);
    data.add(d3)

new Gson().toJson(data);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:04:39+00:00Added an answer on June 7, 2026 at 6:04 am

    The point of generics is NOT to allow a class to use different types at the same time.

    Generics allow you to define/restrict the type used by an instance of an object.

    The idea behind generics is to eliminate the need to cast.

    Using generics with your class should result in something like this:

    Data<String> stringData = new Data<String>();
    String someString = stringData.getValue();
    
    Data<Long> longData = new Data<Long>();
    Long someLong = longData.getValue();
    
    Data<List<String>> listData = new Data<List<String>>();
    List<String> someList = listData.getValue();
    

    You should either use Objects and casting —OR— use generics to avoid casting.

    You seem to believe that generics allow for heterogeneous typing within the same instance.

    That is not correct.

    If you want a list to contain a mixed bag of types, then generics are not appropriate.


    Also…

    To create a long from a double, use Double.longValue().

    To create a float from a double, use Double.floatValue().

    I recommend reading the documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Data class that subclasses NSManagedObject with a single bool property attribute.
I have following controller: def personalization @title = t generic.forms.personalization end def update_personalization begin
I have 2 classes. With the Generic Data Access class I can get the
I have a class which contains a generic dictionary: protected Dictionary<K,T> Data { get;
I have class Fruit with data and logic. Now I need a lot of
Suppose I have a class named Data . Another class annotates one of its
Suppose I have a class that processes some data: class SomeClass { public: void
I have a data class that is serialized with the DataContractSerializer . The class
I have a Data Class called MyTable, and the source property is TABLE (table
I have a Data class with one field, price. I referenced the price field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.