zoukankan      html  css  js  c++  java
  • SharedPreference映射Java类

    package com.overlook.weagree.util;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    
    import com.overlook.weagree.entity.CurrentUser;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.HashMap;
    import java.util.Locale;
    import java.util.Map;
    
    
    public class CurrentUserUtil {
    
        private static final String TAG = "CurrentUserUtil";
    
        private static final String CURRENT_USER = "current-user";
    
        private static final Map<String,Method> SETTER = new HashMap<>();
    
        private static final Map<String,Method> GETTER = new HashMap<>();
    
        static {
            if (SETTER.isEmpty() || GETTER.isEmpty()) {
                Method[] methods = CurrentUser.class.getDeclaredMethods();
                for (Method method : methods) {
                    String methodName = method.getName().toLowerCase(Locale.ENGLISH);
                    if (methodName.startsWith("set")) {
                        SETTER.put(methodName.substring(3),method);
                    }else{
                        GETTER.put(methodName.substring(3),method);
                    }
                }
            }
        }
    
        public static void exit(Activity activity){
            SharedPreferences sharedPref = activity.getSharedPreferences(CURRENT_USER,Context.MODE_PRIVATE);
            if ( sharedPref == null ) {
                return;
            }
            sharedPref.edit().clear().commit();
        }
    
        public static CurrentUser getCurrentUser(Activity activity,Class mClass){
    
            SharedPreferences sharedPref = activity.getSharedPreferences(CURRENT_USER,Context.MODE_PRIVATE);
            if ( sharedPref == null ) {
                return null;
            }
            CurrentUser currentUser = new CurrentUser();
    
            Field[] fields = CurrentUser.class.getDeclaredFields();
    
            for (Field field : fields) {
                String fieldName = field.getName().toLowerCase(Locale.ENGLISH);;
                Class type = field.getType();
                Object value;
                if (type.isAssignableFrom(Long.class)) {
                    value = sharedPref.getLong(fieldName,0L);
                }else if (type.isAssignableFrom(Boolean.class)){
                    value = sharedPref.getBoolean(fieldName,false);
                }else if (type.isAssignableFrom(Integer.class)) {
                    value = sharedPref.getInt(fieldName,0);
                }else{
                    value = sharedPref.getString(fieldName,null);
                }
    
                Method method = SETTER.get(fieldName);
    
                try {
                    if ( method == null ) {
                        Log.d(TAG, "getCurrentUser: "+fieldName);
                    }else{
                        method.invoke(currentUser,value);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
    
            return currentUser;
        }
    
        public static Boolean putCurrentUser(Activity activity,CurrentUser currentUser){
            SharedPreferences sharedPref = activity.getSharedPreferences(CURRENT_USER,Context.MODE_PRIVATE);
    
            Field[] fields = CurrentUser.class.getDeclaredFields();
    
            SharedPreferences.Editor editor = sharedPref.edit();
    
            for (Field field : fields) {
                String fieldName = field.getName().toLowerCase(Locale.ENGLISH);
                Class type = field.getType();
                Method method = GETTER.get(fieldName);
    
                try {
                    Object value = method.invoke(currentUser);
    
                    if ( value == null ) {
                        continue;
                    }
    
                    if (type.isAssignableFrom(Long.class)) {
                        Long longVal = (Long) value;
                        if ( ! longVal.equals(0L)){
                            editor.putLong(fieldName,(Long)value);
                        }
                    }else if (type.isAssignableFrom(Boolean.class)){
                        editor.putBoolean(fieldName,(Boolean)value);
                    }else if (type.isAssignableFrom(Integer.class)) {
                        Integer intVal = (Integer)value;
                        if ( ! intVal.equals(0)){
                            editor.putInt(fieldName,(Integer)value);
                        }
                    }else{
                        editor.putString(fieldName,(String)value);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
            return editor.commit();
        }
    
    }
    

      

  • 相关阅读:
    leetcode — interleaving-string
    leetcode — unique-binary-search-trees-ii
    leetcode — unique-binary-search-trees
    leetcode — binary-tree-inorder-traversal
    leetcode — restore-ip-addresses
    poj 2774 Long Long Message
    bzoj 1031 [JSOI2007]字符加密Cipher
    BZOJ4554 HEOI2016游戏
    BZOJ4552 HEOI2016排序
    BZOJ4551 HEOI2016树
  • 原文地址:https://www.cnblogs.com/zhengwenqiang/p/10560683.html
Copyright © 2011-2022 走看看