简体   繁体   中英

How do I cast from int to generic type Integer?

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.

我可以建议:

Integer i = new Integer(42);

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM