hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package frame.object.reader;
 
import java.util.HashMap;
import java.util.Map;
 
public class EntityReaderContainer {
 
    private static EntityReaderContainer instance;
    private Map<Class<?>, IEntityReader> items;
 
    private EntityReaderContainer() {
        items = new HashMap<Class<?>, IEntityReader>();
    }
 
    public static synchronized EntityReaderContainer getInstance() {
        if (instance == null) {
            instance = new EntityReaderContainer();
        }
 
        return instance;
    }
 
    public IEntityReader getEntityReader(Class<?> clazz) throws Exception {
 
        IEntityReader reader = items.get(clazz);
 
        if (reader == null) {
            reader = EntityReader.getInstance(clazz);
 
            // 保证集合类class、Map类class不在items中
            if (reader instanceof CollectionReader || reader instanceof MapReader) {
                return reader;
            }
            
            items.put(clazz, reader);
        }
 
        return reader;
    }
 
    public ObjectReader getObjectReader(Class<?> clazz) throws Exception {
        IEntityReader reader = items.get(clazz);
 
        if (reader == null) {
            reader = new ObjectReader(clazz);
            items.put(clazz, reader);
        }
 
        return (ObjectReader) reader;
    }
 
    public MapReader getMapReader(Class<?> clazz) throws Exception {
        IEntityReader reader = items.get(clazz);
 
        if (reader == null) {
            reader = new MapReader(clazz);
            items.put(clazz, reader);
        }
 
        return (MapReader) reader;
    }
 
    public CollectionReader getCollectionReader(Class<?> clazz) throws Exception {
        IEntityReader reader = items.get(clazz);
 
        if (reader == null) {
            reader = new CollectionReader(clazz);
            items.put(clazz, reader);
        }
 
        return (CollectionReader) reader;
    }
 
}