P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
121
122
123
124
125
126
package foundation.io.object;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import foundation.json.IJSONProvider;
import foundation.json.IJSONWriter;
import foundation.util.ContentBuilder;
 
public class Titles implements Iterable<String>, IJSONProvider {
 
    private List<String> titleList;
    private Map<String, Integer> titleIndexMap;
    public static Pattern PATTNER_NECESSARY = Pattern.compile("[^(|\\(必填\\)|)]+");
    
    
    public Titles() {
        titleList = new ArrayList<String>();
        titleIndexMap = new HashMap<String, Integer>();
    }
 
    public void setSize(int columnCount) {
        // TODO Auto-generated method stub
        
    }
 
    public void setTitle(int i, String title) {
        // TODO Auto-generated method stub
        
    }
 
    public void setFieldName(int i, String value) {
        // TODO Auto-generated method stub
        
    }
    
    public void add(String title) {
        add(title, titleList.size());
    }
 
    public void add(String title, Integer idx) {
        if (title == null) {
            return;
        }
        title = title.toLowerCase();
        
        Matcher titleMatch = PATTNER_NECESSARY.matcher(title);
        
        if(titleMatch.find()) {
            title = titleMatch.group();
        }     
        titleList.add(title);
        titleIndexMap.put(title, idx);
    }
    
    public Integer getIndex(String title) {
        if (title == null) {
            return null;
        }
        
        title = title.toLowerCase();
        return titleIndexMap.get(title);
    }
    
 
    public boolean contains(String title) {
        if (title == null) {
            return false;
        }
        
        title = title.toLowerCase();
        return titleIndexMap.containsKey(title);
    }
 
    @Override
    public Iterator<String> iterator() {
        return titleList.iterator();
    }
 
    public int size() {
        return titleList.size();
    }
 
    public boolean isEmpty() {
        return titleList.size() == 0;
    }
 
    public String get(int i) {
        return titleList.get(i);
    }
 
    public void clear() {
        titleList.clear();
        titleIndexMap.clear();
    }
 
    @Override
    public String toString() {
        ContentBuilder builder = new ContentBuilder(", ");
        
        for (String title: titleList) {
            builder.append(title);
        }
        
        return builder.toString();
    }
 
    @Override
    public void writeJSON(IJSONWriter writer) {
        writer.beginArray();
        writeJSONBody(writer);
        writer.endArray();
    }    
 
    public void writeJSONBody(IJSONWriter writer) {
        for (String title: titleList) {
            writer.write(title);
        }
    }
 
}