How do I cast from int to generic type Integer?

前提是你 提交于 2020-01-11 13:24:07

问题


I'm relatively new to Java and am used to generics in C# so have struggled a bit with this code. Basically I want a generic method for getting a stored Android preference by key and this code, albeit ugly, works for a Boolean but not an Integer, when it blows up with a ClassCastException. Can anyone tell me why this is wrong and maybe help me improve the whole routine (using wildcards?)?

    public static <T> T getPreference(Class<T> argType, String prefKey, T defaultValue,
        SharedPreferences sharedPreferences) {

    ...

    try {
        if (argType == Boolean.class) {
            Boolean def = (Boolean) defaultValue;
            return argType.cast(sharedPreferences.getBoolean(prefKey, def));
        } else if (argType == Integer.class) {
            Integer def = (Integer) defaultValue;
            return argType.cast(new Integer(sharedPreferences.getInt(prefKey, def)));
        } else {
            AppGlobal.logWarning("getPreference: Unknown type '%s' for preference '%s'. Returning default value.",
                    argType.getName(), prefKey);
            return defaultValue;
        }
    } catch (ClassCastException e) {
        AppGlobal.logError("Cast exception when reading pref %s. Using default value.", prefKey);
        return defaultValue;
    }
}

My calling code is:

        mAccuracy = GlobalPreferences.getPreference(Integer.class, prefKey, mAccuracy, sharedPreferences);

Here is the Android code for getInt():

public int getInt(String key, int defValue) {
        synchronized (this) {
            Integer v = (Integer)mMap.get(key);
            return v != null ? v : defValue;
        }
    }

I've tried various ways - using the native int, casting to an Integer, but nothing works.


回答1:


May I suggest:

Integer i = new Integer(42);



回答2:


It turns out the preference I'm trying to read is stored as a string so the cast exception is coming from inside the Android code not mine. Thanks for your help. But as I am a Java-newbie, if you think there is anything generally wrong with my routine, please teach me a better way.




回答3:


Try defining a bunch of functions with the same name that take a different type for the default, and let the compiler figure it out. Java really ties your hands when working with types, especially primitive types.

public function int getPreference( String key , int missing ) { return sharedPreferences.getInt( key , missing ); }
public function boolean getPreference( String key , boolean missing ) { return sharedPreferences.getBoolean( key , missing ); }
public function String getPreference( String key , String missing ) { return sharedPreferences.getString( key , missing ); }

Edit:

If you are trying to get an object (not primitive) regardless of the type, you can use:

public function Object getPreference( String key , Object missing ) { return sharedpreferences.contains( key ) ? sharedPreferences.getAll().get( key ) : missing; }

If you are trying to get a specific type like int regardless of what is stored, you can use:

public function int getPreference( String key , int missing ) {
    int result = missing;
    Object value = sharedpreferences.contains( key ) ? sharedPreferences.getAll().get( key ) : null;
    if ( value instanceof Integer ) result = ((Integer)value).intValue();
    if ( value instanceof Boolean ) result = ((Boolean)value).booleanValue() ? 1 : 0;
    // repeat for every other primitive wrapper type you care about
    return result;
}

If you are trying to get a result only if it is a certain type, you can use something like:

public function Object getPreference( Class inRequire , String key , Object missing ) {
    Object value = sharedpreferences.contains( key ) ? sharedPreferences.getAll().get( key ) : null;
    if ( !( value instanceof inRequire ) ) {
        value = null;
    }
    return ( value == null ) ? missing : value;
}


来源:https://stackoverflow.com/questions/2705091/how-do-i-cast-from-int-to-generic-type-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!