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.
*/
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);
}
/**

View File

@ -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 <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.
* 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 <tt>null</tt> 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;
}

View File

@ -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);

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;
}
}