From 293f90c6c6cfac0e82d33694f2c82ddb9433eaa2 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 19 Jan 2015 16:17:03 +0100 Subject: [PATCH] Add StanzaIdUtil --- .../org/jivesoftware/smack/packet/Packet.java | 16 +------- .../smack/packet/id/StanzaIdUtil.java | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/packet/id/StanzaIdUtil.java 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 ae26d6524..c7eb84d1a 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 @@ -17,8 +17,8 @@ package org.jivesoftware.smack.packet; +import org.jivesoftware.smack.packet.id.StanzaIdUtil; import org.jivesoftware.smack.util.PacketUtil; -import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.XmlStringBuilder; import org.jxmpp.util.XmppStringUtils; @@ -29,7 +29,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.concurrent.atomic.AtomicLong; /** * Base class for XMPP Stanzas, which are called packets in Smack. @@ -51,17 +50,6 @@ public abstract class Packet implements TopLevelStreamElement { protected static final String DEFAULT_LANGUAGE = java.util.Locale.getDefault().getLanguage().toLowerCase(Locale.US); - /** - * A prefix helps to make sure that ID's are unique across multiple instances. - */ - private static final String prefix = StringUtils.randomString(5) + "-"; - - /** - * Keeps track of the current increment, which is appended to the prefix to - * forum a unique ID. - */ - private static final AtomicLong id = new AtomicLong(); - private final Map packetExtensions = new LinkedHashMap(12); private String packetID = null; @@ -82,7 +70,7 @@ public abstract class Packet implements TopLevelStreamElement { protected String language; public Packet() { - this(prefix + Long.toString(id.incrementAndGet())); + this(StanzaIdUtil.newStanzaId()); } public Packet(String packetID) { diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/id/StanzaIdUtil.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/id/StanzaIdUtil.java new file mode 100644 index 000000000..b13a877d3 --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/id/StanzaIdUtil.java @@ -0,0 +1,39 @@ +/** + * + * Copyright 2003-2007 Jive Software, 2015 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smack.packet.id; + +import java.util.concurrent.atomic.AtomicLong; + +import org.jivesoftware.smack.util.StringUtils; + +public class StanzaIdUtil { + + /** + * A prefix helps to make sure that ID's are unique across multiple instances. + */ + private static final String PREFIX = StringUtils.randomString(5) + "-"; + + /** + * Keeps track of the current increment, which is appended to the prefix to + * forum a unique ID. + */ + private static final AtomicLong ID = new AtomicLong(); + + public static String newStanzaId() { + return PREFIX + Long.toString(ID.incrementAndGet()); + } +}