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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package frame.object.reader;
 
import java.util.Collection;
import java.util.List;
 
import frame.util.ContentBuilder;
import frame.variant.translator.ITranslator;
import frame.variant.translator.Translator;
 
public class CollectionReader extends EntityReader {
 
    protected IEntityReader valueReader;
    protected ITranslator translator;
    
    public CollectionReader(Class<?> clazz) throws Exception {
        super(clazz);
    }
 
    @Override
    protected void initValueReader(Class<?> dataType) throws Exception {
        if (translator == null && valueReader == null) {
            if (Translator.containsType(dataType)) {
                translator = Translator.getInstance(dataType);
            }
            else {
                valueReader = EntityReader.getInstance(dataType);
            }
        }
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public String getString(Object entity) {
        Collection<Object> collection = (Collection<Object>) entity;
        
        if (collection.isEmpty()) {
            return "[]";
        }
        
        ContentBuilder result = new ContentBuilder();
        result.append("[");
        
        try {
            initValueReader(collection.iterator().next().getClass());
        } catch (Exception e) {
            return e.getMessage();
        }
        
        for (Object value: collection) {
            try {
                if (translator != null) {
                    result.append(translator.toString(value), ", ");
                }
                else {
                    result.append(valueReader.getString(value), ", ");
                }
            }
            catch (Exception e) {
                result.append("error", ",");
            }
        }
        
        result.append("]");
        return result.toString();
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public String getJSONString(Object entity) throws Exception {
        Collection<Object> collection = (Collection<Object>) entity;
        
        if (collection.isEmpty()) {
            return "[]";
        }
        
        ContentBuilder result = new ContentBuilder();
        result.append("[");
        
        try {
            Class<?> clazz = collection.iterator().next().getClass();
            initValueReader(clazz);
        } 
        catch (Exception e) {
            return e.getMessage();
        }
        
        for (Object value: collection) {
            try {
                if (translator != null) {
                    result.append(translator.toJSONString(value), ", ");
                }
                else {
                    result.append(valueReader.getJSONString(value), ", ");
                }
            }
            catch (Exception e) {
                result.append("error", ",");
            }
        }
        
        result.append("]");
        return result.toString();
    }
 
    @Override
    public String getJSONString(Object entity, String defaultValue) {
        try {
            return getJSONString(entity);
        }
        catch (Exception e) {
            return defaultValue;
        }
    }
 
    @Override
    public List<NameValuePair> getValueList(Object entity) throws Exception {
        return null;
    }
 
}