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