Remove Packet.ID_NOT_AVAILABLE

This commit is contained in:
Florian Schmaus 2014-07-06 23:57:06 +02:00
parent f671b9e781
commit 8b16c49a31
6 changed files with 21 additions and 40 deletions

View File

@ -60,6 +60,7 @@ public class Message extends Packet {
* Creates a new, "normal" message. * Creates a new, "normal" message.
*/ */
public Message() { public Message() {
super();
} }
/** /**
@ -68,6 +69,7 @@ public class Message extends Packet {
* @param to the recipient of the message. * @param to the recipient of the message.
*/ */
public Message(String to) { public Message(String to) {
super();
setTo(to); setTo(to);
} }
@ -78,11 +80,9 @@ public class Message extends Packet {
* @param type the message type. * @param type the message type.
*/ */
public Message(String to, Type type) { public Message(String to, Type type) {
super();
setTo(to); setTo(to);
setType(type);
if (type != null) {
this.type = type;
}
} }
/** /**

View File

@ -26,6 +26,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicLong;
/** /**
* Base class for XMPP packets. Every packet has a unique ID (which is automatically * Base class for XMPP packets. Every packet has a unique ID (which is automatically
@ -41,14 +42,7 @@ public abstract class Packet {
private static String DEFAULT_XML_NS = null; private static String DEFAULT_XML_NS = null;
/** /**
* Constant used as packetID to indicate that a packet has no id. To indicate that a packet * A prefix helps to make sure that ID's are unique across multiple instances.
* has no id set this constant as the packet's id. When the packet is asked for its id the
* answer will be <tt>null</tt>.
*/
public static final String ID_NOT_AVAILABLE = "ID_NOT_AVAILABLE";
/**
* A prefix helps to make sure that ID's are unique across mutliple instances.
*/ */
private static String prefix = StringUtils.randomString(5) + "-"; private static String prefix = StringUtils.randomString(5) + "-";
@ -56,20 +50,10 @@ public abstract class Packet {
* Keeps track of the current increment, which is appended to the prefix to * Keeps track of the current increment, which is appended to the prefix to
* forum a unique ID. * forum a unique ID.
*/ */
private static long id = 0; private static AtomicLong id = new AtomicLong();
private String xmlns = DEFAULT_XML_NS; private String xmlns = DEFAULT_XML_NS;
/**
* Returns the next unique id. Each id made up of a short alphanumeric
* prefix along with a unique numeric value.
*
* @return the next id.
*/
public static synchronized String nextID() {
return prefix + Long.toString(id++);
}
public static void setDefaultXmlns(String defaultXmlns) { public static void setDefaultXmlns(String defaultXmlns) {
DEFAULT_XML_NS = defaultXmlns; DEFAULT_XML_NS = defaultXmlns;
} }
@ -83,6 +67,11 @@ public abstract class Packet {
private XMPPError error = null; private XMPPError error = null;
public Packet() { public Packet() {
this(prefix + Long.toString(id.incrementAndGet()));
}
public Packet(String packetID) {
setPacketID(packetID);
} }
public Packet(Packet p) { public Packet(Packet p) {
@ -105,13 +94,6 @@ public abstract class Packet {
* @return the packet's unique ID or <tt>null</tt> if the packet's id is not available. * @return the packet's unique ID or <tt>null</tt> if the packet's id is not available.
*/ */
public String getPacketID() { public String getPacketID() {
if (ID_NOT_AVAILABLE.equals(packetID)) {
return null;
}
if (packetID == null) {
packetID = nextID();
}
return packetID; return packetID;
} }

View File

@ -69,6 +69,7 @@ public class Presence extends Packet {
* @param type the type. * @param type the type.
*/ */
public Presence(Type type) { public Presence(Type type) {
super();
setType(type); setType(type);
} }
@ -81,6 +82,7 @@ public class Presence extends Packet {
* @param mode the mode type for this presence update. * @param mode the mode type for this presence update.
*/ */
public Presence(Type type, String status, int priority, Mode mode) { public Presence(Type type, String status, int priority, Mode mode) {
super();
setType(type); setType(type);
setStatus(status); setStatus(status);
setPriority(priority); setPriority(priority);

View File

@ -160,8 +160,7 @@ public class PacketParserUtils {
*/ */
public static Message parseMessage(XmlPullParser parser) throws Exception { public static Message parseMessage(XmlPullParser parser) throws Exception {
Message message = new Message(); Message message = new Message();
String id = parser.getAttributeValue("", "id"); message.setPacketID(parser.getAttributeValue("", "id"));
message.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
message.setTo(parser.getAttributeValue("", "to")); message.setTo(parser.getAttributeValue("", "to"));
message.setFrom(parser.getAttributeValue("", "from")); message.setFrom(parser.getAttributeValue("", "from"));
String typeString = parser.getAttributeValue("", "type"); String typeString = parser.getAttributeValue("", "type");
@ -408,14 +407,12 @@ public class PacketParserUtils {
Presence presence = new Presence(type); Presence presence = new Presence(type);
presence.setTo(parser.getAttributeValue("", "to")); presence.setTo(parser.getAttributeValue("", "to"));
presence.setFrom(parser.getAttributeValue("", "from")); presence.setFrom(parser.getAttributeValue("", "from"));
String id = parser.getAttributeValue("", "id"); presence.setPacketID(parser.getAttributeValue("", "id"));
presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
String language = getLanguageAttribute(parser); String language = getLanguageAttribute(parser);
if (language != null && !"".equals(language.trim())) { if (language != null && !"".equals(language.trim())) {
presence.setLanguage(language); presence.setLanguage(language);
} }
presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
// Parse sub-elements // Parse sub-elements
boolean done = false; boolean done = false;

View File

@ -52,7 +52,7 @@ public class MessageTest {
String control = controlBuilder.toString(); String control = controlBuilder.toString();
Message messageTypeInConstructor = new Message(null, Message.Type.chat); Message messageTypeInConstructor = new Message(null, Message.Type.chat);
messageTypeInConstructor.setPacketID(Packet.ID_NOT_AVAILABLE); messageTypeInConstructor.setPacketID(null);
assertEquals(type, messageTypeInConstructor.getType()); assertEquals(type, messageTypeInConstructor.getType());
assertXMLEqual(control, messageTypeInConstructor.toXML().toString()); assertXMLEqual(control, messageTypeInConstructor.toXML().toString());
@ -257,7 +257,7 @@ public class MessageTest {
private static Message getNewMessage() { private static Message getNewMessage() {
Message message = new Message(); Message message = new Message();
message.setPacketID(Packet.ID_NOT_AVAILABLE); message.setPacketID(null);
return message; return message;
} }
} }

View File

@ -47,7 +47,7 @@ public class PresenceTest {
String control = controlBuilder.toString(); String control = controlBuilder.toString();
Presence presenceTypeInConstructor = new Presence(type); Presence presenceTypeInConstructor = new Presence(type);
presenceTypeInConstructor.setPacketID(Packet.ID_NOT_AVAILABLE); presenceTypeInConstructor.setPacketID(null);
assertEquals(type, presenceTypeInConstructor.getType()); assertEquals(type, presenceTypeInConstructor.getType());
assertXMLEqual(control, presenceTypeInConstructor.toXML().toString()); assertXMLEqual(control, presenceTypeInConstructor.toXML().toString());
@ -146,7 +146,7 @@ public class PresenceTest {
Presence presenceModeInConstructor = new Presence(Presence.Type.available, status, priority, Presence presenceModeInConstructor = new Presence(Presence.Type.available, status, priority,
mode1); mode1);
presenceModeInConstructor.setPacketID(Packet.ID_NOT_AVAILABLE); presenceModeInConstructor.setPacketID(null);
assertEquals(mode1, presenceModeInConstructor.getMode()); assertEquals(mode1, presenceModeInConstructor.getMode());
assertXMLEqual(control, presenceModeInConstructor.toXML().toString()); assertXMLEqual(control, presenceModeInConstructor.toXML().toString());
@ -194,7 +194,7 @@ public class PresenceTest {
private static Presence getNewPresence() { private static Presence getNewPresence() {
Presence presence = new Presence(Presence.Type.available); Presence presence = new Presence(Presence.Type.available);
presence.setPacketID(Packet.ID_NOT_AVAILABLE); presence.setPacketID(null);
return presence; return presence;
} }
} }