This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNote.java
95 lines (81 loc) · 2.33 KB
/
Note.java
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
package notes;
import java.io.UnsupportedEncodingException;
/**
* Note item.
*
* @author skoro
*/
public class Note extends Model {
public static final int TITLE_MAX = 25;
protected String title;
protected String text;
public Note(String text) throws EmptyStringException {
setText(text);
this.title = createTitleFromText(text);
}
public Note(String text, String title) throws EmptyStringException {
setText(text);
setTitle(title);
}
/**
* Create note title from a text.
*
* @param text
* @return
* @throws EmptyStringException
*/
public String createTitleFromText(String text) throws EmptyStringException {
text = text.trim();
if (isEmpty(text)) {
throw new EmptyStringException();
}
int pos = text.indexOf('\n');
if (pos != -1) {
return createTitleFromText(text.substring(0, pos));
}
if (text.length() < TITLE_MAX) {
return text;
}
return text.substring(0, TITLE_MAX - 1) + "...";
}
public String getTitle() {
return title;
}
public void setTitle(String title) throws EmptyStringException {
if (isEmpty(title)) {
this.title = createTitleFromText(text);
} else {
this.title = title;
}
}
public String getText() {
return text;
}
public void setText(String text) throws EmptyStringException {
if (isEmpty(text)) {
throw new EmptyStringException();
}
this.text = text;
}
protected boolean isEmpty(String s) {
return s.trim().length() == 0;
}
public byte[] toBytes() {
byte[] data;
try {
data = text.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
data = text.getBytes();
}
return data;
}
public static Model createFromBytes(byte[] buf) throws EmptyStringException {
String text;
try {
text = new String(buf, "UTF-8");
} catch (UnsupportedEncodingException e) {
text = new String(buf);
}
return new Note(text);
}
}