Users.java 1.22 KB
Newer Older
Gabriele Civitarese's avatar
Gabriele Civitarese committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package beans;

/**
 * Created by civi on 26/04/16.
 */
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType (XmlAccessType.FIELD)
public class Users {

    @XmlElement(name="users")
    private List<User> userslist;
Gabriele Civitarese's avatar
Gabriele Civitarese committed
20

Gabriele Civitarese's avatar
Gabriele Civitarese committed
21 22
    private static Users instance;

Gabriele Civitarese's avatar
Gabriele Civitarese committed
23
    private Users() {
Gabriele Civitarese's avatar
Gabriele Civitarese committed
24 25 26 27 28 29 30 31 32 33 34
        userslist = new ArrayList<User>();
    }

    //singleton
    public synchronized static Users getInstance(){
        if(instance==null)
            instance = new Users();
        return instance;
    }

    public synchronized List<User> getUserslist() {
Gabriele Civitarese's avatar
Gabriele Civitarese committed
35

Gabriele Civitarese's avatar
Gabriele Civitarese committed
36
        return new ArrayList<>(userslist);
Gabriele Civitarese's avatar
Gabriele Civitarese committed
37

Gabriele Civitarese's avatar
Gabriele Civitarese committed
38 39
    }

Gabriele Civitarese's avatar
Gabriele Civitarese committed
40
    public synchronized void setUserslist(List<User> userslist) {
Gabriele Civitarese's avatar
Gabriele Civitarese committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        this.userslist = userslist;
    }

    public synchronized void  add(User u){
        userslist.add(u);
    }

    public User getByName(String name){

        List<User> usersCopy = getUserslist();

        for(User u: usersCopy)
            if(u.getName().toLowerCase().equals(name.toLowerCase()))
                return u;
        return null;
    }

}