Skip to content

Commit 176e5be

Browse files
committed
Added recurring interval inspection
1 parent c94ed13 commit 176e5be

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎nlp/src/main/java/org/ocpsoft/prettytime/nlp/PrettyTimeParser.java‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.ocpsoft.prettytime.nlp;
22

3+
import java.io.ObjectInputStream.GetField;
34
import java.util.ArrayList;
45
import java.util.Calendar;
56
import java.util.Date;
@@ -115,8 +116,9 @@ public List<DateGroup> parseSyntax(String language)
115116

116117
List<DateGroup> result = new ArrayList<DateGroup>();
117118
List<com.joestelmach.natty.DateGroup> groups = parser.parse(language);
119+
Date now = new Date();
118120
for (com.joestelmach.natty.DateGroup group : groups) {
119-
result.add(new DateGroupImpl(group));
121+
result.add(new DateGroupImpl(now, group));
120122
}
121123
return result;
122124
}
@@ -167,9 +169,11 @@ private class DateGroupImpl implements DateGroup
167169
private Date recursUntil;
168170
private String text;
169171
private boolean recurring;
172+
private Date now;
170173

171-
public DateGroupImpl(com.joestelmach.natty.DateGroup group)
174+
public DateGroupImpl(Date now, com.joestelmach.natty.DateGroup group)
172175
{
176+
this.now = now;
173177
dates = group.getDates();
174178
line = group.getLine();
175179
position = group.getPosition();
@@ -209,5 +213,14 @@ public boolean isRecurring()
209213
return recurring;
210214
}
211215

216+
@Override
217+
public long getRecurInterval()
218+
{
219+
if (isRecurring())
220+
return getDates().get(0).getTime() - now.getTime();
221+
else
222+
return -1;
223+
}
224+
212225
}
213226
}

‎nlp/src/main/java/org/ocpsoft/prettytime/nlp/parse/DateGroup.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ public interface DateGroup
3939
* Return <code>true</code> if this {@link DateGroup} is a recurring event.
4040
*/
4141
boolean isRecurring();
42+
43+
/**
44+
* If this {@link DateGroup} is recurring, return the interval in milliseconds with which this {@link DateGroup}
45+
* recurs, otherwise return -1;
46+
*/
47+
long getRecurInterval();
4248
}

‎nlp/src/test/java/org/ocpsoft/prettytime/nlp/PrettyTimeParserTest.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ public void testParseSyntax()
5959
Assert.assertEquals(1, parse.get(0).getDates().size());
6060
Assert.assertNull(parse.get(0).getRecursUntil());
6161
Assert.assertFalse(parse.get(0).isRecurring());
62+
Assert.assertEquals(-1, parse.get(0).getRecurInterval());
63+
}
64+
65+
@Test
66+
public void testParseSyntaxRecurring()
67+
{
68+
List<DateGroup> parse = new PrettyTimeParser().parseSyntax("I do it every three days");
69+
Assert.assertFalse(parse.isEmpty());
70+
String formatted = new PrettyTime().format(parse.get(0).getDates().get(0));
71+
Assert.assertEquals("3 days from now", formatted);
72+
Assert.assertEquals(1, parse.get(0).getLine());
73+
Assert.assertEquals(14, parse.get(0).getPosition());
74+
Assert.assertEquals(1, parse.get(0).getDates().size());
75+
Assert.assertNull(parse.get(0).getRecursUntil());
76+
Assert.assertTrue(parse.get(0).isRecurring());
77+
Assert.assertEquals(1000 * 60 * 60 * 24 * 3, parse.get(0).getRecurInterval());
6278
}
6379

6480
}

0 commit comments

Comments
 (0)