Skip to content

Commit

Permalink
Add transaction predicate map and support classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sp4ce-cowboy committed Oct 28, 2023
1 parent da4b795 commit 4b4ea49
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 50 deletions.
13 changes: 13 additions & 0 deletions src/main/java/unicash/commons/enums/TransactionProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package unicash.commons.enums;

/**
* An enum of all available transaction properties.
*/
public enum TransactionProperty {
AMOUNT,
CATEGORY,
DATETIME,
LOCATION,
NAME,
TYPE;
}
55 changes: 55 additions & 0 deletions src/main/java/unicash/commons/util/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package unicash.commons.util;

/**
* A Generic Pair class for utility purposes.
* @param <T> type parameter of the first type of {@code Object} to be contained
* @param <U> type parameter of the second type of {@code Object} to be contained
*/
public class Pair<T,U> {
private T t;
private U u;

/**
* Creates the pair object.
* @param t The first object
* @param u The second object
*/
public Pair(T t, U u) {
this.t = t;
this.u = u;
}

/**
* Returns the first object.
*/
public T getFirst() {
return t;
}

/**
* Returns the second object
*/
public U getSecond() {
return u;
}

/**
* Sets the first object.
*/
public void setFirst(T t) {
this.t = t;
}

/**
* Sets the second object.
*/
public void setSecond() {
this.u = u;
}

@Override
public String toString() {
return "[" + t + ", " + u + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package unicash.model.transaction.predicates;

import unicash.commons.util.Pair;
import unicash.model.transaction.Transaction;

import java.util.function.Predicate;

/**
* A wrapper class for containing Boolean and TransactionPredicate pairs.
*/
public class BooleanPredicatePair
extends Pair<Boolean, Predicate<Transaction>> {

/**
* Creates the transaction predicate pair object.
*
* @param isActive The boolean indicating the status of the predicate
* @param transactionPredicate The transaction property predicate
*/
public BooleanPredicatePair(Boolean isActive, Predicate<Transaction> transactionPredicate) {
super(isActive, transactionPredicate);
}

/**
* Creates the transaction predicate pair object with default false value
*
* @param transactionPredicate The transaction property predicate
*/
public BooleanPredicatePair(Predicate<Transaction> transactionPredicate) {
super(false, transactionPredicate);
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package unicash.model.transaction.predicates;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.function.Predicate;

import unicash.commons.enums.TransactionProperty;
import unicash.commons.util.ToStringBuilder;
import unicash.model.transaction.Transaction;




/**
* Tests that any of a {@code Transactions}'s properties matches all the keywords given.
* Matching in this context either means a partial match of the keyword with the query, or
Expand All @@ -24,45 +24,39 @@ public class TransactionContainsAllKeywordsPredicate
public static final String EMPTY_STRING = "";
public static final List<String> EMPTY_STRING_LIST = List.of(EMPTY_STRING);

private TransactionAmountContainsValuePredicate amountPredicate;
private boolean amountPredicateExists;

private TransactionCategoryContainsKeywordsPredicate categoryPredicate;
private TransactionDateTimeContainsValuePredicate dateTimePredicate;
private TransactionLocationContainsKeywordsPredicate locationPredicate;
private TransactionNameContainsKeywordsPredicate namePredicate;
private boolean namePredicateExists;

private TransactionTypeContainsValuePredicate typePredicate;
private Predicate<Transaction> composedTransactionPredicate;

public HashMap<TransactionProperty, BooleanPredicatePair>
predicatePairMap = new HashMap<>();

/**
* Creates a composed predicate object.
*/
public TransactionContainsAllKeywordsPredicate() {
amountPredicate = new TransactionAmountContainsValuePredicate(EMPTY_STRING_LIST);
amountPredicateExists = false;

categoryPredicate = new TransactionCategoryContainsKeywordsPredicate(EMPTY_STRING_LIST);
dateTimePredicate = new TransactionDateTimeContainsValuePredicate(EMPTY_STRING_LIST);
locationPredicate = new TransactionLocationContainsKeywordsPredicate(EMPTY_STRING_LIST);

namePredicate = new TransactionNameContainsKeywordsPredicate(EMPTY_STRING_LIST);
namePredicateExists = false;

typePredicate = new TransactionTypeContainsValuePredicate(EMPTY_STRING_LIST);
composedTransactionPredicate = amountPredicate
.and(categoryPredicate)
.and(dateTimePredicate)
.and(locationPredicate)
.and(namePredicate)
.and(typePredicate);

TransactionAmountContainsValuePredicate amountPredicate =
new TransactionAmountContainsValuePredicate(EMPTY_STRING_LIST);
TransactionCategoryContainsKeywordsPredicate categoryPredicate =
new TransactionCategoryContainsKeywordsPredicate(EMPTY_STRING_LIST);
TransactionDateTimeContainsValuePredicate dateTimePredicate =
new TransactionDateTimeContainsValuePredicate(EMPTY_STRING_LIST);
TransactionLocationContainsKeywordsPredicate locationPredicate =
new TransactionLocationContainsKeywordsPredicate(EMPTY_STRING_LIST);
TransactionNameContainsKeywordsPredicate namePredicate =
new TransactionNameContainsKeywordsPredicate(EMPTY_STRING_LIST);
TransactionTypeContainsValuePredicate typePredicate =
new TransactionTypeContainsValuePredicate(EMPTY_STRING_LIST);

predicatePairMap.put(TransactionProperty.AMOUNT, new BooleanPredicatePair(amountPredicate));
predicatePairMap.put(TransactionProperty.CATEGORY, new BooleanPredicatePair(categoryPredicate));
predicatePairMap.put(TransactionProperty.DATETIME, new BooleanPredicatePair(dateTimePredicate));
predicatePairMap.put(TransactionProperty.LOCATION, new BooleanPredicatePair(locationPredicate));
predicatePairMap.put(TransactionProperty.NAME, new BooleanPredicatePair(namePredicate));
predicatePairMap.put(TransactionProperty.TYPE, new BooleanPredicatePair(typePredicate));

}

@Override
public boolean test(Transaction transaction) {
Predicate<Transaction> composedPredicate = composeAllPredicates();
Predicate<Transaction> composedPredicate = composeAllActivePredicates();
return composedPredicate.test(transaction);

}
Expand All @@ -72,28 +66,35 @@ public boolean test(Transaction transaction) {
*
* @return a composed predicate
*/
public Predicate<Transaction> composeAllPredicates() {
public Predicate<Transaction> composeAllActivePredicates() {
Predicate<Transaction> composedPredicate = unused -> true;

if (amountPredicateExists) {
composedPredicate = composedPredicate.and(amountPredicate);
}
for (TransactionProperty property : predicatePairMap.keySet()) {
Boolean predicateState = predicatePairMap.get(property).getFirst();
Predicate<Transaction> predicate = predicatePairMap.get(property).getSecond();

if (namePredicateExists) {
composedPredicate = composedPredicate.and(namePredicate);
if (predicateState) {
composedPredicate = composedPredicate.and(predicate);
}
}

return composedPredicate;
}

public void setAmount(String amount) {
amountPredicate = new TransactionAmountContainsValuePredicate(List.of(amount));
amountPredicateExists = true;
BooleanPredicatePair predicatePair = new BooleanPredicatePair(true,
new TransactionAmountContainsValuePredicate(
Collections.singletonList(amount)));

predicatePairMap.put(TransactionProperty.AMOUNT, predicatePair);
}

public void setName(String name) {
namePredicate = new TransactionNameContainsKeywordsPredicate(List.of(name));
namePredicateExists = true;
BooleanPredicatePair predicatePair = new BooleanPredicatePair(true,
new TransactionNameContainsKeywordsPredicate(
Collections.singletonList(name)));

predicatePairMap.put(TransactionProperty.NAME, predicatePair);
}


Expand All @@ -111,19 +112,15 @@ public boolean equals(Object other) {
TransactionContainsAllKeywordsPredicate otherContainsKeywordsPredicate =
(TransactionContainsAllKeywordsPredicate) other;

return amountPredicate.equals(otherContainsKeywordsPredicate.amountPredicate)
&& namePredicate.equals(otherContainsKeywordsPredicate.namePredicate)
&& composedTransactionPredicate.equals(otherContainsKeywordsPredicate
.composedTransactionPredicate);
return predicatePairMap.equals(
otherContainsKeywordsPredicate.predicatePairMap);

}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("composedTransactionPredicate", composedTransactionPredicate)
.add("amountPredicate", amountPredicate)
.add("namePredicate", namePredicate)
.add("predicatePairMap", predicatePairMap)
.toString();
}
}

0 comments on commit 4b4ea49

Please sign in to comment.