-
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
Open
jejking-tw
wants to merge
11
commits into
ThoughtWorksInc:option-0-no-api
Choose a base branch
from
jejking-tw:zoned-date-time
base: option-0-no-api
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
23e33a7
WIP - assorted improvements to readability. Introduces ZonedDateTime
jejking-tw 89e4c02
WIP - changes readings generator to use a stream, more use of zoned d…
jejking-tw 4c379c1
Completes chunk of proposed refactoring.
jejking-tw d2eb50d
Update src/main/java/tw/joi/energy/config/ElectricityReadingsGenerato…
jejking-tw 19e10a6
Update src/test/java/tw/joi/energy/config/ElectricityReadingsGenerato…
jejking-tw c9c2268
Changes PricePlanTest to use isEqualByComparingTo on BigDecimal
jejking-tw 54a628c
Makes it clearer that the fixed time stamp is just arbitary
jejking-tw 20ce194
Makes it clear that the fixed clock is arbitary.
jejking-tw 6baa03c
Provides an additional constructor for `PricePlan` that initiates an …
jejking-tw 50af408
applies code formatting rules
jejking-tw aa0836c
rationalises test method names to align with @DisplayName annotation
jejking-tw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 30 additions & 22 deletions
52
src/main/java/tw/joi/energy/config/ElectricityReadingsGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,43 @@ | ||
package tw.joi.energy.config; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.time.Clock; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Random; | ||
import java.util.stream.Stream; | ||
import tw.joi.energy.domain.ElectricityReading; | ||
|
||
public class ElectricityReadingsGenerator { | ||
|
||
public static List<ElectricityReading> generate(int number) { | ||
List<ElectricityReading> readings = new ArrayList<>(); | ||
Instant now = Instant.now(); | ||
BigDecimal previousReading = BigDecimal.ONE; | ||
Instant previousReadingTime = now.minusSeconds(2 * number * 60L); | ||
public static final double AVG_HOURLY_USAGE = 0.3; | ||
public static final double VARIANCE = 0.2; | ||
public static final double MIN_HOURLY_USAGE = AVG_HOURLY_USAGE - VARIANCE; | ||
public static final double MAX_HOURLY_USAGE = AVG_HOURLY_USAGE + VARIANCE; | ||
|
||
Random readingRandomiser = new Random(); | ||
private ElectricityReadingsGenerator() {} | ||
|
||
for (int i = 0; i < number; i++) { | ||
double positiveIncrement = Math.abs(readingRandomiser.nextGaussian()); | ||
BigDecimal currentReading = | ||
previousReading.add(BigDecimal.valueOf(positiveIncrement)).setScale(4, RoundingMode.CEILING); | ||
ElectricityReading electricityReading = | ||
new ElectricityReading(previousReadingTime.plusSeconds(i * 60L), currentReading); | ||
readings.add(electricityReading); | ||
previousReading = currentReading; | ||
} | ||
public static Stream<ElectricityReading> generateElectricityReadingStream(int days) { | ||
return generateElectricityReadingStream(Clock.systemDefaultZone(), BigDecimal.ZERO, days); | ||
} | ||
|
||
// we'll provide hourly readings for the specified number of days assuming 24 hours a day | ||
// we'll assume that a house consumes ca 2700 kWh a year, so about 0.3 kWh per hour | ||
|
||
readings.sort(Comparator.comparing(ElectricityReading::time)); | ||
return readings; | ||
// the assumed starting point is the time on the clock, the ending point 24 hours later - so for 1 day, we'll get 25 | ||
// readings | ||
public static Stream<ElectricityReading> generateElectricityReadingStream( | ||
Clock clock, BigDecimal initialReading, int days) { | ||
var now = clock.instant(); | ||
var readingRandomiser = new Random(); | ||
var seed = new ElectricityReading(now, initialReading); | ||
var lastTimeToBeSupplied = now.plus(days * 24L, ChronoUnit.HOURS); | ||
return Stream.iterate( | ||
seed, er -> er.time().equals(lastTimeToBeSupplied) || er.time().isBefore(lastTimeToBeSupplied), er -> { | ||
jejking-tw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var hoursWorthOfEnergy = | ||
BigDecimal.valueOf(readingRandomiser.nextDouble(MIN_HOURLY_USAGE, MAX_HOURLY_USAGE)); | ||
return new ElectricityReading( | ||
er.time().plus(1, ChronoUnit.HOURS), | ||
er.readingInKwH().add(hoursWorthOfEnergy)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
package tw.joi.energy.domain; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.Clock; | ||
import java.time.Instant; | ||
|
||
/** | ||
* @param reading kWh | ||
* @param time point in time | ||
* @param readingInKwH energy consumed in total to this point in time in kWh | ||
*/ | ||
public record ElectricityReading(Instant time, BigDecimal reading) {} | ||
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,46 @@ | ||
package tw.joi.energy.domain; | ||
|
||
import java.io.*; | ||
import java.math.BigDecimal; | ||
import java.text.*; | ||
import java.time.DayOfWeek; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.time.ZonedDateTime; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class PricePlan { | ||
|
||
private String energySupplier; | ||
private String planName; | ||
private final String energySupplier; | ||
private final String planName; | ||
private final BigDecimal unitRate; // unit price per kWh | ||
private final List<PeakTimeMultiplier> peakTimeMultipliers; | ||
private final Map<DayOfWeek, BigDecimal> peakTimeMultipliers; | ||
|
||
public PricePlan( | ||
String planName, String energySupplier, BigDecimal unitRate, List<PeakTimeMultiplier> peakTimeMultipliers) { | ||
String planName, String energySupplier, BigDecimal unitRate, Set<PeakTimeMultiplier> peakTimeMultipliers) { | ||
jejking-tw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.planName = planName; | ||
this.energySupplier = energySupplier; | ||
this.unitRate = unitRate; | ||
this.peakTimeMultipliers = peakTimeMultipliers; | ||
this.peakTimeMultipliers = peakTimeMultipliers.stream() | ||
.collect(Collectors.toUnmodifiableMap(PeakTimeMultiplier::dayOfWeek, PeakTimeMultiplier::multiplier)); | ||
jejking-tw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public String getEnergySupplier() { | ||
return energySupplier; | ||
} | ||
|
||
public void setEnergySupplier(String supplierName) { | ||
this.energySupplier = supplierName; | ||
} | ||
|
||
public String getPlanName() { | ||
return planName; | ||
} | ||
|
||
public void setPlanName(String name) { | ||
this.planName = name; | ||
} | ||
|
||
public BigDecimal getUnitRate() { | ||
return unitRate; | ||
} | ||
|
||
public BigDecimal getPrice(LocalDateTime dateTime) { | ||
public BigDecimal getPrice(ZonedDateTime dateTime) { | ||
return unitRate; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Name: '" + planName + "', Unit Rate: " + unitRate + ", Supplier: '" + energySupplier + "'"; | ||
} | ||
|
||
static class PeakTimeMultiplier { | ||
|
||
DayOfWeek dayOfWeek; | ||
BigDecimal multiplier; | ||
|
||
public PeakTimeMultiplier(DayOfWeek dayOfWeek, BigDecimal multiplier) { | ||
this.dayOfWeek = dayOfWeek; | ||
this.multiplier = multiplier; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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...