mirror of
https://github.com/SimplexDevelopment/FreedomNetworkSuite.git
synced 2024-11-14 05:03:33 +00:00
Add mutable & immutable transaction interfaces and implementations, replacing copy()
This commit is contained in:
parent
95df78da6f
commit
036ddf3e04
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
package me.totalfreedom.economy;
|
||||
|
||||
public interface CompletedTransaction
|
||||
public interface CompletedTransaction extends Transaction
|
||||
{
|
||||
Transaction getTransaction();
|
||||
|
||||
TransactionResult getResult();
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -2,5 +2,5 @@ package me.totalfreedom.economy;
|
||||
|
||||
public interface Transactor
|
||||
{
|
||||
CompletedTransaction handleTransaction(Transaction transaction);
|
||||
CompletedTransaction handleTransaction(MutableTransaction transaction);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user