package ru.job4j.multithreading.users;

import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;

import java.util.HashMap;
import java.util.Map;

@ThreadSafe
public class UserStorage {

    @GuardedBy("storage")
    private final Map<Integer, User> storage = new HashMap<>();

    public boolean add (User user) {
        synchronized(storage) {
            boolean isAdd = false;
            if (!storage.containsKey(user.getId())) {
                storage.put(user.getId(), user);
                isAdd = true;
            }
            return isAdd;
        }
    }

    public boolean update(User user) {
        synchronized (storage) {
            boolean isUpdate = false;
            if (storage.containsKey(user.getId())) {
                User prevUser = storage.get(user.getId());
                prevUser.setAmount(user.getAmount() + prevUser.getAmount());
                storage.put(prevUser.getId(), prevUser);
                isUpdate = true;
            }
            return isUpdate;
        }
    }

    public boolean delete(User user) {
        synchronized (storage) {
            boolean isDelete = false;
            if (storage.containsKey(user.getId())) {
                storage.remove(user.getId());
                isDelete = true;
            }
            return isDelete;
        }
    }

    public boolean transfer(int fromId, int toId, int amount) {
        synchronized (storage) {
            boolean isTransfer = false;
            if (storage.containsKey(fromId) && storage.containsKey(toId)) {
                User from = storage.get(fromId);
                User to = storage.get(toId);
                if (from.getAmount() >= amount) {
                    synchronized (from) {
                        synchronized (to) {
                            from.setAmount(from.getAmount() - amount);
                            storage.put(fromId, from);
                            to.setAmount(to.getAmount() + amount);
                            storage.put(toId, to);
                            isTransfer = true;
                        }
                    }
                }

            }
            return isTransfer;
        }
    }
}

@@ -1699,102 +1699,8 @@
 ) %7B%0A
-                    synchronized (from) %7B%0A                        synchronized (to) %7B%0A        
     
@@ -1774,32 +1774,24 @@
             
-        
 storage.put(
@@ -1825,24 +1825,16 @@
         
-       
- 
 to.setAm
@@ -1880,32 +1880,24 @@
             
-        
 storage.put(
@@ -1927,24 +1927,16 @@
         
-  
-      
 isTransf
@@ -1950,56 +1950,8 @@
 ue;%0A
-                        %7D%0A                    %7D%0A
     

task 1104