Skip to content

Commit be62dd2

Browse files
committed
Add OrgActiveTimestamps
Title must be parsed too, so collecting timestamps from OrgContent alone is not enough. Added for orgzly/orgzly-android#76
1 parent 1457050 commit be62dd2

File tree

7 files changed

+72
-135
lines changed

7 files changed

+72
-135
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.orgzly.org;
2+
3+
import com.orgzly.org.datetime.OrgRange;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.regex.Matcher;
8+
9+
public class OrgActiveTimestamps {
10+
public static List<OrgRange> parse(String str) {
11+
List<OrgRange> timestamps = new ArrayList<>();
12+
13+
if (str != null && str.length() > 0) {
14+
Matcher m = OrgPatterns.DT_OR_RANGE_P.matcher(str);
15+
16+
while (m.find()) {
17+
OrgRange range = OrgRange.parse(m.group());
18+
19+
if (range.getStartTime().isActive()) {
20+
timestamps.add(range);
21+
}
22+
}
23+
}
24+
25+
return timestamps;
26+
}
27+
}
Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
package com.orgzly.org;
22

3-
import com.orgzly.org.datetime.OrgRange;
4-
5-
import java.util.ArrayList;
6-
import java.util.List;
7-
import java.util.regex.Matcher;
8-
93
/**
104
* Text below a heading.
115
*/
126
public class OrgContent {
137
private StringBuilder value;
14-
private List<OrgRange> timestamps;
15-
private boolean dirty; /** Whether timestamps are out of sync with `value`,
16-
* and need to be reparsed. */
178

189
public OrgContent() {
1910
value = new StringBuilder();
20-
dirty = false;
2111
}
2212

2313
/**
@@ -34,12 +24,10 @@ public void set(String value) {
3424
} else {
3525
this.value = new StringBuilder(value);
3626
}
37-
dirty = true;
3827
}
3928

4029
public void append(String s) {
4130
value.append(s);
42-
dirty = true;
4331
}
4432

4533
/**
@@ -48,41 +36,4 @@ public void append(String s) {
4836
public String toString() {
4937
return value.toString();
5038
}
51-
52-
/**
53-
* @return the list of plain timestamps in this content text
54-
*/
55-
public List<OrgRange> getTimestamps() {
56-
if (timestamps == null) {
57-
timestamps = new ArrayList<>();
58-
}
59-
if (dirty) {
60-
reparse();
61-
}
62-
return timestamps;
63-
}
64-
65-
public boolean hasTimestamps() {
66-
if (timestamps == null) {
67-
return false;
68-
}
69-
if (dirty) {
70-
reparse();
71-
}
72-
return !timestamps.isEmpty();
73-
}
74-
75-
/** Parse all plain timestamps in this content and rebuild the timestamps list. */
76-
private void reparse() {
77-
timestamps.clear();
78-
Matcher m = OrgPatterns.DT_OR_RANGE_P.matcher(toString());
79-
while (m.find()) {
80-
OrgRange range = OrgRange.parse(m.group());
81-
if (range.getStartTime().isActive()) {
82-
timestamps.add(range);
83-
}
84-
}
85-
dirty = false;
86-
}
87-
8839
}

src/main/java/com/orgzly/org/OrgHead.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,6 @@ public void appendContent(String s) {
122122
getContentObject().append(s);
123123
}
124124

125-
public List<OrgRange> getTimestamps() {
126-
return getContentObject().getTimestamps();
127-
}
128-
129-
public boolean hasTimestamps() {
130-
return getContentObject().hasTimestamps();
131-
}
132-
133125
/**
134126
* Scheduled time.
135127
*

src/main/java/com/orgzly/org/datetime/OrgDateTime.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,15 @@ public Builder setDelay(OrgDelay delay) {
414414
return this;
415415
}
416416

417+
public Builder setDay(long timestamp) {
418+
Calendar cal = new GregorianCalendar();
419+
cal.setTimeInMillis(timestamp);
420+
421+
return setYear(cal.get(Calendar.YEAR))
422+
.setMonth(cal.get(Calendar.MONTH))
423+
.setDay(cal.get(Calendar.DAY_OF_MONTH));
424+
}
425+
417426
public OrgDateTime build() {
418427
OrgDateTime time = new OrgDateTime();
419428

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.orgzly.org;
2+
3+
import com.orgzly.org.datetime.OrgRange;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.util.List;
9+
10+
public class OrgActiveTimestampsTest extends OrgTestParser {
11+
@Test
12+
public void testPlainTimestamp() {
13+
String str = "<2018-01-15 Mon>\n";
14+
15+
List<OrgRange> timestamps = OrgActiveTimestamps.parse(str);
16+
17+
Assert.assertEquals(1, timestamps.size());
18+
Assert.assertEquals("<2018-01-15 Mon>", timestamps.get(0).toString());
19+
}
20+
21+
@Test
22+
public void testPlainTimestamps() {
23+
String str =
24+
"<2018-01-15 Mon> some text between timestamps <2018-01-16 Tue>\n" +
25+
"Blabla some other text in front <2018-01-17 Wed> text after\n" +
26+
"\n\n" +
27+
"[2018-01-18 Thu]\n\n";
28+
29+
List<OrgRange> timestamps = OrgActiveTimestamps.parse(str);
30+
31+
Assert.assertEquals(3, timestamps.size());
32+
Assert.assertEquals("<2018-01-15 Mon>", timestamps.get(0).toString());
33+
Assert.assertEquals("<2018-01-16 Tue>", timestamps.get(1).toString());
34+
Assert.assertEquals("<2018-01-17 Wed>", timestamps.get(2).toString());
35+
}
36+
}

src/test/java/com/orgzly/org/OrgContentTest.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/test/java/com/orgzly/org/parser/OrgParserTest.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -759,37 +759,6 @@ public void testOrgTagsColumnNegativeWithFullWidthChars() throws IOException {
759759
Assert.assertEquals(expectedStr, parsedStr);
760760
}
761761

762-
@Test
763-
public void testPlainTimestamps() throws IOException {
764-
String str = "* Node\n" +
765-
"CLOSED: [2012-12-29 Sat 08:05] DEADLINE: <2012-12-28 Fri> SCHEDULED: <2012-12-27 Thu>\n" +
766-
"<2018-01-15 Mon> some text between timestamps <2018-01-16 Tue>\n" +
767-
"Blabla some other text in front <2018-01-17 Wed> text after\n" +
768-
"\n\n" +
769-
"[2018-01-18 Thu]\n\n";
770-
771-
OrgParsedFile file = parserBuilder.setInput(str).build().parse();
772-
773-
List<OrgRange> timestamps = file.getHeadsInList().get(0).getHead().getTimestamps();
774-
Assert.assertEquals(4, timestamps.size());
775-
Assert.assertEquals("<2018-01-15 Mon>", timestamps.get(0).toString());
776-
Assert.assertEquals("<2018-01-16 Tue>", timestamps.get(1).toString());
777-
Assert.assertEquals("<2018-01-17 Wed>", timestamps.get(2).toString());
778-
Assert.assertEquals("[2018-01-18 Thu]", timestamps.get(3).toString());
779-
}
780-
781-
@Test
782-
public void testPlainTimestampsNoHeader() throws IOException {
783-
String str = "* Node\n" +
784-
"<2018-01-15 Mon>\n";
785-
786-
OrgParsedFile file = parserBuilder.setInput(str).build().parse();
787-
788-
List<OrgRange> timestamps = file.getHeadsInList().get(0).getHead().getTimestamps();
789-
Assert.assertEquals(1, timestamps.size());
790-
Assert.assertEquals("<2018-01-15 Mon>", timestamps.get(0).toString());
791-
}
792-
793762
private String parsed(String original) throws IOException {
794763
return parsed(original, OrgParserSettings.getBasic());
795764
}

0 commit comments

Comments
 (0)