Skip to content

Commit

Permalink
Fixed NPE when updating security prices and no latest security price …
Browse files Browse the repository at this point in the history
  • Loading branch information
buchen authored and chrisaut committed Jun 12, 2020
1 parent 4245e56 commit 49f6c58
Showing 1 changed file with 22 additions and 11 deletions.
Expand Up @@ -334,12 +334,13 @@ public boolean addPrice(SecurityPrice price)
{
return addPrice(price, true);
}

/**
* Adds security price to historical quotes.
*
* @param overwriteExisting is used to decide on whether to keep or overwrite existing prices
*
* @param overwriteExisting
* is used to decide on whether to keep or overwrite existing
* prices
* @return true if the historical quote was updated.
*/
public boolean addPrice(SecurityPrice price, boolean overwriteExisting)
Expand All @@ -356,11 +357,10 @@ public boolean addPrice(SecurityPrice price, boolean overwriteExisting)
else
{
SecurityPrice replaced = prices.get(index);

// different prices are replaced only, if the source is manual, csv
// or html import, the value is 0.0 or it is the last given price
if (!replaced.equals(price) && (overwriteExisting || replaced.getValue() == 0.0
|| replaced.getDate().equals(latest.getDate())))
// or html import, the value is 0.0
if (!replaced.equals(price) && (overwriteExisting || replaced.getValue() == 0.0))
{
// only replace if necessary -> UI might keep reference!
prices.set(index, price);
Expand All @@ -372,20 +372,31 @@ public boolean addPrice(SecurityPrice price, boolean overwriteExisting)
}
}
}


/**
* Adds all prices to the list of prices unless a security price for that
* date already exists. However, the last historical date is overwritten as
* some quote provider include the latest security price in the list of
* historical prices.
*/
public boolean addAllPrices(List<SecurityPrice> prices)
{
if (prices.isEmpty())
return false;

LocalDate now = LocalDate.now();


LocalDate last = null;
if (!prices.isEmpty())
last = prices.get(prices.size() - 1).getDate();

boolean isUpdated = false;
for (SecurityPrice p : prices)
{
if (p.getDate().isBefore(now))
{
boolean isAdded = addPrice(p, false);
boolean doOverwrite = p.getDate().equals(last);
boolean isAdded = addPrice(p, doOverwrite);
isUpdated = isUpdated || isAdded;
}
}
Expand Down

0 comments on commit 49f6c58

Please sign in to comment.