no longer use ConcurrentHashMap in favor of synchronize on a final HashMap

This commit is contained in:
Daniel Gultsch 2015-01-04 00:14:40 +01:00
parent 4f4eff2353
commit f338d89753
1 changed files with 27 additions and 19 deletions

View File

@ -1,14 +1,14 @@
package eu.siacs.conversations.entities; package eu.siacs.conversations.entities;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
public class Roster { public class Roster {
final Account account; final Account account;
final ConcurrentHashMap<String, Contact> contacts = new ConcurrentHashMap<>(); final HashMap<String, Contact> contacts = new HashMap<>();
private String version = null; private String version = null;
public Roster(Account account) { public Roster(Account account) {
@ -19,25 +19,29 @@ public class Roster {
if (jid == null) { if (jid == null) {
return null; return null;
} }
final Contact contact = contacts.get(jid.toBareJid().toString()); synchronized (this.contacts) {
Contact contact = contacts.get(jid.toBareJid().toString());
if (contact != null && contact.showInRoster()) { if (contact != null && contact.showInRoster()) {
return contact; return contact;
} else { } else {
return null; return null;
} }
} }
}
public Contact getContact(final Jid jid) { public Contact getContact(final Jid jid) {
synchronized (this.contacts) {
final Jid bareJid = jid.toBareJid(); final Jid bareJid = jid.toBareJid();
if (contacts.containsKey(bareJid.toString())) { if (contacts.containsKey(bareJid.toString())) {
return contacts.get(bareJid.toString()); return contacts.get(bareJid.toString());
} else { } else {
final Contact contact = new Contact(bareJid); Contact contact = new Contact(bareJid);
contact.setAccount(account); contact.setAccount(account);
contacts.put(bareJid.toString(), contact); contacts.put(bareJid.toString(), contact);
return contact; return contact;
} }
} }
}
public void clearPresences() { public void clearPresences() {
for (Contact contact : getContacts()) { for (Contact contact : getContacts()) {
@ -46,13 +50,13 @@ public class Roster {
} }
public void markAllAsNotInRoster() { public void markAllAsNotInRoster() {
for (final Contact contact : getContacts()) { for (Contact contact : getContacts()) {
contact.resetOption(Contact.Options.IN_ROSTER); contact.resetOption(Contact.Options.IN_ROSTER);
} }
} }
public void clearSystemAccounts() { public void clearSystemAccounts() {
for (final Contact contact : getContacts()) { for (Contact contact : getContacts()) {
contact.setPhotoUri(null); contact.setPhotoUri(null);
contact.setSystemName(null); contact.setSystemName(null);
contact.setSystemAccount(null); contact.setSystemAccount(null);
@ -60,14 +64,18 @@ public class Roster {
} }
public List<Contact> getContacts() { public List<Contact> getContacts() {
synchronized (this.contacts) {
return new ArrayList<>(this.contacts.values()); return new ArrayList<>(this.contacts.values());
} }
}
public void initContact(final Contact contact) { public void initContact(final Contact contact) {
contact.setAccount(account); contact.setAccount(account);
contact.setOption(Contact.Options.IN_ROSTER); contact.setOption(Contact.Options.IN_ROSTER);
synchronized (this.contacts) {
contacts.put(contact.getJid().toBareJid().toString(), contact); contacts.put(contact.getJid().toBareJid().toString(), contact);
} }
}
public void setVersion(String version) { public void setVersion(String version) {
this.version = version; this.version = version;