-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.java
More file actions
71 lines (57 loc) · 1.44 KB
/
Copy pathMessage.java
File metadata and controls
71 lines (57 loc) · 1.44 KB
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
package nbm.common;
import java.io.*;
import java.util.*;
public class Message {
private static final String DELIMETER = "-_-";
private static final char CHAR = 'S';
private long ts;
private String id;
private String body;
private long originalTs = 0; // read only, not serializable
public Message(String id, String body) {
this.ts = System.currentTimeMillis();
this.id = id;
this.body = body;
}
public Message(String id, int bodySize) {
this(id, getChunk(bodySize, CHAR));
}
public Message(String msg) { // parse from string
String[] result = msg.split(DELIMETER);
this.ts = System.currentTimeMillis();
this.originalTs = Long.parseLong(result[0]);
this.id = result[1];
this.body = result[2];
}
public long getTs() {
return this.ts;
}
public long getOriginalTs() {
return this.originalTs;
}
public String getId() {
return this.id;
}
public void getId(String id) {
this.id = id;
}
public String getBody() {
return this.body;
}
public void setBody(String body) {
this.body = body;
}
public String toString() {
String[] list = new String[3];
list[0] = String.valueOf(this.ts);
list[1] = this.id;
list[2] = this.body;
String str = String.join(DELIMETER, list);
return str;
}
public static String getChunk(int chunkSize, char c) {
char[] data = new char[chunkSize];
Arrays.fill(data, c);
return new String(data);
}
}