Casting Function Return Type Based on Class Field

Coat

I've got a class that looks something like.

public class ParseValue {
  public String value;
  public final Class classType;
}

And I'd like to make a function that does a conversion and returns a casted value.

public T parseValue(ParseValue parseInfo) {
  if(parseInfo.classType == String.class) {
    return parseInfo.value;
  } else if (parseInfo.classType == Double.class) {
    return Double.valueOf(parseInfo.value);
  }
}

Right now I can have this function return an Object and then cast it upon getting the result, but is there a way to make the function do the cast based on the input ParseValue's classType field?

shmosel

The safest way to do it is to make ParseValue generic:

public class ParseValue<T> {
    public String value;
    public final Class<T> classType;

    public T parseValue() {
        Object result;
        if (classType == String.class) {
            result = value;
        } else if (classType == Double.class) {
            result = Double.valueOf(value);
        } else {
            throw new RuntimeException("unknown value type");
        }
        return classType.cast(result);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C# Casting a Class implementing interface to Class based on type

From Dev

Return delegate function based on type

From Dev

return type for class function typescript

From Dev

Type casting to template return type

From Java

Infer return type of function based on type guard

From Dev

type casting the arguments of a function

From Dev

Varying the return type of a templated member function of a class based off of a separate templated member

From Dev

Java generic bi-function to return value based on specified class type

From Dev

Varying the return type of a templated member function of a class based off of a separate templated member

From Java

TypeScript function return type based on input parameter

From Dev

AngularJS: Return function based on input type

From Dev

Casting string to userdefined class type

From Dev

Type Casting of class functions in PHP

From Dev

Swift: how to return class type from function

From Dev

Return type of function call on template argument class

From Dev

C++ class template as function return type

From Dev

Type casting in a generic swift function

From Dev

Type casting in a generic swift function

From Dev

Typescript: Constrain function generic type based on the expected return type

From Dev

Class and generic type casting to use in parameterised type

From Dev

Mapstruct : abstract target class and concrete type based on discriminator field

From Dev

Typescript: function return object type with field from parameter

From Dev

Typescript: function return object type with field from parameter

From Java

How to obtain the return type of a function passed into a templated function or class?

From Dev

Deduce member function return type using a class member function object

From Dev

How to let the return value remain generic (without casting) when a method of a class with a bounded type parameter returns a value of its own type?

From Dev

Casting List<String> to a String return type

From Dev

typescript casting return from database to class

From Dev

Choose return class type based on parameter with Java 7

Related Related

  1. 1

    C# Casting a Class implementing interface to Class based on type

  2. 2

    Return delegate function based on type

  3. 3

    return type for class function typescript

  4. 4

    Type casting to template return type

  5. 5

    Infer return type of function based on type guard

  6. 6

    type casting the arguments of a function

  7. 7

    Varying the return type of a templated member function of a class based off of a separate templated member

  8. 8

    Java generic bi-function to return value based on specified class type

  9. 9

    Varying the return type of a templated member function of a class based off of a separate templated member

  10. 10

    TypeScript function return type based on input parameter

  11. 11

    AngularJS: Return function based on input type

  12. 12

    Casting string to userdefined class type

  13. 13

    Type Casting of class functions in PHP

  14. 14

    Swift: how to return class type from function

  15. 15

    Return type of function call on template argument class

  16. 16

    C++ class template as function return type

  17. 17

    Type casting in a generic swift function

  18. 18

    Type casting in a generic swift function

  19. 19

    Typescript: Constrain function generic type based on the expected return type

  20. 20

    Class and generic type casting to use in parameterised type

  21. 21

    Mapstruct : abstract target class and concrete type based on discriminator field

  22. 22

    Typescript: function return object type with field from parameter

  23. 23

    Typescript: function return object type with field from parameter

  24. 24

    How to obtain the return type of a function passed into a templated function or class?

  25. 25

    Deduce member function return type using a class member function object

  26. 26

    How to let the return value remain generic (without casting) when a method of a class with a bounded type parameter returns a value of its own type?

  27. 27

    Casting List<String> to a String return type

  28. 28

    typescript casting return from database to class

  29. 29

    Choose return class type based on parameter with Java 7

HotTag

Archive