mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
speed optimisation: store computed hashCode
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user