I have a Transaction class which looks like this:
public class Transaction {
private Transaction parentTransaction;
private long amount;
private String orderNumber;
}
So it has a field which points to another Transaction.
There is also a method like this:
public void build(Transaction transaction) {
final long amount = transaction.getParentTransaction() == null ? transaction.getAmount() : transaction.getParentTransaction().getAmount();
final String orderNumber = transaction.getParentTransaction() == null ? transaction.getOrderNumber() : transaction.getParentTransaction().getOrderNumber();
// same pattern follows for more fields...
// do other stuff...
}
Depending on whether a parentTransaction exists, e.g. the amount field shall be used from the parentTransaction or the transaction. What I don't like about this approach is the repetitive usage of the transaction.getParentTransaction() == null ? ... term.
Is there some pattern or apporach how I could avoid this?