package ru.job4j.servlets.UserStore;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

public class MemoryStore implements Store {

    private static MemoryStore _instance = null;

    public MemoryStore() {}

    public static synchronized MemoryStore getInstance() {
        if (_instance == null)
            _instance = new MemoryStore();
        return _instance;
    }
    private AtomicInteger id = new AtomicInteger(0);
    public  List<User> userStore = new CopyOnWriteArrayList<User>();

    @Override
    public void add(User user) {
        user.setId(id.getAndIncrement());
        userStore.add(user);
    }

    @Override
    public void update(int id, User user) {
        for (int i = 0; i < userStore.size(); i++) {
            if (id == userStore.get(i).getId()) {
                userStore.set(i, user);
                break;
            }
        }
    }

    @Override
    public void delete(int id) {
        for (int i = 0; i < userStore.size(); i++) {
            if (id == userStore.get(i).getId()) {
                userStore.remove(i);
                break;
            }
        }
    }

    @Override
    public List<User> findAll() {
        return userStore;
    }

    @Override
    public User findById(int id) {
        User user = new User();
        for (User u : this.userStore) {
            if (id == u.getId()) {
                user = u;
                break;
            }
        }
        return user;
    }
}

@@ -23,13 +23,13 @@
 ets.
-UserS
+users
 tore
@@ -231,17 +231,16 @@
 ryStore 
-_
 instance
@@ -249,16 +249,139 @@
  null;%0A%0A
+    private AtomicInteger id = new AtomicInteger(0);%0A    public  List%3CUser%3E userStore = new CopyOnWriteArrayList%3CUser%3E();%0A%0A
     publ
@@ -402,176 +402,453 @@
 () %7B
+%0A%0A    
 %7D%0A%0A    
-public static synchronized MemoryStore getInstance() %7B%0A        if (_instance == null)%0A            _instance = new MemoryStore();%0A        return _instance
+@Override%0A    public boolean contain(User user) %7B%0A        boolean result = false;%0A        for (int i = 0; i %3C userStore.size(); i++) %7B%0A            if (user.getLogin().equals(userStore.get(i)) %7C%7C user.getEmail().equals(userStore.get(i)) %7C%7C user.getName().isEmpty() %7C%7C user.getLogin().isEmpty() %7C%7C user.getEmail().isEmpty()) %7B%0A                result = true;%0A                break;%0A            %7D%0A        %7D%0A        return result
 ;%0A    %7D%0A
     
@@ -847,129 +847,183 @@
   %7D%0A
+%0A
     p
-rivate AtomicInteger id = new AtomicInteger(0);%0A    public  List%3CUser%3E userStore = new CopyOnWriteArrayList%3CUser%3E();
+ublic static synchronized MemoryStore getInstance() %7B%0A        if (instance == null) %7B%0A            instance = new MemoryStore();%0A        %7D%0A        return instance;%0A    %7D%0A
 %0A%0A  

task #2516#100751