summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/test/java/org/simpleframework/http/socket/table/WebSocketTableRow.java
blob: bb8a6db5a74a3e748fff3f758b62f1c5730fad56 (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
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
package org.simpleframework.http.socket.table;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WebSocketTableRow {

   private final Map<String, WebSocketTableCell> cells;
   private final WebSocketValueEncoder encoder;
   private final WebSocketTableSchema schema;
   private final int index;
   
   public WebSocketTableRow(WebSocketTableSchema schema, int index) {
      this.cells = new ConcurrentHashMap<String, WebSocketTableCell>();
      this.encoder = new WebSocketValueEncoder();
      this.index = index;
      this.schema = schema;
   }
   
   public int getIndex(){
      return index;
   }
   
   public void setValue(String column, String value){      
      WebSocketTableCell cell = getValue(column);
      
      if(cell == null) {
         WebSocketTableCell newCell = new WebSocketTableCell(column, value);
         List<String> columns = schema.columnNames();
         boolean match = false;
         for(String name : columns) {
            if(name.equals(column)) {
               match = true;
            }
         }
         if(!match) {
            throw new IllegalStateException("Could not find " + column + " in schema");
         }
         cells.put(column, newCell);
      } else {
         String previous = cell.getValue();
         
         if(previous != null && !previous.equals(value)) {
            WebSocketTableCell replaceCell = new WebSocketTableCell(column, value);
            cells.put(column, replaceCell);
         }
      }
   }
   
   public WebSocketTableCell getValue(String column) {
      return cells.get(column);
   }
   
   public String calculateChange(long lastUpdateDone) {
      StringBuilder builder = new StringBuilder();
      builder.append(index);
      builder.append(":");
      String delim = "";
      int count = 0;
      List<String> columns = schema.columnNames();
      for(int i = 0; i < columns.size(); i++){
         String column = columns.get(i);
         WebSocketTableCell cell = cells.get(column);
         if(cell != null) {
            long cellChanged = cell.getTimeStamp();
            long difference = cellChanged - lastUpdateDone;           
            
            if(difference > 0) { // positive means change happened later than update
               builder.append(delim);
               builder.append(i);
               builder.append("=");
               builder.append(cell.getValue());
               count++;
               delim = ",";
            }
         }
      }
      if(count <= 0) {
         return "";
      }
      return builder.toString();
   }
}