Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. package fgbank;
  2.  
  3. import java.util.concurrent.locks.Lock;
  4. import java.util.concurrent.locks.ReentrantLock;
  5.  
  6. /**
  7. * Bank implementation.
  8. *
  9. * <p>:TODO: This implementation has to be made thread-safe.
  10. */
  11. public class BankImpl implements Bank {
  12. /**
  13. * An array of accounts by index.
  14. */
  15. private final Account[] accounts;
  16.  
  17. /**
  18. * Creates new bank instance.
  19. * @param n the number of accounts (numbered from 0 to n-1).
  20. */
  21. public BankImpl(int n) {
  22. accounts = new Account[n];
  23. for (int i = 0; i < n; i++) {
  24. accounts[i] = new Account();
  25. }
  26. }
  27.  
  28. /**
  29. * {@inheritDoc}
  30. */
  31. @Override
  32. public int getNumberOfAccounts() {
  33. return accounts.length;
  34. }
  35.  
  36. /**
  37. * {@inheritDoc}
  38. * <p>:TODO: This method has to be made thread-safe.
  39. */
  40. @Override
  41. public long getAmount(int index) {
  42. long res = -1;
  43. accounts[index].lock.lock();
  44. res = accounts[index].amount;
  45. accounts[index].lock.unlock();
  46. return res;
  47. }
  48.  
  49. /**
  50. * {@inheritDoc}
  51. * <p>:TODO: This method has to be made thread-safe.
  52. */
  53. @Override
  54. public long getTotalAmount() {
  55. long sum = 0;
  56. for (Account account : accounts) {
  57. account.lock.lock();
  58. }
  59. for (Account account : accounts) {
  60. sum += account.amount;
  61. }
  62. for (Account account : accounts) {
  63. account.lock.unlock();
  64. }
  65. return sum;
  66. }
  67.  
  68. /**
  69. * {@inheritDoc}
  70. * <p>:TODO: This method has to be made thread-safe.
  71. */
  72. @Override
  73. public long deposit(int index, long amount) {
  74. try {
  75. accounts[index].lock.lock();
  76. if (amount <= 0) {
  77. throw new IllegalArgumentException("Invalid amount: " + amount);
  78. }
  79. if (amount > MAX_AMOUNT || accounts[index].amount + amount > MAX_AMOUNT) {
  80. throw new IllegalStateException("Overflow");
  81. }
  82. accounts[index].amount += amount;
  83. return accounts[index].amount;
  84. } catch (IllegalArgumentException | IllegalStateException ex) {
  85. throw ex;
  86. }
  87. finally {
  88. accounts[index].lock.unlock();
  89. }
  90. }
  91.  
  92. /**
  93. * {@inheritDoc}
  94. * <p>:TODO: This method has to be made thread-safe.
  95. */
  96. @Override
  97. public long withdraw(int index, long amount) {
  98. try {
  99. accounts[index].lock.lock();
  100. if (amount <= 0) {
  101. throw new IllegalArgumentException("Invalid amount: " + amount);
  102. }
  103. if (accounts[index].amount - amount < 0) {
  104. throw new IllegalStateException("Underflow");
  105. }
  106. accounts[index].amount -= amount;
  107. return accounts[index].amount;
  108. } catch (IllegalArgumentException | IllegalStateException ex) {
  109. throw ex;
  110. } finally {
  111. accounts[index].lock.unlock();
  112. }
  113. }
  114.  
  115. /**
  116. * {@inheritDoc}
  117. * <p>:TODO: This method has to be made thread-safe.
  118. */
  119. @Override
  120. public void transfer(int fromIndex, int toIndex, long amount) {
  121. int minInd = Math.min(fromIndex, toIndex);
  122. int maxInd = Math.max(fromIndex, toIndex);
  123. try {
  124. accounts[minInd].lock.lock();
  125. accounts[maxInd].lock.lock();
  126. if (amount <= 0) {
  127. throw new IllegalArgumentException("Invalid amount: " + amount);
  128. }
  129. if (fromIndex == toIndex) {
  130. throw new IllegalArgumentException("fromIndex == toIndex");
  131. }
  132. if (amount > accounts[fromIndex].amount) {
  133. throw new IllegalStateException("Underflow");
  134. }
  135. else if (amount > MAX_AMOUNT || accounts[toIndex].amount + amount > MAX_AMOUNT) {
  136. throw new IllegalStateException("Overflow");
  137. }
  138. accounts[fromIndex].amount -= amount;
  139. accounts[toIndex].amount += amount;
  140. } catch (IllegalArgumentException | IllegalStateException ex) {
  141. throw ex;
  142. } finally {
  143. accounts[minInd].lock.unlock();
  144. accounts[maxInd].lock.unlock();
  145. }
  146. }
  147.  
  148. /**
  149. * Private account data structure.
  150. */
  151. private static class Account {
  152. /**
  153. * Amount of funds in this account.
  154. */
  155. long amount;
  156. Lock lock = new ReentrantLock();
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement