diff --git a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleCompletedTransaction.java b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleCompletedTransaction.java index 9080019..b7ba257 100644 --- a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleCompletedTransaction.java +++ b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleCompletedTransaction.java @@ -1,30 +1,47 @@ package me.totalfreedom.fossil.economy; import me.totalfreedom.economy.CompletedTransaction; +import me.totalfreedom.economy.EconomicEntity; import me.totalfreedom.economy.Transaction; import me.totalfreedom.economy.TransactionResult; public class SimpleCompletedTransaction implements CompletedTransaction { - private final Transaction transaction; private final TransactionResult transactionResult; + private final EconomicEntity source; + private final EconomicEntity destination; + private final long balance; public SimpleCompletedTransaction(Transaction transaction, TransactionResult transactionResult) { - this.transaction = transaction.copy(); + + this.source = transaction.getSource(); + this.destination = transaction.getDestination(); + this.balance = transaction.getBalance(); this.transactionResult = transactionResult; } - - @Override - public Transaction getTransaction() - { - return transaction.copy(); - } - @Override public TransactionResult getResult() { return transactionResult; } + + @Override + public EconomicEntity getSource() + { + return source; + } + + @Override + public EconomicEntity getDestination() + { + return destination; + } + + @Override + public long getBalance() + { + return balance; + } } diff --git a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleLoggedTransactor.java b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleLoggedTransactor.java index 4354cea..4bf35ca 100644 --- a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleLoggedTransactor.java +++ b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleLoggedTransactor.java @@ -1,7 +1,7 @@ package me.totalfreedom.fossil.economy; import me.totalfreedom.economy.CompletedTransaction; -import me.totalfreedom.economy.Transaction; +import me.totalfreedom.economy.MutableTransaction; import me.totalfreedom.economy.TransactionLogger; import me.totalfreedom.economy.Transactor; @@ -22,7 +22,7 @@ public class SimpleLoggedTransactor implements Transactor } @Override - public CompletedTransaction handleTransaction(Transaction transaction) + public CompletedTransaction handleTransaction(MutableTransaction transaction) { CompletedTransaction completedTransaction = transactor.handleTransaction(transaction); diff --git a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleMutableTransaction.java b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleMutableTransaction.java new file mode 100644 index 0000000..c99cb65 --- /dev/null +++ b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleMutableTransaction.java @@ -0,0 +1,30 @@ +package me.totalfreedom.fossil.economy; + +import me.totalfreedom.economy.EconomicEntity; +import me.totalfreedom.economy.MutableTransaction; + +public class SimpleMutableTransaction extends SimpleTransaction implements MutableTransaction +{ + public SimpleMutableTransaction(EconomicEntity source, EconomicEntity destination, long balance) + { + super(source, destination, balance); + } + + @Override + public long addToBalance(long amount) + { + return balance.addAndGet(amount); + } + + @Override + public long removeFromBalance(long amount) + { + return this.addToBalance(-amount); + } + + @Override + public void setBalance(long newBalance) + { + balance.set(newBalance); + } +} diff --git a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransaction.java b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransaction.java index 958634f..4e3e008 100644 --- a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransaction.java +++ b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransaction.java @@ -9,7 +9,7 @@ public class SimpleTransaction implements Transaction { private final EconomicEntity source; private final EconomicEntity destination; - private final AtomicLong balance; + protected final AtomicLong balance; public SimpleTransaction(EconomicEntity source, EconomicEntity destination, long balance) { @@ -18,12 +18,6 @@ public class SimpleTransaction implements Transaction this.balance = new AtomicLong(balance); } - @Override - public Transaction copy() - { - return new SimpleTransaction(source, destination, balance.get()); - } - @Override public EconomicEntity getSource() { @@ -41,22 +35,4 @@ public class SimpleTransaction implements Transaction { return balance.get(); } - - @Override - public long addToBalance(long amount) - { - return balance.addAndGet(amount); - } - - @Override - public long removeFromBalance(long amount) - { - return this.addToBalance(-amount); - } - - @Override - public void setBalance(long newBalance) - { - balance.set(newBalance); - } } diff --git a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactor.java b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactor.java index 913ded3..735571e 100644 --- a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactor.java +++ b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactor.java @@ -5,9 +5,8 @@ import me.totalfreedom.economy.*; public class SimpleTransactor implements Transactor { @Override - public CompletedTransaction handleTransaction(Transaction transaction) + public CompletedTransaction handleTransaction(MutableTransaction transaction) { - Transaction transactionCopy = transaction.copy(); EconomicEntity source = transaction.getSource(); EconomicEntityData sourceData = source.getEconomicData(); @@ -28,7 +27,7 @@ public class SimpleTransactor implements Transactor if (diff > 0) { - return new SimpleCompletedTransaction(transactionCopy, SimpleTransactionResult.INSUFFICIENT_FUNDS); + return new SimpleCompletedTransaction(transaction, SimpleTransactionResult.INSUFFICIENT_FUNDS); } EconomicEntity destination = transaction.getDestination(); @@ -42,6 +41,6 @@ public class SimpleTransactor implements Transactor sourceData.removeFromBalance(transactionAmount); destinationData.addToBalance(transactionAmount); - return new SimpleCompletedTransaction(transactionCopy, SimpleTransactionResult.SUCCESSFUL); + return new SimpleCompletedTransaction(transaction, SimpleTransactionResult.SUCCESSFUL); } } diff --git a/Patchwork/src/main/java/me/totalfreedom/economy/CompletedTransaction.java b/Patchwork/src/main/java/me/totalfreedom/economy/CompletedTransaction.java index 5c67cfa..7b58dfd 100644 --- a/Patchwork/src/main/java/me/totalfreedom/economy/CompletedTransaction.java +++ b/Patchwork/src/main/java/me/totalfreedom/economy/CompletedTransaction.java @@ -1,8 +1,7 @@ package me.totalfreedom.economy; -public interface CompletedTransaction +public interface CompletedTransaction extends Transaction { - Transaction getTransaction(); TransactionResult getResult(); } diff --git a/Patchwork/src/main/java/me/totalfreedom/economy/MutableTransaction.java b/Patchwork/src/main/java/me/totalfreedom/economy/MutableTransaction.java new file mode 100644 index 0000000..8548fa7 --- /dev/null +++ b/Patchwork/src/main/java/me/totalfreedom/economy/MutableTransaction.java @@ -0,0 +1,13 @@ +package me.totalfreedom.economy; + +/** + * Please ensure that all modifications of {@link MutableTransaction} happen BEFORE it is passed to a {@link Transactor} implementation + */ +public interface MutableTransaction extends Transaction +{ + long addToBalance(final long amount); + + long removeFromBalance(final long amount); + + void setBalance(final long newBalance); +} diff --git a/Patchwork/src/main/java/me/totalfreedom/economy/Transaction.java b/Patchwork/src/main/java/me/totalfreedom/economy/Transaction.java index 3413976..eb0e310 100644 --- a/Patchwork/src/main/java/me/totalfreedom/economy/Transaction.java +++ b/Patchwork/src/main/java/me/totalfreedom/economy/Transaction.java @@ -2,17 +2,10 @@ package me.totalfreedom.economy; public interface Transaction { - Transaction copy(); - EconomicEntity getSource(); EconomicEntity getDestination(); long getBalance(); - long addToBalance(final long amount); - - long removeFromBalance(final long amount); - - void setBalance(final long newBalance); } diff --git a/Patchwork/src/main/java/me/totalfreedom/economy/Transactor.java b/Patchwork/src/main/java/me/totalfreedom/economy/Transactor.java index be078ba..8fd9c47 100644 --- a/Patchwork/src/main/java/me/totalfreedom/economy/Transactor.java +++ b/Patchwork/src/main/java/me/totalfreedom/economy/Transactor.java @@ -2,5 +2,5 @@ package me.totalfreedom.economy; public interface Transactor { - CompletedTransaction handleTransaction(Transaction transaction); + CompletedTransaction handleTransaction(MutableTransaction transaction); }