blob: 5a77537d8d2d552b1c72a28b148d29a8b375450e (
plain)
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
|
package org.simpleframework.http.socket.table;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class WebSocketTableChanger {
private final Map<String, Integer> currentRows;
private final WebSocketValueEncoder encoder;
private final WebSocketTable table;
public WebSocketTableChanger(WebSocketTable table) {
this.currentRows = new ConcurrentHashMap<String, Integer>();
this.encoder = new WebSocketValueEncoder();
this.table = table;
}
public void onChange(Map<String, Object> values) {
Map<String, String> row = new HashMap<String, String>();
Map<String, String> header = new HashMap<String, String>();
Set<String> columns = values.keySet();
for (String column : columns) {
Object value = values.get(column);
String encoded = encoder.encode(value);
row.put(column, encoded);
header.put(column, column);
}
WebSocketTableRow headerRow = table.getRow(0);
if (headerRow == null) {
table.addRow(header);
} else {
for (String column : columns) {
String name = header.get(column);
headerRow.setValue(column, name);
}
}
String key = table.getKey();
Object keyAttribute = values.get(key);
if (keyAttribute != null) {
String tableKey = String.valueOf(keyAttribute);
Integer index = currentRows.get(tableKey);
if (index == null) {
WebSocketTableRow newRow = table.addRow(row);
index = newRow.getIndex();
currentRows.put(tableKey, index);
} else {
table.updateRow(index, row);
}
}
}
}
|