Skip to content

Commit

Permalink
Merge pull request #224 from alcarraz/feature/add-affected-layers-to-…
Browse files Browse the repository at this point in the history
…gl-transaction

Convinient methods to make it easy to find out affected layers of a GL transaction
  • Loading branch information
ar authored Sep 2, 2021
2 parents c478704 + 0c07baa commit e2aeccc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
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");
}
}

0 comments on commit e2aeccc

Please sign in to comment.