Roster.java

 1package eu.siacs.conversations.entities;
 2
 3import java.util.ArrayList;
 4import java.util.HashMap;
 5import java.util.Iterator;
 6import java.util.List;
 7
 8import rocks.xmpp.addr.Jid;
 9
10
11public class Roster {
12	final Account account;
13	final HashMap<Jid, Contact> contacts = new HashMap<>();
14	private String version = null;
15
16	public Roster(Account account) {
17		this.account = account;
18	}
19
20	public Contact getContactFromRoster(Jid jid) {
21		if (jid == null) {
22			return null;
23		}
24		synchronized (this.contacts) {
25			Contact contact = contacts.get(jid.asBareJid());
26			if (contact != null && contact.showInRoster()) {
27				return contact;
28			} else {
29				return null;
30			}
31		}
32	}
33
34	public Contact getContact(final Jid jid) {
35		synchronized (this.contacts) {
36			if (!contacts.containsKey(jid.asBareJid())) {
37				Contact contact = new Contact(jid.asBareJid());
38				contact.setAccount(account);
39				contacts.put(contact.getJid().asBareJid(), contact);
40				return contact;
41			}
42			return contacts.get(jid.asBareJid());
43		}
44	}
45
46	public void clearPresences() {
47		for (Contact contact : getContacts()) {
48			contact.clearPresences();
49		}
50	}
51
52	public void markAllAsNotInRoster() {
53		for (Contact contact : getContacts()) {
54			contact.resetOption(Contact.Options.IN_ROSTER);
55		}
56	}
57
58	public List<Contact> getWithSystemAccounts() {
59		List<Contact> with = getContacts();
60		for(Iterator<Contact> iterator = with.iterator(); iterator.hasNext();) {
61			Contact contact = iterator.next();
62			if (contact.getSystemAccount() == null) {
63				iterator.remove();
64			}
65		}
66		return with;
67	}
68
69	public List<Contact> getContacts() {
70		synchronized (this.contacts) {
71			return new ArrayList<>(this.contacts.values());
72		}
73	}
74
75	public void initContact(final Contact contact) {
76		if (contact == null) {
77			return;
78		}
79		contact.setAccount(account);
80		synchronized (this.contacts) {
81			contacts.put(contact.getJid().asBareJid(), contact);
82		}
83	}
84
85	public void setVersion(String version) {
86		this.version = version;
87	}
88
89	public String getVersion() {
90		return this.version;
91	}
92
93	public Account getAccount() {
94		return this.account;
95	}
96}