java generics T extends Simpletype?

BennX

I'd like to write a method, that does return something of a PrimitiveType like float, integer, boolean and also String if possible. I'd like to use generics for it but i stuck and dont find a solution for it. I do need it for a Configparser. Ill use it to get different values from the Config.

Current it des look like this and i know that the switch does not work like this but you get an idea of what id like to do:

public class ConfigurationManager extends XmlReader {
    private final static String FILE_PATH = "config/config.cfg";
    private static Element xml;

    public ConfigurationManager() throws IOException {
        FileHandle handle = Gdx.files.internal(FILE_PATH);
        this.xml = this.parse(handle);
    }

    public Resolution getResolution() {
        Resolution r = new Resolution();
        r.height = xml.getFloat("height");
        r.width = xml.getFloat("width");
        return r;
    }

    public static <T> T getConfig(Class<T> type, String name) {
        if (type.equals(Integer.class)) {
            return type.cast(xml.getInt(name));
        } else if (type.equals(Float.class)) {
            return type.cast(xml.getFloat(name));
        } else if (type.equals(Boolean.class)) {
            return type.cast(xml.getBoolean(name));
        } else if (type.equals(String.class)) {
            return type.cast(xml.get(name));
        }
        throw new AssertionError("Invalid type");
    }
}

Thanks alot

Edwin Dalorzo

Well, I don't think you can do it with primitive types directly, but how about something like this:

public static <T> T getConfig(Class<T> type, String name) {
   if(type.equals(Integer.class)){
    return type.cast(xml.getInteger(name));
   } else if(type.equals(Float.class)){
    return type.cast(xml.getFloat(name));
   } else if(type.equals(Double.class)) {
    return type.cast(xml.getDouble(name));
   } else if(type.equals(String.class)) {
    return type.cast(xml.getString(name));
   } 
   throw new AssertionError("Invalid type");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there a way in Java Generics to say "T extends Thing or T extends SomethingElse"?

From Dev

Java generics extends syntax

From Dev

Java: List initialization with Generics (T extends Enum<T> & AnyInterface)

From Dev

Java Generics compile error in line class HanoiStack<T extends Comparable<T>> extends Stack<T>

From Dev

Java Generics: Special usage <T extends Object & Interface>

From Dev

super and extends combined for generics in Java

From Dev

Java Generics - use of extends keyword

From Dev

correct java syntax for generics with extends

From Dev

Java Generics - use of extends keyword

From Dev

Generics in Java - Why I am not allowed to do Comparable<? extends T> instead of Comparable<? super T>?

From Dev

Java generics: <B extends BaseB> does not match <? extends BaseB>

From Dev

Java Collections Generics <? extends Employee> throwing exception

From Dev

Java Collections Generics <? extends Employee> throwing exception

From Dev

Java generics: cannot convert <X> into <? extends X>

From Dev

java, generics - how to parametrize a class with a type T which BOTH: extends and implements sth

From Dev

java, generics - how to parametrize a class with a type T which BOTH: extends and implements sth

From Dev

TypeScript: Class generics - something like Java's Class<T extends Foo>

From Dev

what the usefulness about Java generics involving inheritance and generics extends self

From Dev

Array of <? extends T> in Java

From Dev

circular generics : interface IFoo <T extends IFoo <T>>

From Dev

Generics, V extends T, no error even if incompatible types

From Dev

Java Generics - Cannot convert from <? extends MyObject> to <MyObject>

From Dev

Issue with using 'extends' and 'super' in java generics with generic methods

From Dev

Java Generics E extends Comparable<E> leaves warning

From Dev

Failing to use User object as parameter for ? extends User in Java generics

From Dev

Java reflection and generics - parameterized type info missing "extends"

From Dev

Failing to use User object as parameter for ? extends User in Java generics

From Dev

Java generics - method with "extends" typed collection parameter rejects valid argument?

From Dev

Use of extends keyword in generics

Related Related

  1. 1

    Is there a way in Java Generics to say "T extends Thing or T extends SomethingElse"?

  2. 2

    Java generics extends syntax

  3. 3

    Java: List initialization with Generics (T extends Enum<T> & AnyInterface)

  4. 4

    Java Generics compile error in line class HanoiStack<T extends Comparable<T>> extends Stack<T>

  5. 5

    Java Generics: Special usage <T extends Object & Interface>

  6. 6

    super and extends combined for generics in Java

  7. 7

    Java Generics - use of extends keyword

  8. 8

    correct java syntax for generics with extends

  9. 9

    Java Generics - use of extends keyword

  10. 10

    Generics in Java - Why I am not allowed to do Comparable<? extends T> instead of Comparable<? super T>?

  11. 11

    Java generics: <B extends BaseB> does not match <? extends BaseB>

  12. 12

    Java Collections Generics <? extends Employee> throwing exception

  13. 13

    Java Collections Generics <? extends Employee> throwing exception

  14. 14

    Java generics: cannot convert <X> into <? extends X>

  15. 15

    java, generics - how to parametrize a class with a type T which BOTH: extends and implements sth

  16. 16

    java, generics - how to parametrize a class with a type T which BOTH: extends and implements sth

  17. 17

    TypeScript: Class generics - something like Java's Class<T extends Foo>

  18. 18

    what the usefulness about Java generics involving inheritance and generics extends self

  19. 19

    Array of <? extends T> in Java

  20. 20

    circular generics : interface IFoo <T extends IFoo <T>>

  21. 21

    Generics, V extends T, no error even if incompatible types

  22. 22

    Java Generics - Cannot convert from <? extends MyObject> to <MyObject>

  23. 23

    Issue with using 'extends' and 'super' in java generics with generic methods

  24. 24

    Java Generics E extends Comparable<E> leaves warning

  25. 25

    Failing to use User object as parameter for ? extends User in Java generics

  26. 26

    Java reflection and generics - parameterized type info missing "extends"

  27. 27

    Failing to use User object as parameter for ? extends User in Java generics

  28. 28

    Java generics - method with "extends" typed collection parameter rejects valid argument?

  29. 29

    Use of extends keyword in generics

HotTag

Archive