Add equals, equalsIgnoreCase and hashCode

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@240404 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2005-08-27 11:31:23 +00:00
parent cb3ac6dd3d
commit 112d13fa39
2 changed files with 137 additions and 0 deletions

View File

@ -2010,6 +2010,86 @@ public class StrBuilder implements Cloneable {
// }
// }
//-----------------------------------------------------------------------
/**
* Checks the contents of this builder against another to see if they
* contain the same character content ignoring case.
*
* @param other the object to check, null returns false
* @return true if the builders contain the same characters in the same order
*/
public boolean equalsIgnoreCase(StrBuilder other) {
if (this == other) {
return true;
}
if (this.size != other.size) {
return false;
}
char thisBuf[] = this.buffer;
char otherBuf[] = other.buffer;
for (int i = size - 1; i >= 0; i--) {
char c1 = thisBuf[i];
char c2 = otherBuf[i];
if (c1 != c2 && Character.toUpperCase(c1) != Character.toUpperCase(c2)) {
return false;
}
}
return true;
}
/**
* Checks the contents of this builder against another to see if they
* contain the same character content.
*
* @param other the object to check, null returns false
* @return true if the builders contain the same characters in the same order
*/
public boolean equals(StrBuilder other) {
if (this == other) {
return true;
}
if (this.size != other.size) {
return false;
}
char thisBuf[] = this.buffer;
char otherBuf[] = other.buffer;
for (int i = size - 1; i >= 0; i--) {
if (thisBuf[i] != otherBuf[i]) {
return false;
}
}
return true;
}
/**
* Checks the contents of this builder against another to see if they
* contain the same character content.
*
* @param obj the object to check, null returns false
* @return true if the builders contain the same characters in the same order
*/
public boolean equals(Object obj) {
if (obj instanceof StrBuilder) {
return equals((StrBuilder) obj);
}
return false;
}
/**
* Gets a suitable hash code for this builder.
*
* @return a hash code
*/
public int hashCode() {
char buf[] = buffer;
int hash = 0;
for (int i = size - 1; i >= 0; i--) {
hash = 31 * hash + buf[i];
}
return hash;
}
//-----------------------------------------------------------------------
/**
* Gets a String version of the string builder, creating a new instance
* each time the method is called.

View File

@ -1588,6 +1588,63 @@ public class StrBuilderTest extends TestCase {
assertEquals("based", sb.toString());
}
//-----------------------------------------------------------------------
public void testEqualsIgnoreCase() {
StrBuilder sb1 = new StrBuilder();
StrBuilder sb2 = new StrBuilder();
assertEquals(true, sb1.equalsIgnoreCase(sb2));
sb1.append("abc");
assertEquals(false, sb1.equalsIgnoreCase(sb2));
sb2.append("ABC");
assertEquals(true, sb1.equalsIgnoreCase(sb2));
sb2.clear().append("abc");
assertEquals(true, sb1.equalsIgnoreCase(sb2));
sb2.clear().append("aBc");
assertEquals(true, sb1.equalsIgnoreCase(sb2));
}
//-----------------------------------------------------------------------
public void testEquals() {
StrBuilder sb1 = new StrBuilder();
StrBuilder sb2 = new StrBuilder();
assertEquals(true, sb1.equals((StrBuilder) sb2));
assertEquals(true, sb1.equals((Object) sb2));
sb1.append("abc");
assertEquals(false, sb1.equals((StrBuilder) sb2));
assertEquals(false, sb1.equals((Object) sb2));
sb2.append("ABC");
assertEquals(false, sb1.equals((StrBuilder) sb2));
assertEquals(false, sb1.equals((Object) sb2));
sb2.clear().append("abc");
assertEquals(true, sb1.equals((StrBuilder) sb2));
assertEquals(true, sb1.equals((Object) sb2));
assertEquals(false, sb1.equals(new Integer(1)));
assertEquals(false, sb1.equals("abc"));
}
//-----------------------------------------------------------------------
public void testHashCode() {
StrBuilder sb = new StrBuilder();
int hc1a = sb.hashCode();
int hc1b = sb.hashCode();
assertEquals(0, hc1a);
assertEquals(hc1a, hc1b);
sb.append("abc");
int hc2a = sb.hashCode();
int hc2b = sb.hashCode();
assertEquals(true, hc2a != 0);
assertEquals(hc2a, hc2b);
}
//-----------------------------------------------------------------------
public void testToString() {
StrBuilder sb = new StrBuilder("abc");