From 023d9e7845ada6e91cf5ae3e3e3c867b87d7de6e Mon Sep 17 00:00:00 2001 From: Daan Kooij Date: Mon, 16 Jul 2018 10:11:18 +0200 Subject: [PATCH] Add support for timestamp field for Balance Candlesticks --- .../utwente/ing/misc/date/IntervalHelper.java | 2 +- .../ing/model/bean/BalanceCandlestick.java | 20 +++++++++++++++---- .../persistentmodel/PersistentModel.java | 9 ++++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/java/nl/utwente/ing/misc/date/IntervalHelper.java b/src/main/java/nl/utwente/ing/misc/date/IntervalHelper.java index c1c6022..46315e1 100644 --- a/src/main/java/nl/utwente/ing/misc/date/IntervalHelper.java +++ b/src/main/java/nl/utwente/ing/misc/date/IntervalHelper.java @@ -23,7 +23,7 @@ public class IntervalHelper { * * @param intervalPeriod The size of the intervals to be generated. * @param amount The amount of intervals to be generated. - * @return An array containing (amount + * @return An array containing LocalDateTime objects representing the requested intervals. */ public static LocalDateTime[] getIntervals(IntervalPeriod intervalPeriod, int amount) { LocalDateTime[] intervals = new LocalDateTime[amount + 1]; diff --git a/src/main/java/nl/utwente/ing/model/bean/BalanceCandlestick.java b/src/main/java/nl/utwente/ing/model/bean/BalanceCandlestick.java index 8e1d213..fbca34e 100644 --- a/src/main/java/nl/utwente/ing/model/bean/BalanceCandlestick.java +++ b/src/main/java/nl/utwente/ing/model/bean/BalanceCandlestick.java @@ -9,21 +9,23 @@ public class BalanceCandlestick { private float open, close, high, low, volume; + private long timestamp; /** * The constructor of BalanceCandlestick. - * Initially, only the opening balance is specified, after which this balance can be modified by using the mutate - * method. Along the way, this bean keeps track of the opening balance, the closing balance, the highest balance, - * the lowest balance and the volume of all the mutations combined. + * Initially, only the opening balance and the opening timestamp are specified, after which this balance can be + * modified by using the mutate method. Along the way, this bean keeps track of the opening balance, the closing + * balance, the highest balance, the lowest balance and the volume of all the mutations combined. * * @param open The opening balance of this BalanceCandlestick. */ - public BalanceCandlestick(float open) { + public BalanceCandlestick(float open, long timestamp) { this.open = open; this.close = open; this.high = open; this.low = open; this.volume = 0; + this.timestamp = timestamp; } /** @@ -71,6 +73,16 @@ public float getVolume() { return volume; } + /** + * Method used to retrieve the unix (seconds) timestamp indicating the beginning of the period of the + * BalanceCandlestick. + * + * @return The unix (seconds) timestamp indicating the beginning of the period of the BalanceCandlestick. + */ + public long getTimestamp() { + return timestamp; + } + /** * Method used to indicate a mutation of the balance of BalanceCandlestick. * Along the way, this method updates the closing balance, the highest balance, the lowest balance diff --git a/src/main/java/nl/utwente/ing/model/persistentmodel/PersistentModel.java b/src/main/java/nl/utwente/ing/model/persistentmodel/PersistentModel.java index a293bbe..4d81d0e 100644 --- a/src/main/java/nl/utwente/ing/model/persistentmodel/PersistentModel.java +++ b/src/main/java/nl/utwente/ing/model/persistentmodel/PersistentModel.java @@ -10,6 +10,7 @@ import java.sql.Connection; import java.sql.SQLException; import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.ArrayList; import java.util.UUID; @@ -466,10 +467,12 @@ public ArrayList getBalanceHistory(String sessionID, Interva ArrayList candlesticks = new ArrayList<>(); int index = 0; for (int i = 1; i <= amount; i++) { - LocalDateTime interval = intervals[i]; - BalanceCandlestick candlestick = new BalanceCandlestick(balance); + LocalDateTime startInterval = intervals[i - 1]; + LocalDateTime endInterval = intervals[i]; + long startUnixTime = startInterval.toEpochSecond(ZoneOffset.UTC); // Convert start of interval to UNIX time + BalanceCandlestick candlestick = new BalanceCandlestick(balance, startUnixTime); while (index < transactions.size() && - !IntervalHelper.isSmallerThan(interval, transactions.get(index).getDate())) { + !IntervalHelper.isSmallerThan(endInterval, transactions.get(index).getDate())) { if (transactions.get(index).getType().equals("deposit")) { candlestick.mutation(transactions.get(index).getAmount()); } else {