• 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();
        }
    
    }
    

      

  • 相关阅读:
    给统计人讲python(3)模拟城市_数据分析
    非均匀时间序列按固定时间间隔求和
    随机训练样本的方法
    df.empty判断空df,timestamp推算n秒前的时间
    优雅地循环字典键值
    panel的dropna方法
    给统计人讲Python(4)_股票数据处理
    给统计人讲Python(2)_Pandas入门
    给统计人讲Python(1)_科学计算库-Numpy
    生成不重复随机数
  • 原文地址:https://www.cnblogs.com/zhengwenqiang/p/10560683.html
Copyright © 2020-2023  润新知