Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private inner class TransferOp(val fromIndex: Int, val toIndex: Int, val amount: Long) : Op() {
- var errorMessage: String? = null
- override fun invokeOperation() {
- // todo: write implementation for this method, use TotalAmountOp as an example
- /*
- * In the implementation of this operation only two accounts (with fromIndex and toIndex) needs
- * to be acquired. Unlike TotalAmountOp, this operation has its own result in errorMessage string,
- * and it must also update AcquiredAccount.newAmount fields before setting completed to true
- * and invoking release on those acquired accounts.
- *
- * Basically, implementation of this method must perform the logic of the following code "atomically":
- */
- val from: AcquiredAccount?
- val to: AcquiredAccount?
- if (fromIndex < toIndex) {
- from = acquire(fromIndex, this)
- to = acquire(toIndex, this)
- } else {
- to = acquire(toIndex, this)
- from = acquire(fromIndex, this)
- }
- if (from != null && to != null) {
- if (amount > from.amount)
- errorMessage = "Underflow"
- else if (to.amount + amount > MAX_AMOUNT)
- errorMessage = "Overflow"
- else {
- from.newAmount = from.amount - amount
- to.newAmount = to.amount + amount
- }
- completed = true
- }
- if (fromIndex < toIndex) {
- release(fromIndex, this)
- release(toIndex, this)
- } else {
- release(toIndex, this)
- release(fromIndex, this)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement