Skip to content

Commit

Permalink
Ensure prices have been adjusted and remove need for the lock
Browse files Browse the repository at this point in the history
  • Loading branch information
ccavanaugh committed Aug 29, 2015
1 parent 64d4070 commit 7a0f219
Showing 1 changed file with 31 additions and 35 deletions.
66 changes: 31 additions & 35 deletions jgnash-core/src/main/java/jgnash/engine/SecurityNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,45 +311,41 @@ public List<SecurityHistoryNode> getHistoryNodes() {
*/
public List<List<SecurityHistoryNode>> getHistoryNodeGroupsBySplits() {

final List<List<SecurityHistoryNode>> groups = new ArrayList<>();
final List<SecurityHistoryEvent> splitEvents = getSplitEvents();

if (splitEvents.size() == 0) {
groups.add(getHistoryNodes());
} else { // count should be split events + 1 when complete
lock.readLock().lock();

try {
final ListIterator<SecurityHistoryEvent> historyEventIterator = splitEvents.listIterator();

LocalDate eventDate = historyEventIterator.next().getDate();

List<SecurityHistoryNode> group = new ArrayList<>();

for (int i = 0; i < sortedHistoryNodeCache.size(); i++) {
if (eventDate == null || sortedHistoryNodeCache.get(i).getLocalDate().isBefore(eventDate)) {
group.add(sortedHistoryNodeCache.get(i));
} else {
groups.add(group); // save the current group
group = new ArrayList<>(); // start a new group
group.add(sortedHistoryNodeCache.get(i - 1)); // create continuity with the previous group
group.add(sortedHistoryNodeCache.get(i));

if (historyEventIterator.hasNext()) {
eventDate = historyEventIterator.next().getDate();
} else {
eventDate = null;
}
}
final List<List<SecurityHistoryNode>> groups = new ArrayList<>();
final List<SecurityHistoryEvent> splitEvents = getSplitEvents();

if (splitEvents.size() == 0) {
groups.add(getHistoryNodes());
} else { // count should be split events + 1 when complete

// Create a defensive copy that has the adjustment multiplier set
final List<SecurityHistoryNode> securityHistoryNodes = getHistoryNodes();
final ListIterator<SecurityHistoryEvent> historyEventIterator = splitEvents.listIterator();

LocalDate eventDate = historyEventIterator.next().getDate();

List<SecurityHistoryNode> group = new ArrayList<>();

for (int i = 0; i < securityHistoryNodes.size(); i++) {
if (eventDate == null || securityHistoryNodes.get(i).getLocalDate().isBefore(eventDate)) {
group.add(securityHistoryNodes.get(i));
} else {
groups.add(group); // save the current group
group = new ArrayList<>(); // start a new group
group.add(securityHistoryNodes.get(i - 1)); // create continuity with the previous group
group.add(securityHistoryNodes.get(i)); // add the current node

if (historyEventIterator.hasNext()) {
eventDate = historyEventIterator.next().getDate();
} else {
eventDate = null;
}

groups.add(group); // add last group
} finally {
lock.readLock().unlock();
}
}
groups.add(group); // add last group
}

return groups;
return groups;
}

/**
Expand Down

0 comments on commit 7a0f219

Please sign in to comment.