Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Convinient methods to make it easy to find out affected layers of a GL transaction #224

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions modules/minigl/src/main/java/org/jpos/gl/GLTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

package org.jpos.gl;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.ArrayList;
import java.util.*;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jdom2.Element;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
Expand Down Expand Up @@ -325,7 +326,7 @@ public GLTransaction createReverse(boolean keepEntryTags) {
* Create a reverse transaction based on this one
*
* @param keepEntryTags if true entries tags are copied to the reversal entries
* @param layer entries with layer <code>layer</code> are selected
* @param layers entries with layer <code>layer</code> are selected
* @return a reversal transaction
*/
public GLTransaction createReverse(boolean keepEntryTags, short... layers) {
Expand Down Expand Up @@ -534,5 +535,27 @@ public GLTransaction clone() throws CloneNotSupportedException {
}
return glt;
}

/**
* Handy method to obtain affected layers considering only entries for which all predicates are true
* @param predicates Predicates that the inputs have to satisfy, if none, then consider all entries
* @return layers affected by entries of this transaction.
*/
@SafeVarargs
public final Set<Short> getAffectedLayers(Predicate<GLEntry>... predicates) {
Stream<GLEntry> entryStream = getEntries().stream();
for (Predicate<GLEntry> p : predicates) entryStream = entryStream.filter(p);
return entryStream.map(GLEntry::getLayer).collect(Collectors.toSet());
}

public Set<Short> getAffectedLayers(Collection<Account> accounts) {
Objects.requireNonNull(accounts);
return getAffectedLayers(e -> accounts.contains(e.getAccount()));
}

public Set<Short> getAffectedLayers(Account ... accounts) {
return getAffectedLayers(Arrays.asList(accounts));
}

}

16 changes: 16 additions & 0 deletions modules/minigl/src/test/java/org/jpos/gl/GLTransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.Set;

class GLTransactionTest extends TestBase{

Expand Down Expand Up @@ -86,4 +88,18 @@ void testCreateReverseWithTagsKeepTags() {
Assertions.assertEquals(entry.getTags(), new Tags(TEST_TAG, CREDIT_TAG));
}
}

@Test
void testGetAffectedLayers() {
GLTransaction txn = new GLTransaction();
txn.createCredit(bobEquity, AMOUNT, null, LAYER);
txn.createDebit(aliceEquity, AMOUNT, null, LAYER);
Set<Short> affectedLayers = txn.getAffectedLayers(bobEquity);
Set<Short> expected = Collections.singleton(LAYER);
Assertions.assertEquals(expected, affectedLayers, "Affected layers did not match for bob");
affectedLayers = txn.getAffectedLayers(aliceEquity);
Assertions.assertEquals(expected, affectedLayers, "Affected layers did not match for alice");
affectedLayers = txn.getAffectedLayers(aliceEquity, bobEquity);
Assertions.assertEquals(expected, affectedLayers, "Affected layers did not match for both");
}
}