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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package foundation.util;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
 
 
public class MapList<K, T> implements Iterable<T> {
 
    protected List<T> itemList;
    protected Map<K, T> itemMap;
    protected Map<K, Integer> indexMap;
    protected boolean ignoreCase;
    
    
    public MapList() {
        itemList = new ArrayList<T>();
        itemMap = new HashMap<K, T>();
        ignoreCase = true;
    }
    
    public MapList(int size) {
        itemList = new ArrayList<T>(size);
        itemMap = new HashMap<K, T>();
        ignoreCase = true;
    }
    
    public MapList(boolean ignoreCase) {
        this();
        this.ignoreCase = ignoreCase;
    }
    
    public void addAll(Map<K, T> map) {
        Set<K> keys = map.keySet();
        
        for (K key: keys) {
            add(key, map.get(key));
        }
    }
    
    @SuppressWarnings("unchecked")
    public void add(K key, T item) {
        if (key == null) {
            return;
        }
        
        if (ignoreCase && (key instanceof String)) {
            key = (K)((String)key).toLowerCase();
        }
        
        T old = itemMap.get(key);
        
        if (old != null) {
            itemList.remove(old);
        }
        
        itemMap.put(key, item);
        itemList.add(item);
    }
 
    @SuppressWarnings("unchecked")
    public void set(K key, T item) {
        if (key == null) {
            return;
        }
        
        if (indexMap == null) {
            indexMap = new HashMap<K, Integer>();
        }
        
        if (ignoreCase) {
            key = (K)((String)key).toLowerCase();
        }
        
        if (!itemMap.containsKey(key)) {
            itemMap.put(key, item);
            indexMap.put(key, itemList.size());
            itemList.add(item);
        }
        else {
            Integer idx = indexMap.get(key);
            itemList.set(idx, item);
        }
    }
    
    @SuppressWarnings("unchecked")
    public T get(K key) {
        if (key == null) {
            return null;
        }
        
        if (ignoreCase && (key instanceof String)) {
            key = (K)((String)key).toLowerCase();
        }
        
        return itemMap.get(key);
    }
    
    public T get(int idx) {
        return itemList.get(idx);
    }
    
    @SuppressWarnings("unchecked")
    public T remove(K key) {
        if (key == null) {
            return null;
        }
        
        if (ignoreCase && (key instanceof String)) {
            key = (K)((String)key).toLowerCase();
        }
        
        T obj = itemMap.get(key);
        
        if (obj != null) {
            itemMap.remove(key);
            itemList.remove(obj);
        }
        
        return obj;
    }
 
    @SuppressWarnings("unchecked")
    public void addBeforeLast(K key, T item) {
        if (key == null) {
            return;
        }
        
        if (ignoreCase && (key instanceof String)) {
            key = (K)((String)key).toLowerCase();
        }
        
        if (itemList.isEmpty()) {
            add(key, item);
        }
        
        itemMap.put(key, item);
        
        T last = itemList.remove(itemList.size() - 1);
        itemList.add(item);
        itemList.add(last);
    }
    
    public List<T> getItemList() {
        return itemList;
    }
 
    public void setList(List<T> list) {
        itemList = list;
    }
 
    @SuppressWarnings("unchecked")
    public void sortList(Comparator<T> comparator) {
        int max = itemList.size();
        T[] array = (T[]) new Object[max];
        
        for (int i = 0; i < max; i++) {
             array[i] = itemList.get(i);
        }
        
        Arrays.sort(array, comparator);
    
        List<T> result = Arrays.asList(array);
        itemList = result;
    }
    
    public Map<K, T> getItemMap() {
        return itemMap;
    }
    
    public Set<K> getKeySet() {
        return itemMap.keySet();
    }    
 
    public boolean isEmpty() {
        return itemList.isEmpty();
    }
    
    public int size() {
        return itemList.size();
    }
    
    public int mapSize() {
        return itemMap.size();
    }
    
    public Iterator<T> iterator() {
        return itemList.iterator();
    }
 
    public void clear() {
        itemList.clear();
        itemMap.clear();
    }
 
    public String toString(String separator) {
        ContentBuilder builder = new ContentBuilder(separator);
        
        for (Object key: itemMap.keySet()) {
            builder.append(String.valueOf(key));
        }
        
        return builder.toString();
    }
 
    public String mapToString(String separator) {
        ContentBuilder builder = new ContentBuilder(separator);
        Set<Entry<K, T>> entrySet = itemMap.entrySet();
        
        for (Entry<K, T> entry: entrySet) {
            String key = String.valueOf(entry.getKey());
            String value = String.valueOf(entry.getValue());
            
            if (value.startsWith("{") || value.startsWith("[")) {
                builder.append( Util.doubleQuotedStr(key) + ":" + value);
            }
            else {
                builder.append( Util.doubleQuotedStr(key) + ":" + Util.doubleQuotedStr(value));
            }
            
        }
        
        return "{" + builder.toString() + "}";
    }
    
    public Set<K> keySet() {
        return itemMap.keySet();
    }
 
    public boolean contains(Object key) {
        if (key == null) {
            return false;
        }
        
        if (ignoreCase && (key instanceof String)) {
            key = ((String)key).toLowerCase();
        }
        
        return itemMap.containsKey(key);
    }
 
    public Object[] toArray() {
        return itemList.toArray();
    }
 
    @SuppressWarnings("hiding")
    public <T> T[] toArray(T[] a) {
        return itemList.toArray(a);
    }
    
    @SuppressWarnings("unchecked")
    public void sort(Comparator<Object> comparator) {
        Object[] array = new Object[itemList.size()];
        itemList.toArray(array);
        Arrays.sort(array, comparator);
        
        itemList.clear();
        
        for (Object obj: array) {
            itemList.add((T) obj);
        }
    }
 
}