diff --git a/source/org/jivesoftware/smack/util/Base32Encoder.java b/source/org/jivesoftware/smack/util/Base32Encoder.java index c7cc1d028..0a4ea21e7 100644 --- a/source/org/jivesoftware/smack/util/Base32Encoder.java +++ b/source/org/jivesoftware/smack/util/Base32Encoder.java @@ -29,7 +29,7 @@ import java.io.IOException; */ public class Base32Encoder implements StringEncoder { - private static Base32Encoder instance; + private static Base32Encoder instance = new Base32Encoder(); private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ2345678"; private Base32Encoder() { @@ -37,9 +37,6 @@ public class Base32Encoder implements StringEncoder { } public static Base32Encoder getInstance() { - if (instance == null) { - instance = new Base32Encoder(); - } return instance; } diff --git a/source/org/jivesoftware/smack/util/Base64Encoder.java b/source/org/jivesoftware/smack/util/Base64Encoder.java index 78399b463..8d29f12c5 100644 --- a/source/org/jivesoftware/smack/util/Base64Encoder.java +++ b/source/org/jivesoftware/smack/util/Base64Encoder.java @@ -20,16 +20,13 @@ package org.jivesoftware.smack.util; */ public class Base64Encoder implements StringEncoder { - private static Base64Encoder instance; + private static Base64Encoder instance = new Base64Encoder(); private Base64Encoder() { // Use getInstance() } public static Base64Encoder getInstance() { - if (instance == null) { - instance = new Base64Encoder(); - } return instance; } diff --git a/source/org/jivesoftware/smack/util/StringEncoder.java b/source/org/jivesoftware/smack/util/StringEncoder.java index 5a15c9548..4c3d373f3 100644 --- a/source/org/jivesoftware/smack/util/StringEncoder.java +++ b/source/org/jivesoftware/smack/util/StringEncoder.java @@ -17,8 +17,6 @@ */ package org.jivesoftware.smack.util; -// TODO move StringEncoder, Base64Encoder and Base32Encoder to smack.util - public interface StringEncoder { /** * Encodes an string to another representation @@ -26,7 +24,7 @@ public interface StringEncoder { * @param string * @return */ - public String encode(String string); + String encode(String string); /** * Decodes an string back to it's initial representation @@ -34,5 +32,5 @@ public interface StringEncoder { * @param string * @return */ - public String decode(String string); + String decode(String string); } diff --git a/source/org/jivesoftware/smackx/FormField.java b/source/org/jivesoftware/smackx/FormField.java index 44dfa8cec..3d15e921a 100644 --- a/source/org/jivesoftware/smackx/FormField.java +++ b/source/org/jivesoftware/smackx/FormField.java @@ -299,24 +299,23 @@ public class FormField { return buf.toString(); } + @Override public boolean equals(Object obj) { if (obj == null) return false; if (obj == this) return true; - if (obj.getClass() != getClass()) + if (!(obj instanceof FormField)) return false; FormField other = (FormField) obj; - String thisXml = toXML(); - String otherXml = other.toXML(); + return toXML().equals(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; } + @Override public String toString() { return getLabel(); } @@ -375,6 +375,7 @@ public class FormField { return buf.toString(); } + @Override public boolean equals(Object obj) { if (obj == null) return false; @@ -396,5 +397,13 @@ public class FormField { return true; } + + @Override + public int hashCode() { + int result = 1; + result = 37 * result + value.hashCode(); + result = 37 * result + (label == null ? 0 : label.hashCode()); + return result; + } } } diff --git a/source/org/jivesoftware/smackx/NodeInformationProvider.java b/source/org/jivesoftware/smackx/NodeInformationProvider.java index 68bb613d3..27ee53a01 100644 --- a/source/org/jivesoftware/smackx/NodeInformationProvider.java +++ b/source/org/jivesoftware/smackx/NodeInformationProvider.java @@ -45,7 +45,7 @@ public interface NodeInformationProvider { * * @return a list of the Items defined in the node. */ - public abstract List getNodeItems(); + List getNodeItems(); /** * 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. */ - public abstract List getNodeFeatures(); + List getNodeFeatures(); /** * 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. */ - public abstract List getNodeIdentities(); + List getNodeIdentities(); /** * Returns a list of the packet extensions defined in the node. * * @return a list of the packet extensions defined in the node. */ - public abstract List getNodePacketExtensions(); + List getNodePacketExtensions(); } diff --git a/source/org/jivesoftware/smackx/ServiceDiscoveryManager.java b/source/org/jivesoftware/smackx/ServiceDiscoveryManager.java index f0e7912aa..9e31f6777 100644 --- a/source/org/jivesoftware/smackx/ServiceDiscoveryManager.java +++ b/source/org/jivesoftware/smackx/ServiceDiscoveryManager.java @@ -513,7 +513,7 @@ public class ServiceDiscoveryManager { // If the node version is known, store the new entry. if (nvh != null) { - if (EntityCapsManager.verifyDiscvoerInfoVersion(nvh.getVer(), nvh.getHash(), info)) + if (EntityCapsManager.verifyDiscoverInfoVersion(nvh.getVer(), nvh.getHash(), info)) EntityCapsManager.addDiscoverInfoByNode(nvh.getNodeVer(), info); } diff --git a/source/org/jivesoftware/smackx/entitycaps/EntityCapsManager.java b/source/org/jivesoftware/smackx/entitycaps/EntityCapsManager.java index d5d6402d2..1da222efd 100644 --- a/source/org/jivesoftware/smackx/entitycaps/EntityCapsManager.java +++ b/source/org/jivesoftware/smackx/entitycaps/EntityCapsManager.java @@ -506,7 +506,7 @@ public class EntityCapsManager { * @param info * @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 if (info.containsDuplicateIdentities()) return false; @@ -583,7 +583,7 @@ public class EntityCapsManager { // NAME is not included (in accordance with XEP-0030, the category and // type MUST be included. SortedSet sortedIdentities = new TreeSet(); - ; + for (Iterator it = discoverInfo.getIdentities(); it.hasNext();) 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 // field // see XEP-0115 5.4 step 3.6 - if (extendedInfo != null && extendedInfo.hasHiddenFromTypeField()) { + if (extendedInfo != null && extendedInfo.hasHiddenFormTypeField()) { synchronized (extendedInfo) { // 6. If the service discovery information response includes // XEP-0128 data forms, sort the forms by the FORM_TYPE (i.e., diff --git a/source/org/jivesoftware/smackx/entitycaps/cache/EntityCapsPersistentCache.java b/source/org/jivesoftware/smackx/entitycaps/cache/EntityCapsPersistentCache.java index 4247e7b1b..044104351 100644 --- a/source/org/jivesoftware/smackx/entitycaps/cache/EntityCapsPersistentCache.java +++ b/source/org/jivesoftware/smackx/entitycaps/cache/EntityCapsPersistentCache.java @@ -24,15 +24,15 @@ public interface EntityCapsPersistentCache { * @param node * @param info */ - abstract void addDiscoverInfoByNodePersistent(String node, DiscoverInfo info); + void addDiscoverInfoByNodePersistent(String node, DiscoverInfo info); /** * Replay the Caches data into EntityCapsManager */ - abstract void replay() throws IOException; + void replay() throws IOException; /** * Empty the Cache */ - abstract void emptyCache(); + void emptyCache(); } diff --git a/source/org/jivesoftware/smackx/entitycaps/provider/CapsExtensionProvider.java b/source/org/jivesoftware/smackx/entitycaps/provider/CapsExtensionProvider.java index 4328d21b3..a112cd52b 100644 --- a/source/org/jivesoftware/smackx/entitycaps/provider/CapsExtensionProvider.java +++ b/source/org/jivesoftware/smackx/entitycaps/provider/CapsExtensionProvider.java @@ -1,4 +1,4 @@ -/* +/** * Copyright 2009 Jonas Ã…dahl. * Copyright 2011-2013 Florian Schmaus * diff --git a/source/org/jivesoftware/smackx/packet/DataForm.java b/source/org/jivesoftware/smackx/packet/DataForm.java index 4bc1f6994..4d12892b5 100644 --- a/source/org/jivesoftware/smackx/packet/DataForm.java +++ b/source/org/jivesoftware/smackx/packet/DataForm.java @@ -202,7 +202,7 @@ public class DataForm implements PacketExtension { * * @return */ - public boolean hasHiddenFromTypeField() { + public boolean hasHiddenFormTypeField() { boolean found = false; for (FormField f : fields) { if (f.getVariable().equals("FORM_TYPE") && f.getType() != null && f.getType().equals("hidden")) diff --git a/source/org/jivesoftware/smackx/packet/DiscoverInfo.java b/source/org/jivesoftware/smackx/packet/DiscoverInfo.java index e2219032d..ba873a96d 100644 --- a/source/org/jivesoftware/smackx/packet/DiscoverInfo.java +++ b/source/org/jivesoftware/smackx/packet/DiscoverInfo.java @@ -257,13 +257,25 @@ public class DiscoverInfo extends IQ { * attributes. * */ - public static class Identity implements Comparable { + public static class Identity implements Comparable { private String category; private String name; private String type; 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. * '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). */ 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.name = name; this.type = type; @@ -313,6 +328,7 @@ public class DiscoverInfo extends IQ { * 'type' attribute refer to Jabber::Registrar * * @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) { this.type = type; @@ -374,11 +390,14 @@ public class DiscoverInfo extends IQ { String otherLang = other.lang == null ? "" : other.lang; String thisLang = lang == null ? "" : lang; - - if (!other.type.equals(type)) - return false; if (!otherLang.equals(thisLang)) 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 thisName = name == null ? "" : other.name; @@ -387,23 +406,35 @@ public class DiscoverInfo extends IQ { 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. * Name is not used for comparision, as defined by XEP-0115 * * @param obj * @return */ - public int compareTo(Object obj) { - - DiscoverInfo.Identity other = (DiscoverInfo.Identity) obj; + public int compareTo(DiscoverInfo.Identity other) { String otherLang = other.lang == null ? "" : other.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 (type.equals(other.type)) { + if (thisType.equals(otherType)) { if (thisLang.equals(otherLang)) { // Don't compare on name, XEP-30 says that name SHOULD // be equals for all identities of an entity @@ -412,7 +443,7 @@ public class DiscoverInfo extends IQ { return thisLang.compareTo(otherLang); } } else { - return type.compareTo(other.type); + return thisType.compareTo(otherType); } } else { return category.compareTo(other.category); @@ -436,6 +467,8 @@ public class DiscoverInfo extends IQ { * @param variable the feature's variable. */ public Feature(String variable) { + if (variable == null) + throw new IllegalArgumentException("variable cannot be null"); this.variable = variable; } @@ -465,5 +498,10 @@ public class DiscoverInfo extends IQ { DiscoverInfo.Feature other = (DiscoverInfo.Feature) obj; return variable.equals(other.variable); } + + @Override + public int hashCode() { + return 37 * variable.hashCode(); + } } } diff --git a/test/org/jivesoftware/smack/util/ConnectionUtils.java b/test/org/jivesoftware/smack/util/ConnectionUtils.java index 6d9f49106..74426793e 100644 --- a/test/org/jivesoftware/smack/util/ConnectionUtils.java +++ b/test/org/jivesoftware/smack/util/ConnectionUtils.java @@ -6,6 +6,8 @@ import org.jivesoftware.smack.XMPPException; public class ConnectionUtils { + private ConnectionUtils() {} + public static void becomeFriends(Connection con0, Connection con1) throws XMPPException { Roster r0 = con0.getRoster(); Roster r1 = con1.getRoster();