Skip to content

Commit d919151

Browse files
sylvain-bodintginsberg
authored andcommitted
Add support for TemporalAmount in every() functions (#3)
* Add support for TemporalAmount in every() functions
1 parent 0a855ae commit d919151

File tree

9 files changed

+168
-45
lines changed

9 files changed

+168
-45
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<artifactId>java-timestream</artifactId>
3232
<description>A set of builders that create streams of java.time objects</description>
3333
<url>https://github.com/tginsberg/java-timestream</url>
34-
<version>1.0.1-SNAPSHOT</version>
34+
<version>1.0.2-SNAPSHOT</version>
3535
<packaging>jar</packaging>
3636

3737
<issueManagement>

src/main/java/com/ginsberg/timestream/LocalDateStream.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
package com.ginsberg.timestream;
2626

2727
import java.time.LocalDate;
28+
import java.time.Period;
2829
import java.time.temporal.ChronoUnit;
2930
import java.util.Objects;
3031
import java.util.function.UnaryOperator;
3132

3233
/**
3334
* A builder that creates a stream of LocalDate objects.
34-
*
35+
* <p>
3536
* <pre>
3637
* {@code
3738
* // Print all of the LocalDates between now and a year from now, every other day.
@@ -47,9 +48,13 @@
4748
* @author Todd Ginsberg (todd@ginsberg.com)
4849
*/
4950
public class LocalDateStream extends AbstractComparableStream<LocalDate> {
50-
private int amount = 1;
51+
private long amount = 1;
5152
private ChronoUnit unit = ChronoUnit.DAYS;
5253

54+
private LocalDateStream(final LocalDate from) {
55+
super(from);
56+
}
57+
5358
/**
5459
* Create a LocalDateStream, starting at LocalDate.now().
5560
*
@@ -69,10 +74,6 @@ public static LocalDateStream from(final LocalDate from) {
6974
return new LocalDateStream(from);
7075
}
7176

72-
private LocalDateStream(final LocalDate from) {
73-
super(from);
74-
}
75-
7677
/**
7778
* Set the inclusive end point of the stream, using an absolute LocalDate.
7879
*
@@ -88,7 +89,7 @@ public LocalDateStream to(final LocalDate to) {
8889
* Set the inclusive end point of the stream, using a relative duration.
8990
*
9091
* @param amount The number of units to use when calculating the duration of the stream. May be negative.
91-
* @param unit The non-null unit the amount is denominated in. May not be null.
92+
* @param unit The non-null unit the amount is denominated in. May not be null.
9293
* @return A non-null LocalDateStream.
9394
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
9495
* @see ChronoUnit
@@ -115,7 +116,7 @@ public LocalDateStream until(final LocalDate until) {
115116
* Set the exclusive end point of the stream, using a relative duration.
116117
*
117118
* @param amount The number of units to use when calculating the duration of the stream. May be negative.
118-
* @param unit The non-null unit the amount is denominated in.
119+
* @param unit The non-null unit the amount is denominated in.
119120
* @return A non-null LocalDateStream.
120121
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
121122
* @see ChronoUnit
@@ -132,7 +133,7 @@ public LocalDateStream until(int amount,
132133
* for this builder is 1 Day.
133134
*
134135
* @param amount The number of units to use when calculating the next element of the stream.
135-
* @param unit The non-null unit the amount is denominated in.
136+
* @param unit The non-null unit the amount is denominated in.
136137
* @return A non-null LocalDateStream.
137138
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
138139
* @see ChronoUnit
@@ -146,6 +147,23 @@ public LocalDateStream every(int amount,
146147
return this;
147148
}
148149

150+
/**
151+
* Set the duration between successive elements produced by the stream. The default
152+
* for this builder is 1 Day.
153+
*
154+
* @param period The interval to use when calculating the next element of the stream.
155+
* @return A non-null LocalDateStream.
156+
* @throws java.time.temporal.UnsupportedTemporalTypeException if the period is not supported.
157+
* @see ChronoUnit
158+
*/
159+
public LocalDateStream every(Period period) {
160+
Objects.requireNonNull(period);
161+
this.unit = ChronoUnit.DAYS;
162+
this.amount = period.get(this.unit);
163+
LocalDate.now().plus(0, unit); // Fail fast test
164+
return this;
165+
}
166+
149167
@Override
150168
UnaryOperator<LocalDate> next() {
151169
return date -> date.plus(isForward() ? amount : 0 - amount, unit);

src/main/java/com/ginsberg/timestream/LocalDateTimeStream.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424

2525
package com.ginsberg.timestream;
2626

27+
import java.time.Duration;
2728
import java.time.LocalDateTime;
2829
import java.time.temporal.ChronoUnit;
2930
import java.util.Objects;
3031
import java.util.function.UnaryOperator;
3132

3233
/**
3334
* A builder that creates a stream of LocalDateTime objects.
34-
*
35+
* <p>
3536
* <pre>
3637
* {@code
3738
* // Print all of the LocalDateTimes between now and a day from now, every other minute.
@@ -47,9 +48,13 @@
4748
* @author Todd Ginsberg (todd@ginsberg.com)
4849
*/
4950
public class LocalDateTimeStream extends AbstractComparableStream<LocalDateTime> {
50-
private int amount = 1;
51+
private long amount = 1;
5152
private ChronoUnit unit = ChronoUnit.SECONDS;
5253

54+
private LocalDateTimeStream(final LocalDateTime from) {
55+
super(from);
56+
}
57+
5358
/**
5459
* Create a LocalDateTimeStream, starting at LocalDateTime.now().
5560
*
@@ -69,10 +74,6 @@ public static LocalDateTimeStream from(final LocalDateTime from) {
6974
return new LocalDateTimeStream(from);
7075
}
7176

72-
private LocalDateTimeStream(final LocalDateTime from) {
73-
super(from);
74-
}
75-
7677
/**
7778
* Set the inclusive end point of the stream, using an absolute LocalDateTime.
7879
*
@@ -88,7 +89,7 @@ public LocalDateTimeStream to(final LocalDateTime to) {
8889
* Set the inclusive end point of the stream, using a relative duration.
8990
*
9091
* @param amount The number of units to use when calculating the duration of the stream. May be negative.
91-
* @param unit The non-null unit the amount is denominated in. May not be null.
92+
* @param unit The non-null unit the amount is denominated in. May not be null.
9293
* @return A non-null LocalDateTimeStream.
9394
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
9495
* @see ChronoUnit
@@ -115,7 +116,7 @@ public LocalDateTimeStream until(final LocalDateTime until) {
115116
* Set the exclusive end point of the stream, using a relative duration.
116117
*
117118
* @param amount The number of units to use when calculating the duration of the stream. May be negative.
118-
* @param unit The non-null unit the amount is denominated in.
119+
* @param unit The non-null unit the amount is denominated in.
119120
* @return A non-null LocalDateTimeStream.
120121
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
121122
* @see ChronoUnit
@@ -132,7 +133,7 @@ public LocalDateTimeStream until(int amount,
132133
* for this builder is 1 Second.
133134
*
134135
* @param amount The number of units to use when calculating the next element of the stream.
135-
* @param unit The non-null unit the amount is denominated in.
136+
* @param unit The non-null unit the amount is denominated in.
136137
* @return A non-null LocalDateTimeStream.
137138
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
138139
* @see ChronoUnit
@@ -145,6 +146,22 @@ public LocalDateTimeStream every(int amount,
145146
return this;
146147
}
147148

149+
/**
150+
* Set the duration between successive elements produced by the stream. The default
151+
* for this builder is 1 Second.
152+
*
153+
* @param duration The interval to use when calculating the next element of the stream.
154+
* @return A non-null LocalDateTimeStream.
155+
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
156+
* @see ChronoUnit
157+
*/
158+
public LocalDateTimeStream every(Duration duration) {
159+
Objects.requireNonNull(unit);
160+
this.unit = ChronoUnit.SECONDS;
161+
this.amount = duration.get(this.unit);
162+
return this;
163+
}
164+
148165
@Override
149166
UnaryOperator<LocalDateTime> next() {
150167
return date -> date.plus(isForward() ? amount : 0 - amount, unit);

src/main/java/com/ginsberg/timestream/YearMonthStream.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424

2525
package com.ginsberg.timestream;
2626

27+
import java.time.Period;
2728
import java.time.YearMonth;
2829
import java.time.temporal.ChronoUnit;
2930
import java.util.Objects;
3031
import java.util.function.UnaryOperator;
3132

3233
/**
3334
* A builder that creates a stream of YearMonth objects.
34-
*
35+
* <p>
3536
* <pre>
3637
* {@code
3738
* // Print all of the YearMonths between now and a year from now, every other month.
@@ -47,9 +48,13 @@
4748
* @author Todd Ginsberg (todd@ginsberg.com)
4849
*/
4950
public class YearMonthStream extends AbstractComparableStream<YearMonth> {
50-
private int amount = 1;
51+
private long amount = 1;
5152
private ChronoUnit unit = ChronoUnit.MONTHS;
5253

54+
private YearMonthStream(final YearMonth from) {
55+
super(from);
56+
}
57+
5358
/**
5459
* Create a YearMonthStream, starting at YearMonth.now().
5560
*
@@ -69,10 +74,6 @@ public static YearMonthStream from(final YearMonth from) {
6974
return new YearMonthStream(from);
7075
}
7176

72-
private YearMonthStream(final YearMonth from) {
73-
super(from);
74-
}
75-
7677
/**
7778
* Set the inclusive end point of the stream, using an absolute YearMonth.
7879
*
@@ -88,7 +89,7 @@ public YearMonthStream to(final YearMonth to) {
8889
* Set the inclusive end point of the stream, using a relative duration.
8990
*
9091
* @param amount The number of units to use when calculating the duration of the stream. May be negative.
91-
* @param unit The non-null unit the amount is denominated in. May not be null.
92+
* @param unit The non-null unit the amount is denominated in. May not be null.
9293
* @return A non-null YearMonthStream.
9394
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
9495
* @see ChronoUnit
@@ -115,7 +116,7 @@ public YearMonthStream until(final YearMonth until) {
115116
* Set the exclusive end point of the stream, using a relative duration.
116117
*
117118
* @param amount The number of units to use when calculating the duration of the stream. May be negative.
118-
* @param unit The non-null unit the amount is denominated in.
119+
* @param unit The non-null unit the amount is denominated in.
119120
* @return A non-null YearMonthStream.
120121
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
121122
* @see ChronoUnit
@@ -132,7 +133,7 @@ public YearMonthStream until(int amount,
132133
* for this builder is 1 Month.
133134
*
134135
* @param amount The number of units to use when calculating the next element of the stream.
135-
* @param unit The non-null unit the amount is denominated in.
136+
* @param unit The non-null unit the amount is denominated in.
136137
* @return A non-null YearMonthStream.
137138
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
138139
* @see ChronoUnit
@@ -146,6 +147,22 @@ public YearMonthStream every(int amount,
146147
return this;
147148
}
148149

150+
/**
151+
* Set the duration between successive elements produced by the stream. The default
152+
* for this builder is 1 Month.
153+
*
154+
* @return A non-null YearMonthStream.
155+
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
156+
* @see ChronoUnit
157+
*/
158+
public YearMonthStream every(Period period) {
159+
Objects.requireNonNull(unit);
160+
this.unit = ChronoUnit.MONTHS;
161+
this.amount = period.get(this.unit);
162+
YearMonth.now().plus(0, unit); // Fail fast test
163+
return this;
164+
}
165+
149166
@Override
150167
UnaryOperator<YearMonth> next() {
151168
return date -> date.plus(isForward() ? amount : 0 - amount, unit);

src/main/java/com/ginsberg/timestream/ZonedDateTimeStream.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424

2525
package com.ginsberg.timestream;
2626

27+
import java.time.Duration;
2728
import java.time.ZonedDateTime;
2829
import java.time.temporal.ChronoUnit;
2930
import java.util.Objects;
3031
import java.util.function.UnaryOperator;
3132

3233
/**
3334
* A builder that creates a stream of ZonedDateTime objects.
34-
*
35+
* <p>
3536
* <pre>
3637
* {@code
3738
* // Print all of the ZonedDateTimes between now and a day from now, every other minute.
@@ -47,9 +48,13 @@
4748
* @author Todd Ginsberg (todd@ginsberg.com)
4849
*/
4950
public class ZonedDateTimeStream extends AbstractComparableStream<ZonedDateTime> {
50-
private int amount = 1;
51+
private long amount = 1;
5152
private ChronoUnit unit = ChronoUnit.SECONDS;
5253

54+
private ZonedDateTimeStream(final ZonedDateTime from) {
55+
super(from);
56+
}
57+
5358
/**
5459
* Create a ZonedDateTimeStream, starting at ZonedDateTime.now().
5560
*
@@ -69,10 +74,6 @@ public static ZonedDateTimeStream from(final ZonedDateTime from) {
6974
return new ZonedDateTimeStream(from);
7075
}
7176

72-
private ZonedDateTimeStream(final ZonedDateTime from) {
73-
super(from);
74-
}
75-
7677
/**
7778
* Set the inclusive end point of the stream, using an absolute ZonedDateTime.
7879
*
@@ -88,7 +89,7 @@ public ZonedDateTimeStream to(final ZonedDateTime to) {
8889
* Set the inclusive end point of the stream, using a relative duration.
8990
*
9091
* @param amount The number of units to use when calculating the duration of the stream. May be negative.
91-
* @param unit The non-null unit the amount is denominated in. May not be null.
92+
* @param unit The non-null unit the amount is denominated in. May not be null.
9293
* @return A non-null ZonedDateTimeStream.
9394
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
9495
* @see ChronoUnit
@@ -115,7 +116,7 @@ public ZonedDateTimeStream until(final ZonedDateTime until) {
115116
* Set the exclusive end point of the stream, using a relative duration.
116117
*
117118
* @param amount The number of units to use when calculating the duration of the stream. May be negative.
118-
* @param unit The non-null unit the amount is denominated in.
119+
* @param unit The non-null unit the amount is denominated in.
119120
* @return A non-null ZonedDateTimeStream.
120121
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
121122
* @see ChronoUnit
@@ -132,7 +133,7 @@ public ZonedDateTimeStream until(int amount,
132133
* for this builder is 1 Second.
133134
*
134135
* @param amount The number of units to use when calculating the next element of the stream.
135-
* @param unit The non-null unit the amount is denominated in.
136+
* @param unit The non-null unit the amount is denominated in.
136137
* @return A non-null ZonedDateTimeStream.
137138
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
138139
* @see ChronoUnit
@@ -145,6 +146,22 @@ public ZonedDateTimeStream every(int amount,
145146
return this;
146147
}
147148

149+
/**
150+
* Set the duration between successive elements produced by the stream. The default
151+
* for this builder is 1 Second.
152+
*
153+
154+
* @return A non-null ZonedDateTimeStream.
155+
* @throws java.time.temporal.UnsupportedTemporalTypeException if the unit is not supported.
156+
* @see ChronoUnit
157+
*/
158+
public ZonedDateTimeStream every(Duration duration) {
159+
Objects.requireNonNull(unit);
160+
this.unit = ChronoUnit.SECONDS;
161+
this.amount = duration.get(this.unit);
162+
return this;
163+
}
164+
148165
@Override
149166
UnaryOperator<ZonedDateTime> next() {
150167
return date -> date.plus(isForward() ? amount : 0 - amount, unit);

0 commit comments

Comments
 (0)