001package net.gdface.cli;
002
003import java.util.HashMap;
004import java.util.Map;
005
006/**
007 * 上下文数据管理类
008 * @author guyadong
009 *
010 */
011public class Context {
012        public static final class Builder {
013                private final Map<String, Object> map = new HashMap<String, Object>();
014
015                private Builder() {
016                }
017
018                public Builder addProperties(Map<String, ? extends Object> properties) {
019                        if (properties != null){
020                                map.putAll(properties);
021                        }
022                        return this;
023                }
024
025                public <T> Builder addProperty(String name, T property) {
026                        map.put(name, property);
027                        return this;
028                }
029
030                public Context build() {
031                        return new Context(map);
032                }
033                public <T extends Context> T build(Class<T> clazz) {
034                        if(null == clazz){
035                                throw new NullPointerException("clazz must not be null");
036                        }
037                        T instance;
038                        try {
039                                instance = clazz.newInstance();
040                        } catch (Exception e) {
041                                throw new RuntimeException(e);
042                        }
043                        instance.setProperties(map);
044                        return instance;
045                }
046        }
047
048        public static final Builder builder() {
049                return new Builder();
050        }
051
052        private Map<String, Object> context = new HashMap<String, Object>();
053
054        public Context() {
055        }
056
057        public Context(Map<String, Object> map) {
058                context.putAll(map);
059        }
060        public Context(Context context) {
061                this.context.putAll(context.getContext());
062        }
063        /**
064         * @return map
065         */
066        public Map<String, Object> getContext() {
067                return context;
068        }
069
070        /**
071         * @param name
072         * @return
073         * @see java.util.Map#get(java.lang.Object)
074         */
075        @SuppressWarnings("unchecked")
076        public <T> T getProperty(String name) {
077                return (T) context.get(name);
078        }
079
080        /**
081         * @param name
082         * @return
083         * @see #getProperty(String)
084         */
085        public boolean hasProperty(String name) {
086                return getProperty(name) != null;
087        }
088
089        /**
090         * @param properties
091         * @see java.util.Map#putAll(java.util.Map)
092         */
093        public void setProperties(Map<String, ? extends Object> properties) {
094                context.putAll(properties);
095        }
096
097        /**
098         * @param name
099         * @param property
100         * @return
101         * @see java.util.Map#put(java.lang.Object, java.lang.Object)
102         */
103        @SuppressWarnings("unchecked")
104        public <T> T setProperty(String name, T property) {
105                return (T) context.put(name, property);
106        }
107
108}