1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-26 05:52:06 +01:00

SMACK-361 Some general code cleanup added some missing hashcode methods and added back some removed public API methods (marked as deprecated).

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_0@13598 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
rcollier 2013-03-31 14:24:50 +00:00
parent 58f56ee31b
commit 0fdfd6e75e
12 changed files with 84 additions and 43 deletions

View file

@ -29,7 +29,7 @@ import java.io.IOException;
*/ */
public class Base32Encoder implements StringEncoder { public class Base32Encoder implements StringEncoder {
private static Base32Encoder instance; private static Base32Encoder instance = new Base32Encoder();
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ2345678"; private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ2345678";
private Base32Encoder() { private Base32Encoder() {
@ -37,9 +37,6 @@ public class Base32Encoder implements StringEncoder {
} }
public static Base32Encoder getInstance() { public static Base32Encoder getInstance() {
if (instance == null) {
instance = new Base32Encoder();
}
return instance; return instance;
} }

View file

@ -20,16 +20,13 @@ package org.jivesoftware.smack.util;
*/ */
public class Base64Encoder implements StringEncoder { public class Base64Encoder implements StringEncoder {
private static Base64Encoder instance; private static Base64Encoder instance = new Base64Encoder();
private Base64Encoder() { private Base64Encoder() {
// Use getInstance() // Use getInstance()
} }
public static Base64Encoder getInstance() { public static Base64Encoder getInstance() {
if (instance == null) {
instance = new Base64Encoder();
}
return instance; return instance;
} }

View file

@ -17,8 +17,6 @@
*/ */
package org.jivesoftware.smack.util; package org.jivesoftware.smack.util;
// TODO move StringEncoder, Base64Encoder and Base32Encoder to smack.util
public interface StringEncoder { public interface StringEncoder {
/** /**
* Encodes an string to another representation * Encodes an string to another representation
@ -26,7 +24,7 @@ public interface StringEncoder {
* @param string * @param string
* @return * @return
*/ */
public String encode(String string); String encode(String string);
/** /**
* Decodes an string back to it's initial representation * Decodes an string back to it's initial representation
@ -34,5 +32,5 @@ public interface StringEncoder {
* @param string * @param string
* @return * @return
*/ */
public String decode(String string); String decode(String string);
} }

View file

@ -299,24 +299,23 @@ public class FormField {
return buf.toString(); return buf.toString();
} }
@Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == null) if (obj == null)
return false; return false;
if (obj == this) if (obj == this)
return true; return true;
if (obj.getClass() != getClass()) if (!(obj instanceof FormField))
return false; return false;
FormField other = (FormField) obj; FormField other = (FormField) obj;
String thisXml = toXML(); return toXML().equals(other.toXML());
String otherXml = other.toXML();
if (thisXml.equals(otherXml)) {
return true;
} else {
return false;
} }
@Override
public int hashCode() {
return toXML().hashCode();
} }
/** /**
@ -356,6 +355,7 @@ public class FormField {
return value; return value;
} }
@Override
public String toString() { public String toString() {
return getLabel(); return getLabel();
} }
@ -375,6 +375,7 @@ public class FormField {
return buf.toString(); return buf.toString();
} }
@Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == null) if (obj == null)
return false; return false;
@ -396,5 +397,13 @@ public class FormField {
return true; return true;
} }
@Override
public int hashCode() {
int result = 1;
result = 37 * result + value.hashCode();
result = 37 * result + (label == null ? 0 : label.hashCode());
return result;
}
} }
} }

View file

@ -45,7 +45,7 @@ public interface NodeInformationProvider {
* *
* @return a list of the Items defined in the node. * @return a list of the Items defined in the node.
*/ */
public abstract List<DiscoverItems.Item> getNodeItems(); List<DiscoverItems.Item> getNodeItems();
/** /**
* Returns a list of the features defined in the node. For * Returns a list of the features defined in the node. For
@ -55,7 +55,7 @@ public interface NodeInformationProvider {
* *
* @return a list of the feature strings defined in the node. * @return a list of the feature strings defined in the node.
*/ */
public abstract List<String> getNodeFeatures(); List<String> getNodeFeatures();
/** /**
* Returns a list of the indentites defined in the node. For * Returns a list of the indentites defined in the node. For
@ -64,12 +64,12 @@ public interface NodeInformationProvider {
* *
* @return a list of the Identities defined in the node. * @return a list of the Identities defined in the node.
*/ */
public abstract List<DiscoverInfo.Identity> getNodeIdentities(); List<DiscoverInfo.Identity> getNodeIdentities();
/** /**
* Returns a list of the packet extensions defined in the node. * Returns a list of the packet extensions defined in the node.
* *
* @return a list of the packet extensions defined in the node. * @return a list of the packet extensions defined in the node.
*/ */
public abstract List<PacketExtension> getNodePacketExtensions(); List<PacketExtension> getNodePacketExtensions();
} }

View file

@ -513,7 +513,7 @@ public class ServiceDiscoveryManager {
// If the node version is known, store the new entry. // If the node version is known, store the new entry.
if (nvh != null) { if (nvh != null) {
if (EntityCapsManager.verifyDiscvoerInfoVersion(nvh.getVer(), nvh.getHash(), info)) if (EntityCapsManager.verifyDiscoverInfoVersion(nvh.getVer(), nvh.getHash(), info))
EntityCapsManager.addDiscoverInfoByNode(nvh.getNodeVer(), info); EntityCapsManager.addDiscoverInfoByNode(nvh.getNodeVer(), info);
} }

View file

@ -506,7 +506,7 @@ public class EntityCapsManager {
* @param info * @param info
* @return true if it's valid and should be cache, false if not * @return true if it's valid and should be cache, false if not
*/ */
public static boolean verifyDiscvoerInfoVersion(String ver, String hash, DiscoverInfo info) { public static boolean verifyDiscoverInfoVersion(String ver, String hash, DiscoverInfo info) {
// step 3.3 check for duplicate identities // step 3.3 check for duplicate identities
if (info.containsDuplicateIdentities()) if (info.containsDuplicateIdentities())
return false; return false;
@ -583,7 +583,7 @@ public class EntityCapsManager {
// NAME is not included (in accordance with XEP-0030, the category and // NAME is not included (in accordance with XEP-0030, the category and
// type MUST be included. // type MUST be included.
SortedSet<DiscoverInfo.Identity> sortedIdentities = new TreeSet<DiscoverInfo.Identity>(); SortedSet<DiscoverInfo.Identity> sortedIdentities = new TreeSet<DiscoverInfo.Identity>();
;
for (Iterator<DiscoverInfo.Identity> it = discoverInfo.getIdentities(); it.hasNext();) for (Iterator<DiscoverInfo.Identity> it = discoverInfo.getIdentities(); it.hasNext();)
sortedIdentities.add(it.next()); sortedIdentities.add(it.next());
@ -616,7 +616,7 @@ public class EntityCapsManager {
// only use the data form for calculation is it has a hidden FORM_TYPE // only use the data form for calculation is it has a hidden FORM_TYPE
// field // field
// see XEP-0115 5.4 step 3.6 // see XEP-0115 5.4 step 3.6
if (extendedInfo != null && extendedInfo.hasHiddenFromTypeField()) { if (extendedInfo != null && extendedInfo.hasHiddenFormTypeField()) {
synchronized (extendedInfo) { synchronized (extendedInfo) {
// 6. If the service discovery information response includes // 6. If the service discovery information response includes
// XEP-0128 data forms, sort the forms by the FORM_TYPE (i.e., // XEP-0128 data forms, sort the forms by the FORM_TYPE (i.e.,

View file

@ -24,15 +24,15 @@ public interface EntityCapsPersistentCache {
* @param node * @param node
* @param info * @param info
*/ */
abstract void addDiscoverInfoByNodePersistent(String node, DiscoverInfo info); void addDiscoverInfoByNodePersistent(String node, DiscoverInfo info);
/** /**
* Replay the Caches data into EntityCapsManager * Replay the Caches data into EntityCapsManager
*/ */
abstract void replay() throws IOException; void replay() throws IOException;
/** /**
* Empty the Cache * Empty the Cache
*/ */
abstract void emptyCache(); void emptyCache();
} }

View file

@ -1,4 +1,4 @@
/* /**
* Copyright 2009 Jonas Ådahl. * Copyright 2009 Jonas Ådahl.
* Copyright 2011-2013 Florian Schmaus * Copyright 2011-2013 Florian Schmaus
* *

View file

@ -202,7 +202,7 @@ public class DataForm implements PacketExtension {
* *
* @return * @return
*/ */
public boolean hasHiddenFromTypeField() { public boolean hasHiddenFormTypeField() {
boolean found = false; boolean found = false;
for (FormField f : fields) { for (FormField f : fields) {
if (f.getVariable().equals("FORM_TYPE") && f.getType() != null && f.getType().equals("hidden")) if (f.getVariable().equals("FORM_TYPE") && f.getType() != null && f.getType().equals("hidden"))

View file

@ -257,13 +257,25 @@ public class DiscoverInfo extends IQ {
* attributes. * attributes.
* *
*/ */
public static class Identity implements Comparable<Object> { public static class Identity implements Comparable<Identity> {
private String category; private String category;
private String name; private String name;
private String type; private String type;
private String lang; // 'xml:lang; private String lang; // 'xml:lang;
/**
* Creates a new identity for an XMPP entity.
*
* @param category the entity's category.
* @param name the entity's name.
* @deprecated As per the spec, the type field is mandatory and the 3 argument constructor should be used instead.
*/
public Identity(String category, String name) {
this.category = category;
this.name = name;
}
/** /**
* Creates a new identity for an XMPP entity. * Creates a new identity for an XMPP entity.
* 'category' and 'type' are required by * 'category' and 'type' are required by
@ -274,6 +286,9 @@ public class DiscoverInfo extends IQ {
* @param type the entity's type (required as per XEP-30). * @param type the entity's type (required as per XEP-30).
*/ */
public Identity(String category, String name, String type) { public Identity(String category, String name, String type) {
if ((category == null) || (type == null))
throw new IllegalArgumentException("category and type cannot be null");
this.category = category; this.category = category;
this.name = name; this.name = name;
this.type = type; this.type = type;
@ -313,6 +328,7 @@ public class DiscoverInfo extends IQ {
* 'type' attribute refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a> * 'type' attribute refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>
* *
* @param type the identity's type. * @param type the identity's type.
* @deprecated As per the spec, this field is mandatory and the 3 argument constructor should be used instead.
*/ */
public void setType(String type) { public void setType(String type) {
this.type = type; this.type = type;
@ -374,12 +390,15 @@ public class DiscoverInfo extends IQ {
String otherLang = other.lang == null ? "" : other.lang; String otherLang = other.lang == null ? "" : other.lang;
String thisLang = lang == null ? "" : lang; String thisLang = lang == null ? "" : lang;
if (!other.type.equals(type))
return false;
if (!otherLang.equals(thisLang)) if (!otherLang.equals(thisLang))
return false; return false;
// This safeguard can be removed once the deprecated constructor is removed.
String otherType = other.type == null ? "" : other.type;
String thisType = type == null ? "" : type;
if (!otherType.equals(thisType))
return false;
String otherName = other.name == null ? "" : other.name; String otherName = other.name == null ? "" : other.name;
String thisName = name == null ? "" : other.name; String thisName = name == null ? "" : other.name;
if (!thisName.equals(otherName)) if (!thisName.equals(otherName))
@ -388,22 +407,34 @@ public class DiscoverInfo extends IQ {
return true; return true;
} }
@Override
public int hashCode() {
int result = 1;
result = 37 * result + category.hashCode();
result = 37 * result + (lang == null ? 0 : lang.hashCode());
result = 37 * result + (type == null ? 0 : type.hashCode());
result = 37 * result + (name == null ? 0 : name.hashCode());
return result;
}
/** /**
* Compares and identity with another object. The comparison order is: * Compares this identity with another one. The comparison order is:
* Category, Type, Lang. If all three are identical the other Identity is considered equal. * Category, Type, Lang. If all three are identical the other Identity is considered equal.
* Name is not used for comparision, as defined by XEP-0115 * Name is not used for comparision, as defined by XEP-0115
* *
* @param obj * @param obj
* @return * @return
*/ */
public int compareTo(Object obj) { public int compareTo(DiscoverInfo.Identity other) {
DiscoverInfo.Identity other = (DiscoverInfo.Identity) obj;
String otherLang = other.lang == null ? "" : other.lang; String otherLang = other.lang == null ? "" : other.lang;
String thisLang = lang == null ? "" : lang; String thisLang = lang == null ? "" : lang;
// This can be removed once the deprecated constructor is removed.
String otherType = other.type == null ? "" : other.type;
String thisType = type == null ? "" : type;
if (category.equals(other.category)) { if (category.equals(other.category)) {
if (type.equals(other.type)) { if (thisType.equals(otherType)) {
if (thisLang.equals(otherLang)) { if (thisLang.equals(otherLang)) {
// Don't compare on name, XEP-30 says that name SHOULD // Don't compare on name, XEP-30 says that name SHOULD
// be equals for all identities of an entity // be equals for all identities of an entity
@ -412,7 +443,7 @@ public class DiscoverInfo extends IQ {
return thisLang.compareTo(otherLang); return thisLang.compareTo(otherLang);
} }
} else { } else {
return type.compareTo(other.type); return thisType.compareTo(otherType);
} }
} else { } else {
return category.compareTo(other.category); return category.compareTo(other.category);
@ -436,6 +467,8 @@ public class DiscoverInfo extends IQ {
* @param variable the feature's variable. * @param variable the feature's variable.
*/ */
public Feature(String variable) { public Feature(String variable) {
if (variable == null)
throw new IllegalArgumentException("variable cannot be null");
this.variable = variable; this.variable = variable;
} }
@ -465,5 +498,10 @@ public class DiscoverInfo extends IQ {
DiscoverInfo.Feature other = (DiscoverInfo.Feature) obj; DiscoverInfo.Feature other = (DiscoverInfo.Feature) obj;
return variable.equals(other.variable); return variable.equals(other.variable);
} }
@Override
public int hashCode() {
return 37 * variable.hashCode();
}
} }
} }

View file

@ -6,6 +6,8 @@ import org.jivesoftware.smack.XMPPException;
public class ConnectionUtils { public class ConnectionUtils {
private ConnectionUtils() {}
public static void becomeFriends(Connection con0, Connection con1) throws XMPPException { public static void becomeFriends(Connection con0, Connection con1) throws XMPPException {
Roster r0 = con0.getRoster(); Roster r0 = con0.getRoster();
Roster r1 = con1.getRoster(); Roster r1 = con1.getRoster();