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 Hashtable<String, Integer> presences = new Hashtable<String, Integer>();
19
20	public Hashtable<String, Integer> getPresences() {
21		return this.presences;
22	}
23
24	public void updatePresence(String resource, int status) {
25		this.presences.put(resource, status);
26	}
27
28	public void removePresence(String resource) {
29		this.presences.remove(resource);
30	}
31
32	public void clearPresences() {
33		this.presences.clear();
34	}
35
36	public int getMostAvailableStatus() {
37		int status = OFFLINE;
38		Iterator<Entry<String, Integer>> it = presences.entrySet().iterator();
39		while (it.hasNext()) {
40			Entry<String, Integer> entry = it.next();
41			if (entry.getValue() < status)
42				status = entry.getValue();
43		}
44		return status;
45	}
46
47	public static int parseShow(Element show) {
48		if ((show == null) || (show.getContent() == null)) {
49			return Presences.ONLINE;
50		} else if (show.getContent().equals("away")) {
51			return Presences.AWAY;
52		} else if (show.getContent().equals("xa")) {
53			return Presences.XA;
54		} else if (show.getContent().equals("chat")) {
55			return Presences.CHAT;
56		} else if (show.getContent().equals("dnd")) {
57			return Presences.DND;
58		} else {
59			return Presences.OFFLINE;
60		}
61	}
62
63	public int size() {
64		return presences.size();
65	}
66
67	public String[] asStringArray() {
68		final String[] presencesArray = new String[presences.size()];
69		presences.keySet().toArray(presencesArray);
70		return presencesArray;
71	}
72
73	public boolean has(String presence) {
74		return presences.containsKey(presence);
75	}
76}