-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJournalEntry.java
More file actions
68 lines (55 loc) · 1.87 KB
/
Copy pathJournalEntry.java
File metadata and controls
68 lines (55 loc) · 1.87 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
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class JournalEntry {
private final int id;
private String title;
private String content;
private final LocalDateTime createdAt;
public JournalEntry(int id, String title, String content, LocalDateTime createdAt) {
this.id = id;
this.title = title;
this.content = content;
this.createdAt = createdAt;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}
public String serialize() {
return id + "||" + escape(title) + "||" + escape(content) + "||" +
createdAt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
public static JournalEntry deserialize(String line) {
String[] parts = line.split("\\|\\|", 4);
if (parts.length != 4) return null;
int id = Integer.parseInt(parts[0]);
String title = unescape(parts[1]);
String content = unescape(parts[2]);
LocalDateTime createdAt = LocalDateTime.parse(parts[3], DateTimeFormatter.ISO_LOCAL_DATE_TIME);
return new JournalEntry(id, title, content, createdAt);
}
private static String escape(String text) {
return text.replace("\\", "\\\\").replace("||", "\\|\\|");
}
private static String unescape(String text) {
return text.replace("\\|\\|", "||").replace("\\\\", "\\");
}
@Override
public String toString() {
return "[" + id + "] " + title + " (" + createdAt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")) + ")";
}
}