1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-23 04:57:58 +01:00

Make XMPPError.Condition.equals null-safe

also make Condition implement CharSequence.

SMACK-603
This commit is contained in:
Florian Schmaus 2014-09-01 10:13:23 +02:00
parent 72557dd354
commit 5342aaf137
2 changed files with 40 additions and 1 deletions

View file

@ -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;
* </table>
*
* @author Matt Tucker
* @see <a href="http://xmpp.org/rfcs/rfc6120.html#stanzas-error-syntax">RFC 6120 - 8.3.2 Syntax: The Syntax of XMPP error stanzas</a>
*/
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);
}
}

View file

@ -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());
}
}