mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-26 14:02:06 +01:00
Add StanzaIdUtil
This commit is contained in:
parent
bf9fb7d2d9
commit
293f90c6c6
2 changed files with 41 additions and 14 deletions
|
@ -17,8 +17,8 @@
|
||||||
|
|
||||||
package org.jivesoftware.smack.packet;
|
package org.jivesoftware.smack.packet;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.packet.id.StanzaIdUtil;
|
||||||
import org.jivesoftware.smack.util.PacketUtil;
|
import org.jivesoftware.smack.util.PacketUtil;
|
||||||
import org.jivesoftware.smack.util.StringUtils;
|
|
||||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||||
import org.jxmpp.util.XmppStringUtils;
|
import org.jxmpp.util.XmppStringUtils;
|
||||||
|
|
||||||
|
@ -29,7 +29,6 @@ import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for XMPP Stanzas, which are called packets in Smack.
|
* 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 =
|
protected static final String DEFAULT_LANGUAGE =
|
||||||
java.util.Locale.getDefault().getLanguage().toLowerCase(Locale.US);
|
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<String, PacketExtension> packetExtensions = new LinkedHashMap<String, PacketExtension>(12);
|
private final Map<String, PacketExtension> packetExtensions = new LinkedHashMap<String, PacketExtension>(12);
|
||||||
|
|
||||||
private String packetID = null;
|
private String packetID = null;
|
||||||
|
@ -82,7 +70,7 @@ public abstract class Packet implements TopLevelStreamElement {
|
||||||
protected String language;
|
protected String language;
|
||||||
|
|
||||||
public Packet() {
|
public Packet() {
|
||||||
this(prefix + Long.toString(id.incrementAndGet()));
|
this(StanzaIdUtil.newStanzaId());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Packet(String packetID) {
|
public Packet(String packetID) {
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue