View Javadoc
1   /**
2    *    Copyright 2009-2018 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.reflection.wrapper;
17  
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.ibatis.reflection.MetaObject;
23  import org.apache.ibatis.reflection.SystemMetaObject;
24  import org.apache.ibatis.reflection.factory.ObjectFactory;
25  import org.apache.ibatis.reflection.property.PropertyTokenizer;
26  
27  /**
28   * @author Clinton Begin
29   */
30  public class MapWrapper extends BaseWrapper {
31  
32    private final Map<String, Object> map;
33  
34    public MapWrapper(MetaObject metaObject, Map<String, Object> map) {
35      super(metaObject);
36      this.map = map;
37    }
38  
39    @Override
40    public Object get(PropertyTokenizer prop) {
41      if (prop.getIndex() != null) {
42        Object collection = resolveCollection(prop, map);
43        return getCollectionValue(prop, collection);
44      } else {
45        return map.get(prop.getName());
46      }
47    }
48  
49    @Override
50    public void set(PropertyTokenizer prop, Object value) {
51      if (prop.getIndex() != null) {
52        Object collection = resolveCollection(prop, map);
53        setCollectionValue(prop, collection, value);
54      } else {
55        map.put(prop.getName(), value);
56      }
57    }
58  
59    @Override
60    public String findProperty(String name, boolean useCamelCaseMapping) {
61      return name;
62    }
63  
64    @Override
65    public String[] getGetterNames() {
66      return map.keySet().toArray(new String[map.keySet().size()]);
67    }
68  
69    @Override
70    public String[] getSetterNames() {
71      return map.keySet().toArray(new String[map.keySet().size()]);
72    }
73  
74    @Override
75    public Class<?> getSetterType(String name) {
76      PropertyTokenizerrty/PropertyTokenizer.html#PropertyTokenizer">PropertyTokenizer prop = new PropertyTokenizer(name);
77      if (prop.hasNext()) {
78        MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
79        if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
80          return Object.class;
81        } else {
82          return metaValue.getSetterType(prop.getChildren());
83        }
84      } else {
85        if (map.get(name) != null) {
86          return map.get(name).getClass();
87        } else {
88          return Object.class;
89        }
90      }
91    }
92  
93    @Override
94    public Class<?> getGetterType(String name) {
95      PropertyTokenizerrty/PropertyTokenizer.html#PropertyTokenizer">PropertyTokenizer prop = new PropertyTokenizer(name);
96      if (prop.hasNext()) {
97        MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
98        if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
99          return Object.class;
100       } else {
101         return metaValue.getGetterType(prop.getChildren());
102       }
103     } else {
104       if (map.get(name) != null) {
105         return map.get(name).getClass();
106       } else {
107         return Object.class;
108       }
109     }
110   }
111 
112   @Override
113   public boolean hasSetter(String name) {
114     return true;
115   }
116 
117   @Override
118   public boolean hasGetter(String name) {
119     PropertyTokenizerrty/PropertyTokenizer.html#PropertyTokenizer">PropertyTokenizer prop = new PropertyTokenizer(name);
120     if (prop.hasNext()) {
121       if (map.containsKey(prop.getIndexedName())) {
122         MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
123         if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
124           return true;
125         } else {
126           return metaValue.hasGetter(prop.getChildren());
127         }
128       } else {
129         return false;
130       }
131     } else {
132       return map.containsKey(prop.getName());
133     }
134   }
135 
136   @Override
137   public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
138     HashMap<String, Object> map = new HashMap<>();
139     set(prop, map);
140     return MetaObject.forObject(map, metaObject.getObjectFactory(), metaObject.getObjectWrapperFactory(), metaObject.getReflectorFactory());
141   }
142 
143   @Override
144   public boolean isCollection() {
145     return false;
146   }
147 
148   @Override
149   public void add(Object element) {
150     throw new UnsupportedOperationException();
151   }
152 
153   @Override
154   public <E> void addAll(List<E> element) {
155     throw new UnsupportedOperationException();
156   }
157 
158 }