diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java index da829b9be..e4e9238c3 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java @@ -60,6 +60,7 @@ public class Message extends Packet { * Creates a new, "normal" message. */ public Message() { + super(); } /** @@ -68,6 +69,7 @@ public class Message extends Packet { * @param to the recipient of the message. */ public Message(String to) { + super(); setTo(to); } @@ -78,11 +80,9 @@ public class Message extends Packet { * @param type the message type. */ public Message(String to, Type type) { + super(); setTo(to); - - if (type != null) { - this.type = type; - } + setType(type); } /** diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Packet.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Packet.java index 4e02fceca..04676d078 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Packet.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Packet.java @@ -26,6 +26,7 @@ import java.util.Collections; import java.util.List; import java.util.Locale; 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 @@ -41,14 +42,7 @@ public abstract class Packet { private static String DEFAULT_XML_NS = null; /** - * Constant used as packetID to indicate that a packet has no id. To indicate that a packet - * has no id set this constant as the packet's id. When the packet is asked for its id the - * answer will be null. - */ - public static final String ID_NOT_AVAILABLE = "ID_NOT_AVAILABLE"; - - /** - * A prefix helps to make sure that ID's are unique across mutliple instances. + * A prefix helps to make sure that ID's are unique across multiple instances. */ 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 * forum a unique ID. */ - private static long id = 0; + private static AtomicLong id = new AtomicLong(); 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) { DEFAULT_XML_NS = defaultXmlns; } @@ -83,6 +67,11 @@ public abstract class Packet { private XMPPError error = null; public Packet() { + this(prefix + Long.toString(id.incrementAndGet())); + } + + public Packet(String packetID) { + setPacketID(packetID); } public Packet(Packet p) { @@ -105,13 +94,6 @@ public abstract class Packet { * @return the packet's unique ID or null if the packet's id is not available. */ public String getPacketID() { - if (ID_NOT_AVAILABLE.equals(packetID)) { - return null; - } - - if (packetID == null) { - packetID = nextID(); - } return packetID; } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java index fcf7bcea6..03538808d 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java @@ -69,6 +69,7 @@ public class Presence extends Packet { * @param type the type. */ public Presence(Type type) { + super(); setType(type); } @@ -81,6 +82,7 @@ public class Presence extends Packet { * @param mode the mode type for this presence update. */ public Presence(Type type, String status, int priority, Mode mode) { + super(); setType(type); setStatus(status); setPriority(priority); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java index 2c8cd7744..64125e00b 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java @@ -160,8 +160,7 @@ public class PacketParserUtils { */ public static Message parseMessage(XmlPullParser parser) throws Exception { Message message = new Message(); - String id = parser.getAttributeValue("", "id"); - message.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id); + message.setPacketID(parser.getAttributeValue("", "id")); message.setTo(parser.getAttributeValue("", "to")); message.setFrom(parser.getAttributeValue("", "from")); String typeString = parser.getAttributeValue("", "type"); @@ -408,14 +407,12 @@ public class PacketParserUtils { Presence presence = new Presence(type); presence.setTo(parser.getAttributeValue("", "to")); presence.setFrom(parser.getAttributeValue("", "from")); - String id = parser.getAttributeValue("", "id"); - presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id); + presence.setPacketID(parser.getAttributeValue("", "id")); String language = getLanguageAttribute(parser); if (language != null && !"".equals(language.trim())) { presence.setLanguage(language); } - presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id); // Parse sub-elements boolean done = false; diff --git a/smack-core/src/test/java/org/jivesoftware/smack/packet/MessageTest.java b/smack-core/src/test/java/org/jivesoftware/smack/packet/MessageTest.java index dab1d67ce..8867b65b3 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/packet/MessageTest.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/packet/MessageTest.java @@ -52,7 +52,7 @@ public class MessageTest { String control = controlBuilder.toString(); Message messageTypeInConstructor = new Message(null, Message.Type.chat); - messageTypeInConstructor.setPacketID(Packet.ID_NOT_AVAILABLE); + messageTypeInConstructor.setPacketID(null); assertEquals(type, messageTypeInConstructor.getType()); assertXMLEqual(control, messageTypeInConstructor.toXML().toString()); @@ -257,7 +257,7 @@ public class MessageTest { private static Message getNewMessage() { Message message = new Message(); - message.setPacketID(Packet.ID_NOT_AVAILABLE); + message.setPacketID(null); return message; } } diff --git a/smack-core/src/test/java/org/jivesoftware/smack/packet/PresenceTest.java b/smack-core/src/test/java/org/jivesoftware/smack/packet/PresenceTest.java index 5985096ab..4c7f14b51 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/packet/PresenceTest.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/packet/PresenceTest.java @@ -47,7 +47,7 @@ public class PresenceTest { String control = controlBuilder.toString(); Presence presenceTypeInConstructor = new Presence(type); - presenceTypeInConstructor.setPacketID(Packet.ID_NOT_AVAILABLE); + presenceTypeInConstructor.setPacketID(null); assertEquals(type, presenceTypeInConstructor.getType()); assertXMLEqual(control, presenceTypeInConstructor.toXML().toString()); @@ -146,7 +146,7 @@ public class PresenceTest { Presence presenceModeInConstructor = new Presence(Presence.Type.available, status, priority, mode1); - presenceModeInConstructor.setPacketID(Packet.ID_NOT_AVAILABLE); + presenceModeInConstructor.setPacketID(null); assertEquals(mode1, presenceModeInConstructor.getMode()); assertXMLEqual(control, presenceModeInConstructor.toXML().toString()); @@ -194,7 +194,7 @@ public class PresenceTest { private static Presence getNewPresence() { Presence presence = new Presence(Presence.Type.available); - presence.setPacketID(Packet.ID_NOT_AVAILABLE); + presence.setPacketID(null); return presence; } }