📄 bsindividualpresences.java
字号:
package edu.ou.kmi.buddyspace.core;
/*
* BSIndividualPresences.java
*
* Project: BuddySpace
* (C) Copyright Knowledge Media Institute 2003
*
*
* Created on 10 November 2003, 12:24
*/
import java.util.*;
//import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.util.*;
//import org.jabber.jabberbeans.Extension.*;
/**
* <code>BSIndividualPresences</code> stores individually set presences
* different from overall presence. A different presence can be set for each JID.
* There are two policies for overriding the presence when set several times:
* (i) use better presence or (ii) use worse presence.
*
* @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
*/
public class BSIndividualPresences {
static final public int BETTER_TAKES_POLICY = 0;
static final public int WORSE_TAKES_POLICY = 1;
Hashtable resultPresences = null;
Hashtable oldResultPresences = null;
Hashtable groups = null;
Hashtable jids = null;
int policy = WORSE_TAKES_POLICY;
BSRosterBean rosterBean = null;
BSPresenceBean presenceBean = null;
/** Constructor */
public BSIndividualPresences(BSPresenceBean presenceBean) {
resultPresences = new Hashtable();
groups = new Hashtable();
jids = new Hashtable();
policy = WORSE_TAKES_POLICY;
this.presenceBean = presenceBean;
}
/** Sets roster bean for getting groups */
public void setRosterBean(BSRosterBean rosterBean) {
this.rosterBean = rosterBean;
}
/** Sets the policy of overriding */
public void setPolicy(int policy) {
this.policy = policy;
refreshPresences();
}
/** Returnss the policy of overriding */
public int getPolicy() {
return policy;
}
/** Sets presence for JID */
public void setPresence(JID jid, BSPresenceInfo pi) {
if (jid == null || pi == null) return;
//String jidStr = BSPresenceBean.getJIDHashString(jid, false);
JID key = jid.getSimpleJID();
BSIndividualPresenceItem ipi = new BSIndividualPresenceItem(jid, pi);
//jids.put(jidStr, ipi);
jids.put(key, ipi);
refreshPresences();
}
/** Sets presence for group */
public void setPresence(String groupName, BSPresenceInfo pi) {
if (groupName == null || pi == null) return;
groups.put(groupName, pi);
refreshPresences();
}
/** Resets individual presence for JID - no special presence will be sent */
public void clearPresence(JID jid) {
if (jid == null) return;
//String jidStr = BSPresenceBean.getJIDHashString(jid, false);
JID key = jid.getSimpleJID();
//jids.remove(jidStr);
jids.remove(key);
refreshPresences();
}
/** Resets individual presence for group - no special presence will be sent */
public void clearPresence(String groupName) {
if (groupName == null) return;
groups.remove(groupName);
refreshPresences();
}
/** Resets all individual presences - no special presences will be sent */
public void clearAll() {
jids.clear();
groups.clear();
refreshPresences();
}
/** Returns Enumeration of result individual presences for JIDs */
public Enumeration getItems() {
return resultPresences.elements();
}
/** Returns individual presence for JID after combining all */
public BSPresenceInfo getResultPresenceFor(JID jid) {
if (jid == null) return null;
//String jidStr = BSPresenceBean.getJIDHashString(jid, false);
JID key = jid.getSimpleJID();
//BSIndividualPresenceItem ipi = (BSIndividualPresenceItem) resultPresences.get(jidStr);
BSIndividualPresenceItem ipi = (BSIndividualPresenceItem) resultPresences.get(key);
return (ipi == null)? null : ipi.presence;
}
/** Returns individually set presence for JID - not combined with group settings */
public BSPresenceInfo getPresenceSettingFor(JID jid) {
if (jid == null) return null;
//String jidStr = BSPresenceBean.getJIDHashString(jid, false);
JID key = jid.getSimpleJID();
//BSIndividualPresenceItem ipi = (BSIndividualPresenceItem) jids.get(jidStr);
BSIndividualPresenceItem ipi = (BSIndividualPresenceItem) jids.get(key);
return (ipi == null)? null : ipi.presence;
}
/** Returns individually set presence for group - not combined with individual settings */
public BSPresenceInfo getPresenceSettingFor(String groupName) {
if (groupName == null) return null;
return (BSPresenceInfo) groups.get(groupName);
}
/** Recounts the resultant presences for all JIDs */
public void refreshPresences() {
oldResultPresences = resultPresences;
// clears result presences
resultPresences = new Hashtable();
// for all individual presences for JIDs
combineWithGroupPresences();
// combines the results with the overall presence
if (presenceBean != null)
combineWithOverallPresence(presenceBean.getMyPresence());
// for all individual presences for groups
combineWithJIDPresences();
// adds presences which need to be sent, because offline was sent previously
if (presenceBean != null)
combineWithPreviousOfflineFix(presenceBean.getMyPresence());
oldResultPresences = null;
}
/** Combines resultant presences according to settings for groups */
public void combineWithGroupPresences() {
// for all individual presences for groups
Enumeration groupEnum = groups.keys();
while (groupEnum.hasMoreElements()) {
String groupName = (String) groupEnum.nextElement();
BSPresenceInfo newPi = (BSPresenceInfo) groups.get(groupName);
if (newPi == null) continue;
maybeUpdateResultPresence(groupName, newPi);
}
}
/** Combines the resultant presences with overall presence. This changes
* presence for people who are in more groups and some of the groups don't
* have the individual presence set.
*/
protected void combineWithOverallPresence(BSPresenceInfo overallPresence) {
if (rosterBean == null || overallPresence == null) return;
// gets the jids wich are also in groups without presence set
Iterator allGroupsIter = rosterBean.getGroups();
HashSet jidsToCheck = new HashSet();
while (allGroupsIter.hasNext()) {
String g = (String) allGroupsIter.next();
// if the individual presence is not set for the group
if (!groups.keySet().contains(g)) {
Enumeration jidEnum = rosterBean.getJIDsInGroup(g);
while (jidEnum.hasMoreElements()) {
JID jid = (JID) jidEnum.nextElement();
//jidsToCheck.add(BSPresenceBean.getJIDHashString(jid, false));
jidsToCheck.add(jid.getSimpleJID());
}
}
}
// for all jids with individual presence
Enumeration setJidsEnum = resultPresences.keys();
while (setJidsEnum.hasMoreElements()) {
//String jidStr = (String) setJidsEnum.nextElement();
JID jid = (JID) setJidsEnum.nextElement();
// if it should be checked
//if (jidsToCheck.contains(jidStr)) {
if (jidsToCheck.contains(jid)) {
//BSIndividualPresenceItem ipi = (BSIndividualPresenceItem) resultPresences.get(jidStr);
BSIndividualPresenceItem ipi = (BSIndividualPresenceItem) resultPresences.get(jid);
// if the overall presence is stronger
if (isStrongerPresence(overallPresence, ipi.presence))
// cancels the individual setting
//resultPresences.remove(jidStr);
resultPresences.remove(jid);
}
}
}
/** Combines resultant presences according to settings for JIDs */
public void combineWithJIDPresences() {
// for all individual presences for JIDs
Enumeration jidEnum = jids.elements();
while (jidEnum.hasMoreElements()) {
BSIndividualPresenceItem newIpi = (BSIndividualPresenceItem) jidEnum.nextElement();
if (newIpi.jid == null || newIpi.presence == null) continue;
maybeUpdateResultPresence(newIpi.jid, newIpi.presence);
}
}
/** Combines resultant presences with presences needed to be sent because of
* previous offline for the JID */
protected void combineWithPreviousOfflineFix(BSPresenceInfo overallPresence) {
if (oldResultPresences == null) return;
// for all old individual presences
Enumeration ipiEnum = oldResultPresences.elements();
while (ipiEnum.hasMoreElements()) {
BSIndividualPresenceItem ipi = (BSIndividualPresenceItem) ipiEnum.nextElement();
// if that was offline
if (!ipi.presence.isOnline()) {
//String jidStr = BSPresenceBean.getJIDHashString(ipi.jid, false);
JID jid = ipi.jid.getSimpleJID();
//BSIndividualPresenceItem newIpi = (BSIndividualPresenceItem) resultPresences.get(jidStr);
BSIndividualPresenceItem newIpi = (BSIndividualPresenceItem) resultPresences.get(jid);
// if there's no individual presence for it now
if (newIpi == null) {
// adds the overall one there
newIpi = new BSIndividualPresenceItem(ipi.jid, overallPresence);
//resultPresences.put(jidStr, newIpi);
resultPresences.put(jid, newIpi);
}
}
}
}
/** If given presence is stronger, updates the resultant presence for jid */
protected void maybeUpdateResultPresence(String groupName, BSPresenceInfo pi) {
if (rosterBean == null || pi == null) return;
Enumeration jidEnum = rosterBean.getJIDsInGroup(groupName);
while (jidEnum.hasMoreElements()) {
JID jid = (JID) jidEnum.nextElement();
maybeUpdateResultPresence(jid, pi);
}
}
/** If given presence is stronger, updates the resultant presence for jid */
protected void maybeUpdateResultPresence(JID jid, BSPresenceInfo pi) {
if (jid == null || pi == null) return;
BSPresenceInfo oldPi = getResultPresenceFor(jid);
if (isStrongerPresence(pi, oldPi)) {
//String jidStr = BSPresenceBean.getJIDHashString(jid, false);
JID key = jid.getSimpleJID();
BSIndividualPresenceItem ipi = new BSIndividualPresenceItem(jid, pi);
//resultPresences.put(jidStr, ipi);
resultPresences.put(key, ipi);
}
}
/** Returns if the first presence is stronger accoring to current policy - if equal returns false. */
protected boolean isStrongerPresence(BSPresenceInfo first, BSPresenceInfo second) {
if (first == null) return false;
if (second == null) return true;
// if first is stronger
if (policy == BETTER_TAKES_POLICY && first.isBetterPresence(second))
return true;
if (policy == WORSE_TAKES_POLICY && second.isBetterPresence(first))
return true;
// else second is stronger or equal
return false;
}
/** Represents individual presence and its receiver JID */
public static class BSIndividualPresenceItem {
public BSPresenceInfo presence = null;
public JID jid = null;
public BSIndividualPresenceItem(JID jid, BSPresenceInfo pi) {
presence = pi;
this.jid = jid;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -