Skip to content

Commit

Permalink
Add timestamp field to balance history items
Browse files Browse the repository at this point in the history
  • Loading branch information
oplosthee committed Jun 10, 2018
1 parent f4740ed commit 91d4ffd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,16 @@ public String getBalanceHistory(@RequestHeader(value = "X-session-ID", required
List<HistoryItem> historyItems = new LinkedList<>();

// Create an initial HistoryItem with the balance of the account as values.
HistoryItem currentItem = new HistoryItem(sum);
HistoryItem currentItem = new HistoryItem(sum, calendar.getTimeInMillis() / 1000);
historyItems.add(currentItem);

int groupCount = 0;

// In case there are no transactions, fill the results with groups representing the sum and no movement.
if (transactionsSet.isAfterLast()) {
while (historyItems.size() < count) {
historyItems.add(new HistoryItem(sum));
calendar.add(intervalType, -1);
historyItems.add(new HistoryItem(sum, calendar.getTimeInMillis() / 1000));
}
}

Expand All @@ -165,9 +166,9 @@ public String getBalanceHistory(@RequestHeader(value = "X-session-ID", required
pointer.setTime(DATE_FORMAT.parse(transactionsSet.getString("date")));

while (calendar.after(pointer)) {
currentItem = new HistoryItem(currentItem.getOpen());
historyItems.add(currentItem);
calendar.add(intervalType, -1);
currentItem = new HistoryItem(currentItem.getOpen(), calendar.getTimeInMillis() / 1000);
historyItems.add(currentItem);
}

long amount = transactionsSet.getLong("amount");
Expand All @@ -186,9 +187,9 @@ public String getBalanceHistory(@RequestHeader(value = "X-session-ID", required
}

// Some data from the old group has to be carried over to the new group.
currentItem = new HistoryItem(currentItem.getOpen());
historyItems.add(currentItem);
calendar.add(intervalType, -1);
currentItem = new HistoryItem(currentItem.getOpen(), calendar.getTimeInMillis() / 1000);
historyItems.add(currentItem);
}

// Update the open value, which is the "current" value.
Expand All @@ -208,7 +209,8 @@ public String getBalanceHistory(@RequestHeader(value = "X-session-ID", required
// In case not enough transactions were retrieved from the database to fill the count:
// Similarly to the case of the empty set, create items of the last sum without any movement.
while (historyItems.size() < count) {
historyItems.add(new HistoryItem(endSum));
calendar.add(intervalType, -1);
historyItems.add(new HistoryItem(endSum, calendar.getTimeInMillis() / 1000));
}

GsonBuilder gsonBuilder = new GsonBuilder();
Expand Down Expand Up @@ -238,6 +240,7 @@ public JsonElement serialize(HistoryItem historyItem, Type type, JsonSerializati
object.addProperty("high", historyItem.getHigh() / 100.0);
object.addProperty("low", historyItem.getLow() / 100.0);
object.addProperty("volume", historyItem.getVolume() / 100.0);
object.addProperty("timestamp", historyItem.getTimestamp());
return object;
}
}
8 changes: 7 additions & 1 deletion src/main/java/nl/utwente/ing/model/HistoryItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ public class HistoryItem {
private long high;
private long low;
private long volume;
private long timestamp;

public HistoryItem(long start) {
public HistoryItem(long start, long timestamp) {
this.open = start;
this.close = start;
this.high = start;
this.low = start;
this.timestamp = timestamp;
}

public long getOpen() {
Expand Down Expand Up @@ -73,4 +75,8 @@ public long getVolume() {
public void setVolume(long volume) {
this.volume = volume;
}

public long getTimestamp() {
return timestamp;
}
}

0 comments on commit 91d4ffd

Please sign in to comment.