-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Zoned date time #3
base: option-0-no-api
Are you sure you want to change the base?
Changes from 1 commit
23e33a7
89e4c02
4c379c1
d2eb50d
19e10a6
c9c2268
54a628c
20ce194
6baa03c
50af408
aa0836c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
package tw.joi.energy.domain; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.Clock; | ||
import java.time.Instant; | ||
|
||
/** | ||
* @param time point in time | ||
* @param readingInKwH energy consumed in total to this point in time in kWh | ||
*/ | ||
public record ElectricityReading(Instant time, BigDecimal readingInKwH) {} | ||
public record ElectricityReading(Instant time, BigDecimal readingInKwH) { | ||
|
||
public ElectricityReading(Clock clock, double readingInKwH) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this constructor meant to be for testing purposes (faking the current time, lighter syntax for the reading) or also for production usage? |
||
this(clock.instant(), BigDecimal.valueOf(readingInKwH)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package tw.joi.energy.domain; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.DayOfWeek; | ||
|
||
public record PeakTimeMultiplier(DayOfWeek dayOfWeek, BigDecimal multiplier) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package tw.joi.energy.config; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static tw.joi.energy.config.ElectricityReadingsGenerator.MAX_HOURLY_USAGE; | ||
import static tw.joi.energy.config.ElectricityReadingsGenerator.MIN_HOURLY_USAGE; | ||
import static tw.joi.energy.config.ElectricityReadingsGenerator.generateElectricityReadingStream; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.Duration; | ||
import java.util.function.BiConsumer; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import tw.joi.energy.domain.ElectricityReading; | ||
|
||
class ElectricityReadingsGeneratorTest { | ||
|
||
@Test | ||
@DisplayName("Stream for one day should have 25 entries") | ||
void streamShouldHave25EntriesForOneDay() { | ||
assertThat(generateElectricityReadingStream(1).count()).isEqualTo(25); | ||
jejking-tw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Test | ||
@DisplayName("Stream for two days should have 49 entries") | ||
void streamShouldHave49EntriesForTwoDays() { | ||
jejking-tw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assertThat(generateElectricityReadingStream(2).count()).isEqualTo(49); | ||
} | ||
|
||
@Test | ||
@DisplayName("Stream for one day should end 24 hours after initial entry") | ||
void streamShouldHave24HoursAfterInitialEntry() { | ||
var streamAsList = generateElectricityReadingStream(1).toList(); | ||
var firstEntry = streamAsList.getFirst(); | ||
var lastEntry = streamAsList.getLast(); | ||
assertThat(Duration.between(firstEntry.time(), lastEntry.time())).hasHours(24); | ||
} | ||
|
||
@Test | ||
@DisplayName("Stream entries should be one hour apart") | ||
void streamEntriesShouldBeOneHourApart() { | ||
validateOrderedPairsOfEntries(generateElectricityReadingStream(1), (earlierReading, laterReading) -> assertThat( | ||
Duration.between(earlierReading.time(), laterReading.time())) | ||
.hasHours(1)); | ||
} | ||
|
||
@Test | ||
@DisplayName("Stream entries should have an increasing energy consumption over time") | ||
void streamEntriesShouldHaveAnIncreasingEnergyConsumptionOverTime() { | ||
validateOrderedPairsOfEntries(generateElectricityReadingStream(1), (earlierReading, laterReading) -> assertThat( | ||
laterReading.readingInKwH().compareTo(earlierReading.readingInKwH())) | ||
.isEqualTo(1)); | ||
} | ||
|
||
@Test | ||
@DisplayName("Stream entries should have an energy consumption in the expected reange") | ||
void streamEntriesShouldHaveAnIncreasingEnergyConsumption() { | ||
var min = BigDecimal.valueOf(MIN_HOURLY_USAGE); | ||
var max = BigDecimal.valueOf(MAX_HOURLY_USAGE); | ||
|
||
validateOrderedPairsOfEntries(generateElectricityReadingStream(1), (earlierReading, laterReading) -> { | ||
var energyBetweenReadings = laterReading.readingInKwH().subtract(earlierReading.readingInKwH()); | ||
assertThat(energyBetweenReadings.compareTo(min)).isEqualTo(1); | ||
assertThat(energyBetweenReadings.compareTo(max)).isEqualTo(-1); | ||
}); | ||
} | ||
|
||
private void validateOrderedPairsOfEntries( | ||
Stream<ElectricityReading> stream, BiConsumer<ElectricityReading, ElectricityReading> validator) { | ||
var streamAsList = stream.toList(); | ||
for (int i = 1; i <= streamAsList.size() - 1; i++) { | ||
var laterElectricityReading = streamAsList.get(i); | ||
var earlierElectricityReading = streamAsList.get(i - 1); | ||
validator.accept(earlierElectricityReading, laterElectricityReading); | ||
} | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still being used by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It works on my machine ... That's possibly because it seems to have disappeared. I'll double check what's happening there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't actually see that test in the branch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad, somehow that one was sitting around from another branch 🤷 |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I buy returning a stream here. The implementation doesn't look much simpler, and every single caller wants a
List
instead of aStream
.The name also adds a lot of stutter (unless every caller switches to a static import).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend the static import 😄 which was sort of my assumption anyway.
Lets think a bit more about streams - they're trivially convertible to lists anyway. To be honest, the functions also need some more possible parameters - eg the interval between the readings to generate and the function to generate the values. I hadn't quite finished removing all dependencies on hidden state such as Random...