speed optimisation: store computed hashCode

This commit is contained in:
Alexey Kudravtsev
2015-07-02 12:48:45 +03:00
parent bbb6fa6d01
commit afac0df426
@@ -15,15 +15,36 @@
*/
package com.intellij.util.text;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
public class ByteArrayCharSequence implements CharSequence {
private int hash;
private final byte[] myChars;
public ByteArrayCharSequence(@NotNull byte... chars) {
private ByteArrayCharSequence(@NotNull byte[] chars) {
myChars = chars;
}
@Override
public int hashCode() {
int h = hash;
if (h == 0) {
byte[] chars = myChars;
for (byte aChar : chars) {
h = 31 * h + aChar;
}
hash = h;
}
return h;
}
@Override
public boolean equals(Object obj) {
return obj instanceof CharSequence && StringUtil.equals(this, (CharSequence)obj);
}
@Override
public final int length() {
return myChars.length;
@@ -63,6 +84,7 @@ public class ByteArrayCharSequence implements CharSequence {
//noinspection RedundantStringConstructorCall
return new String(name.toString()); // So we don't hold whole char[] buffer of a lengthy path on JDK 6
}
bytes[i] = (byte)c;
}
return new ByteArrayCharSequence(bytes);