Add equality check to contentEquals instance methods. (#9130)

Motivation:

An instance is always equal to itself. It makes sense to skip processing for this case, which isn't uncommon since `AsciiString` is often memoized within an application when used as HTTP header names.

Modification:

`contentEquals` methods first check for instance equality before doing processing.

Result:

`contentEquals` will be faster when comparing an instance with itself.

I couldn't find any unit tests for these methods, only the static version. Let me know if I should add something to `AsciiStringCharacterTest`.

Came up here:
https://github.com/line/armeria/pull/1731#discussion_r280396280
This commit is contained in:
Anuraag Agrawal 2019-05-08 14:30:34 +09:00 committed by Norman Maurer
parent 71c184076c
commit 526f2da912

View File

@ -522,6 +522,10 @@ public final class AsciiString implements CharSequence, Comparable<CharSequence>
* @return {@code true} if the specified string is equal to this string, {@code false} otherwise.
*/
public boolean contentEqualsIgnoreCase(CharSequence string) {
if (this == string) {
return true;
}
if (string == null || string.length() != length()) {
return false;
}
@ -1036,6 +1040,10 @@ public final class AsciiString implements CharSequence, Comparable<CharSequence>
* @return {@code true} if equal, otherwise {@code false}
*/
public boolean contentEquals(CharSequence a) {
if (this == a) {
return true;
}
if (a == null || a.length() != length()) {
return false;
}