From 5342aaf137e4cf8b9e41cb296f6460274f3f5398 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 1 Sep 2014 10:13:23 +0200 Subject: [PATCH] Make XMPPError.Condition.equals null-safe also make Condition implement CharSequence. SMACK-603 --- .../jivesoftware/smack/packet/XMPPError.java | 27 ++++++++++++++++++- .../jivesoftware/smack/util/StringUtils.java | 14 ++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/XMPPError.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/XMPPError.java index c3343759b..c25e8066d 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/XMPPError.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/XMPPError.java @@ -23,6 +23,8 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import org.jivesoftware.smack.util.StringUtils; + /** * Represents a XMPP error sub-packet. Typically, a server responds to a request that has * problems by sending the packet back and including an error packet. Each error has a type, @@ -57,6 +59,7 @@ import java.util.Map; * * * @author Matt Tucker + * @see RFC 6120 - 8.3.2 Syntax: The Syntax of XMPP error stanzas */ public class XMPPError { @@ -266,7 +269,7 @@ public class XMPPError { /** * A class to represent predefined error conditions. */ - public static class Condition { + public static class Condition implements CharSequence { public static final Condition internal_server_error = new Condition("internal-server-error"); public static final Condition forbidden = new Condition("forbidden"); @@ -306,13 +309,35 @@ public class XMPPError { @Override public boolean equals(Object other) { + if (other == null) { + return false; + } return toString().equals(other.toString()); } + public boolean equals(CharSequence other) { + return StringUtils.nullSafeCharSequenceEquals(this, other); + } + @Override public int hashCode() { return value.hashCode(); } + + @Override + public int length() { + return value.length(); + } + + @Override + public char charAt(int index) { + return value.charAt(index); + } + + @Override + public CharSequence subSequence(int start, int end) { + return value.subSequence(start, end); + } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java index 796b7ff1f..9e4dbdb92 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java @@ -546,4 +546,18 @@ public class StringUtils { public static boolean isEmpty(CharSequence cs) { return cs.length() == 0; } + + public static boolean nullSafeCharSequenceEquals(CharSequence csOne, CharSequence csTwo) { + return nullSafeCharSequenceComperator(csOne, csTwo) == 0; + } + + public static int nullSafeCharSequenceComperator(CharSequence csOne, CharSequence csTwo) { + if (csOne == null ^ csTwo == null) { + return (csOne == null) ? -1 : 1; + } + if (csOne == null && csTwo == null) { + return 0; + } + return csOne.toString().compareTo(csTwo.toString()); + } }