Presences.java

 1package eu.siacs.conversations.entities;
 2
 3import java.util.Hashtable;
 4import java.util.Iterator;
 5import java.util.Map.Entry;
 6
 7import eu.siacs.conversations.xml.Element;
 8
 9public class Presences {
10
11	public static final int CHAT = -1;
12	public static final int ONLINE = 0;
13	public static final int AWAY = 1;
14	public static final int XA = 2;
15	public static final int DND = 3;
16	public static final int OFFLINE = 4;
17
18	private final Hashtable<String, Integer> presences = new Hashtable<>();
19
20	public Hashtable<String, Integer> getPresences() {
21		return this.presences;
22	}
23
24	public void updatePresence(String resource, int status) {
25		synchronized (this.presences) {
26			this.presences.put(resource, status);
27		}
28	}
29
30	public void removePresence(String resource) {
31		synchronized (this.presences) {
32			this.presences.remove(resource);
33		}
34	}
35
36	public void clearPresences() {
37		synchronized (this.presences) {
38			this.presences.clear();
39		}
40	}
41
42	public int getMostAvailableStatus() {
43		int status = OFFLINE;
44		synchronized (this.presences) {
45			Iterator<Entry<String, Integer>> it = presences.entrySet().iterator();
46			while (it.hasNext()) {
47				Entry<String, Integer> entry = it.next();
48				if (entry.getValue() < status)
49					status = entry.getValue();
50			}
51		}
52		return status;
53	}
54
55	public static int parseShow(Element show) {
56		if ((show == null) || (show.getContent() == null)) {
57			return Presences.ONLINE;
58		} else if (show.getContent().equals("away")) {
59			return Presences.AWAY;
60		} else if (show.getContent().equals("xa")) {
61			return Presences.XA;
62		} else if (show.getContent().equals("chat")) {
63			return Presences.CHAT;
64		} else if (show.getContent().equals("dnd")) {
65			return Presences.DND;
66		} else {
67			return Presences.OFFLINE;
68		}
69	}
70
71	public int size() {
72		synchronized (this.presences) {
73			return presences.size();
74		}
75	}
76
77	public String[] asStringArray() {
78		synchronized (this.presences) {
79			final String[] presencesArray = new String[presences.size()];
80			presences.keySet().toArray(presencesArray);
81			return presencesArray;
82		}
83	}
84
85	public boolean has(String presence) {
86		synchronized (this.presences) {
87			return presences.containsKey(presence);
88		}
89	}
90}