Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.SharedPreferences;

public class Main {
    /**
     * Gets an enum value in the SharedPreference.
     *
     * @param sharedPreference a {@link SharedPreferences} to be loaded.
     * @param key a key name
     * @param type a class of enum value
     * @param defaultValue default value if the {@link SharedPreferences} doesn't have corresponding
     *        entry.
     * @param conversionRecoveryValue default value if unknown value is stored.
     *        For example, if the value is "ALPHA" and {@code type} doesn't have "ALPHA" entry,
     *        this argument is returned.
     */
    public static <T extends Enum<T>> T getEnum(SharedPreferences sharedPreference, String key, Class<T> type,
            T defaultValue, T conversionRecoveryValue) {
        if (sharedPreference == null) {
            return defaultValue;
        }
        String name = sharedPreference.getString(key, null);
        if (name != null) {
            try {
                return Enum.valueOf(type, name);
            } catch (IllegalArgumentException e) {
                return conversionRecoveryValue;
            }
        }
        return defaultValue;
    }

    /**
     * Same as {@link getEnum}.
     *
     * {@code defaultValue} is used as {@code conversionRecoveryValue}
     */
    @SuppressWarnings("javadoc")
    public static <T extends Enum<T>> T getEnum(SharedPreferences sharedPreference, String key, Class<T> type,
            T defaultValue) {
        return getEnum(sharedPreference, key, type, defaultValue, defaultValue);
    }
}