Created
February 3, 2021 21:11
-
-
Save littlebeeper/37703eb62622e9da2e65f9e240fc29b5 to your computer and use it in GitHub Desktop.
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
import java.math.BigDecimal; | |
import java.time.LocalDate; | |
import java.time.LocalTime; | |
import java.time.ZoneId; | |
import java.time.ZonedDateTime; | |
import java.time.temporal.TemporalAdjusters; | |
import java.util.*; | |
public class LegendOfEffectiveDate { | |
private static final TamsDb tamsDb = initialize(); | |
private static TamsDb initialize() { | |
final var constant = new Constant(); | |
constant.id = 1L; | |
constant.name = "Heat Rate 10%"; | |
constant.val = new BigDecimal("10.83302"); | |
tamsDb.constants.add(constant); | |
final var constantAud = new ConstantAud(); | |
constantAud.id = constant.id; | |
constantAud.name = constant.name; | |
constantAud.rev = 1L; | |
constantAud.timestamp = ZonedDateTime.parse("2020-06-31T23:59:59+01:00[Europe/Amsterdam]"); | |
tamsDb.constantsWithEffectiveDates.put(constantAud.timestamp, constantAud); | |
return tamsDb; | |
} | |
public static void main(String... args) { | |
BigDecimal monthlyReport = generateMonthlyReport(); | |
System.out.println("SCENARIO #1 Time: 2021-02-04, Result: " + monthlyReport); | |
List<BigDecimal> reportssByMonthDefault = generate2020LastQuarterReport(); | |
} | |
private static BigDecimal generateMonthlyReport() { | |
final var endOfLastMonth = LocalDate.now() | |
.minusMonths(1) | |
.with(TemporalAdjusters.lastDayOfMonth()) | |
.atTime(LocalTime.of(23, 45)) | |
.atZone(ZoneId.of("Europe/Amsterdam")); | |
return generateMonthlyReport(endOfLastMonth, tamsDb); | |
} | |
private static List<BigDecimal> generate2020LastQuarterReport() { | |
final var effectiveDate2020Dec = ZonedDateTime.parse("2020-12-31T23:59:59+01:00[Europe/Amsterdam]"); | |
BigDecimal report2020December = generateMonthlyReport(effectiveDate2020Dec, tamsDb); | |
final var effectiveDate2020Nov = ZonedDateTime.parse("2020-11-31T23:59:59+01:00[Europe/Amsterdam]"); | |
BigDecimal report2020November = generateMonthlyReport(effectiveDate2020Nov, tamsDb); | |
final var effectiveDate2020Oct = ZonedDateTime.parse("2020-10-31T23:59:59+01:00[Europe/Amsterdam]"); | |
BigDecimal report2020October = generateMonthlyReport(effectiveDate2020Oct, tamsDb); | |
return List.of(report2020December, report2020November, report2020October); | |
} | |
private static BigDecimal generateMonthlyReport(ZonedDateTime effectiveDate, TamsDb tamsDb) { | |
return BigDecimal.TEN.multiply(tamsDb.findValByEffectiveDate(effectiveDate)); | |
} | |
} | |
class TamsDb { | |
List<Constant> constants = new ArrayList<>(); | |
Map<ZonedDateTime, ConstantAud> constantsWithEffectiveDates = new TreeMap<>(); | |
public BigDecimal findValByEffectiveDate(ZonedDateTime effectiveDate) { | |
return constantsWithEffectiveDates.entrySet() | |
.stream() | |
.filter(entry -> entry.getKey().isBefore(effectiveDate)) | |
.findFirst() | |
.get() | |
.getValue() | |
.val; | |
} | |
} | |
class Constant { | |
Long id; | |
String name; | |
BigDecimal val; | |
} | |
// REVINFO joined | |
class ConstantAud { | |
Long rev; | |
//Data snapshot instant | |
ZonedDateTime timestamp; | |
Long id; | |
String name; | |
BigDecimal val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment