diff --git a/modules/minigl/src/main/java/org/jpos/gl/GLTransaction.java b/modules/minigl/src/main/java/org/jpos/gl/GLTransaction.java index 3b41e26e24..d244d3045b 100644 --- a/modules/minigl/src/main/java/org/jpos/gl/GLTransaction.java +++ b/modules/minigl/src/main/java/org/jpos/gl/GLTransaction.java @@ -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; @@ -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 layer are selected + * @param layers entries with layer layer are selected * @return a reversal transaction */ public GLTransaction createReverse(boolean keepEntryTags, short... layers) { @@ -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 getAffectedLayers(Predicate... predicates) { + Stream entryStream = getEntries().stream(); + for (Predicate p : predicates) entryStream = entryStream.filter(p); + return entryStream.map(GLEntry::getLayer).collect(Collectors.toSet()); + } + + public Set getAffectedLayers(Collection accounts) { + Objects.requireNonNull(accounts); + return getAffectedLayers(e -> accounts.contains(e.getAccount())); + } + + public Set getAffectedLayers(Account ... accounts) { + return getAffectedLayers(Arrays.asList(accounts)); + } + } diff --git a/modules/minigl/src/test/java/org/jpos/gl/GLTransactionTest.java b/modules/minigl/src/test/java/org/jpos/gl/GLTransactionTest.java index 192e5a1b44..93cbb9e211 100644 --- a/modules/minigl/src/test/java/org/jpos/gl/GLTransactionTest.java +++ b/modules/minigl/src/test/java/org/jpos/gl/GLTransactionTest.java @@ -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{ @@ -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 affectedLayers = txn.getAffectedLayers(bobEquity); + Set 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"); + } } \ No newline at end of file