• 利用Map创建Copy Object


    定义上下文行为,要继承Map

    package com.wjz.core;
    
    import java.io.Serializable;
    import java.util.Map;
    
    public interface CustomSubjectContext extends Map<String, Object> {
    
        void setSessionId(Serializable sessionId);
        
        Serializable getSessionId();
        
        void setAuthenticated(Boolean authc);
        
        Boolean isAuthenticated();
    }

    定义MapContext上下文,定义备份Map,实现备份Map的存取等关乎map的行为,实现Map

    package com.wjz.core;
    
    import java.io.Serializable;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    import org.springframework.util.CollectionUtils;
    
    public class CustomMapContext implements Map<String, Object>, Serializable {
    
        private static final long serialVersionUID = 6611136447999868186L;
        
        private final Map<String, Object> backingMap;
        
        public CustomMapContext() {
            backingMap = new HashMap<String, Object>();
        }
        
        public CustomMapContext(Map<String, Object> map) {
            this();
            if (!CollectionUtils.isEmpty(map)) {
                this.backingMap.putAll(map);
            }
        }
    
        @Override
        public int size() {
            return backingMap.size();
        }
    
        @Override
        public boolean isEmpty() {
            return backingMap.isEmpty();
        }
    
        @Override
        public boolean containsKey(Object key) {
            return backingMap.containsKey(key);
        }
    
        @Override
        public boolean containsValue(Object value) {
            return backingMap.containsValue(value);
        }
    
        @Override
        public Object get(Object key) {
            return backingMap.get(key);
        }
    
        @Override
        public Object put(String key, Object value) {
            return backingMap.put(key, value);
        }
    
        @Override
        public Object remove(Object key) {
            return backingMap.remove(key);
        }
    
        @Override
        public void putAll(Map<? extends String, ?> m) {
            backingMap.putAll(m);
        }
    
        @Override
        public void clear() {
            backingMap.clear();
        }
    
        @Override
        public Set<String> keySet() {
            return Collections.unmodifiableSet(backingMap.keySet());
        }
    
        @Override
        public Collection<Object> values() {
            return Collections.unmodifiableCollection(backingMap.values());
        }
    
        @Override
        public Set<Entry<String, Object>> entrySet() {
            return Collections.unmodifiableSet(backingMap.entrySet());
        }
        
        protected void nullSafePut(String key, Object value) {
            if (value != null) {
                put(key, value);
            }
        }
        
        @SuppressWarnings("unchecked")
        protected <E> E getTypeValue(String key, Class<E> type) {
            E val = null;
            Object result = backingMap.get(key);
            if (result != null) {
                if (!type.isAssignableFrom(result.getClass())) {
                    // 类型不正确
                }
                val = (E) result;
            }
            return val;
        }
    
    }

    定义默认的上下文实现类,继承MapContext上下文,具体实现上下文

    package com.wjz.core;
    
    import java.io.Serializable;
    
    public class DefaultCustomSubjectContext extends CustomMapContext implements CustomSubjectContext {
    
        private static final long serialVersionUID = -273450905542274017L;
        
        private static final String SESSION_ID = ".SESSION_ID";
        
        private static final String AUTHC = ".AUTHC";
    
        public DefaultCustomSubjectContext() {
            super();
        }
        
        public DefaultCustomSubjectContext(CustomSubjectContext sctx) {
            super(sctx);
        }
        
        @Override
        public void setSessionId(Serializable sessionId) {
            nullSafePut(SESSION_ID, sessionId);
        }
    
        @Override
        public Serializable getSessionId() {
            return getTypeValue(SESSION_ID, Serializable.class);
        }
    
        @Override
        public void setAuthenticated(Boolean authc) {
            nullSafePut(AUTHC, authc);
        }
    
        @Override
        public Boolean isAuthenticated() {
            return getTypeValue(AUTHC, Boolean.class);
        }
    
    }

    demo

    package com.wjz.demo;
    
    import com.wjz.core.CustomSubjectContext;
    import com.wjz.core.DefaultCustomSubjectContext;
    
    public class CopyDemo {
    
        public static void main(String[] args) {
            CustomSubjectContext sctx = new DefaultCustomSubjectContext();
            sctx.setSessionId("sctx 123456");
            sctx.setAuthenticated(true);
            System.out.println("sctx getSessionId :" + sctx.getSessionId());
            System.out.println("sctx isAuthenticated :" + sctx.isAuthenticated());
            System.out.println();
            CustomSubjectContext copy = new DefaultCustomSubjectContext(sctx);
            copy.setSessionId("copy 123456");
            System.out.println("copy getSessionId :" + copy.getSessionId());
            System.out.println("copy isAuthenticated :" + copy.isAuthenticated());
        }
    }
  • 相关阅读:
    VUE脚手架,babel转码 常用命令
    ES6那些事半功倍的新特性(一)
    vuex中怎么把‘库’中的状态对象赋值给内部对象(三种方法)
    VUE自定义指令生命周期,VUE生命周期
    判断是数组还是对象的方法
    利用cordova打包H5混合app
    webstorm皮肤外观样式快速设置,CMD的使用方法,webstorm11激活方法
    数组操作方法中的splice()和concat() 以及slice()
    Angularjs跨域
    Node.js的基础知识(一)
  • 原文地址:https://www.cnblogs.com/BINGJJFLY/p/9101989.html
Copyright © 2020-2023  润新知