1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-27 00:32:07 +01:00

Fixed possible NPE. SMACK-229

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@8451 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Alex Wenckus 2007-06-04 22:59:20 +00:00 committed by alex
parent c5ab2829d4
commit 5d01a7faf0

View file

@ -44,10 +44,10 @@ import java.util.WeakHashMap;
*/ */
public class ChatStateManager { public class ChatStateManager {
private static Map<XMPPConnection, ChatStateManager> managers = private static final Map<XMPPConnection, ChatStateManager> managers =
new WeakHashMap<XMPPConnection, ChatStateManager>(); new WeakHashMap<XMPPConnection, ChatStateManager>();
private static PacketFilter filter = new NotFilter( private static final PacketFilter filter = new NotFilter(
new PacketExtensionFilter("http://jabber.org/protocol/chatstates")); new PacketExtensionFilter("http://jabber.org/protocol/chatstates"));
/** /**
@ -58,7 +58,10 @@ public class ChatStateManager {
* @return the ChatStateManager related the the connection. * @return the ChatStateManager related the the connection.
*/ */
public static ChatStateManager getInstance(final XMPPConnection connection) { public static ChatStateManager getInstance(final XMPPConnection connection) {
synchronized (connection) { if(connection == null) {
return null;
}
synchronized (managers) {
ChatStateManager manager = managers.get(connection); ChatStateManager manager = managers.get(connection);
if (manager == null) { if (manager == null) {
manager = new ChatStateManager(connection); manager = new ChatStateManager(connection);
@ -70,17 +73,17 @@ public class ChatStateManager {
} }
} }
private XMPPConnection connection; private final XMPPConnection connection;
private OutgoingMessageInterceptor outgoingInterceptor = new OutgoingMessageInterceptor(); private final OutgoingMessageInterceptor outgoingInterceptor = new OutgoingMessageInterceptor();
private IncomingMessageInterceptor incomingInterceptor = new IncomingMessageInterceptor(); private final IncomingMessageInterceptor incomingInterceptor = new IncomingMessageInterceptor();
/** /**
* Maps chat to last chat state. * Maps chat to last chat state.
*/ */
private Map<Chat, ChatState> chatStates = new ReferenceMap<Chat, ChatState>(ReferenceMap.WEAK, private final Map<Chat, ChatState> chatStates =
ReferenceMap.HARD); new ReferenceMap<Chat, ChatState>(ReferenceMap.WEAK, ReferenceMap.HARD);
private ChatStateManager(XMPPConnection connection) { private ChatStateManager(XMPPConnection connection) {
this.connection = connection; this.connection = connection;
@ -97,7 +100,8 @@ public class ChatStateManager {
/** /**
* Sets the current state of the provided chat. This method will send an empty bodied Message * Sets the current state of the provided chat. This method will send an empty bodied Message
* packet with the state attached as a {@link org.jivesoftware.smack.packet.PacketExtension}. * packet with the state attached as a {@link org.jivesoftware.smack.packet.PacketExtension}, if
* and only if the new chat state is different than the last state.
* *
* @param newState the new state of the chat * @param newState the new state of the chat
* @param chat the chat. * @param chat the chat.
@ -106,6 +110,9 @@ public class ChatStateManager {
* packet. * packet.
*/ */
public void setCurrentState(ChatState newState, Chat chat) throws XMPPException { public void setCurrentState(ChatState newState, Chat chat) throws XMPPException {
if(chat == null || newState == null) {
throw new IllegalArgumentException("Arguments cannot be null.");
}
if(!updateChatState(chat, newState)) { if(!updateChatState(chat, newState)) {
return; return;
} }
@ -116,6 +123,21 @@ public class ChatStateManager {
chat.sendMessage(message); chat.sendMessage(message);
} }
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChatStateManager that = (ChatStateManager) o;
return connection.equals(that.connection);
}
public int hashCode() {
return connection.hashCode();
}
private boolean updateChatState(Chat chat, ChatState newState) { private boolean updateChatState(Chat chat, ChatState newState) {
ChatState lastChatState = chatStates.get(chat); ChatState lastChatState = chatStates.get(chat);
if (lastChatState == null || lastChatState != newState) { if (lastChatState == null || lastChatState != newState) {