Conversations/src/main/java/eu/siacs/conversations/entities/Presences.java

91 lines
2.1 KiB
Java
Raw Normal View History

2014-10-22 12:38:44 -04:00
package eu.siacs.conversations.entities;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
import eu.siacs.conversations.xml.Element;
public class Presences {
public static final int CHAT = -1;
public static final int ONLINE = 0;
public static final int AWAY = 1;
public static final int XA = 2;
public static final int DND = 3;
public static final int OFFLINE = 4;
private Hashtable<String, Integer> presences = new Hashtable<String, Integer>();
public Hashtable<String, Integer> getPresences() {
return this.presences;
}
public void updatePresence(String resource, int status) {
2014-11-13 08:44:19 -05:00
synchronized (this.presences) {
this.presences.put(resource, status);
}
2014-10-22 12:38:44 -04:00
}
public void removePresence(String resource) {
2014-11-13 08:44:19 -05:00
synchronized (this.presences) {
this.presences.remove(resource);
}
2014-10-22 12:38:44 -04:00
}
public void clearPresences() {
2014-11-13 08:44:19 -05:00
synchronized (this.presences) {
this.presences.clear();
}
2014-10-22 12:38:44 -04:00
}
public int getMostAvailableStatus() {
int status = OFFLINE;
2014-11-13 08:44:19 -05:00
synchronized (this.presences) {
Iterator<Entry<String, Integer>> it = presences.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Integer> entry = it.next();
if (entry.getValue() < status)
status = entry.getValue();
}
2014-10-22 12:38:44 -04:00
}
return status;
}
public static int parseShow(Element show) {
if ((show == null) || (show.getContent() == null)) {
return Presences.ONLINE;
} else if (show.getContent().equals("away")) {
return Presences.AWAY;
} else if (show.getContent().equals("xa")) {
return Presences.XA;
} else if (show.getContent().equals("chat")) {
return Presences.CHAT;
} else if (show.getContent().equals("dnd")) {
return Presences.DND;
} else {
return Presences.OFFLINE;
}
}
public int size() {
2014-11-13 08:44:19 -05:00
synchronized (this.presences) {
return presences.size();
}
2014-10-22 12:38:44 -04:00
}
public String[] asStringArray() {
2014-11-13 08:44:19 -05:00
synchronized (this.presences) {
final String[] presencesArray = new String[presences.size()];
presences.keySet().toArray(presencesArray);
return presencesArray;
}
2014-10-22 12:38:44 -04:00
}
public boolean has(String presence) {
2014-11-13 08:44:19 -05:00
synchronized (this.presences) {
return presences.containsKey(presence);
}
2014-10-22 12:38:44 -04:00
}
}