Merge pull request #15465 from dreis2211

* pr/15465:
  Polish "Fix StringSequence.equals() for different lengths"
  Fix StringSequence.equals() for different lengths
This commit is contained in:
Stephane Nicoll 2018-12-17 18:26:09 +01:00
commit 1944954d95
2 changed files with 17 additions and 10 deletions

View File

@ -111,20 +111,20 @@ final class StringSequence implements CharSequence {
if (this == obj) {
return true;
}
if (obj == null || !CharSequence.class.isInstance(obj)) {
if (!(obj instanceof CharSequence)) {
return false;
}
CharSequence other = (CharSequence) obj;
int n = length();
if (n == other.length()) {
int i = 0;
while (n-- != 0) {
if (charAt(i) != other.charAt(i)) {
return false;
}
i++;
if (n != other.length()) {
return false;
}
int i = 0;
while (n-- != 0) {
if (charAt(i) != other.charAt(i)) {
return false;
}
return true;
i++;
}
return true;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -167,6 +167,13 @@ public class StringSequenceTests {
assertThat(a).isEqualTo(b).isNotEqualTo(c);
}
@Test
public void notEqualsWhenSequencesOfDifferentLength() {
StringSequence a = new StringSequence("abcd");
StringSequence b = new StringSequence("ef");
assertThat(a).isNotEqualTo(b);
}
@Test
public void startsWithWhenExactMatch() {
assertThat(new StringSequence("abc").startsWith("abc")).isTrue();