package org.h2.value;
import java.nio.charset.Charset;
import java.text.CollationKey;
import java.text.Collator;
import java.util.Comparator;
public class CharsetCollator extends Collator {
static final Comparator<byte[]> COMPARATOR = new Comparator<byte[]>() {
@Override
public int compare(byte[] b1, byte[] b2) {
int minLength = Math.min(b1.length, b2.length);
for (int index = 0; index < minLength; index++) {
int result = b1[index] - b2[index];
if (result != 0) {
return result;
}
}
return b1.length - b2.length;
}
};
private final Charset charset;
public CharsetCollator(Charset charset) {
this.charset = charset;
}
public Charset getCharset() {
return charset;
}
@Override
public int compare(String source, String target) {
return COMPARATOR.compare(toBytes(source), toBytes(target));
}
byte[] toBytes(String source) {
return source.getBytes(charset);
}
@Override
public CollationKey getCollationKey(final String source) {
return new CharsetCollationKey(source);
}
@Override
public int hashCode() {
return 255;
}
private class CharsetCollationKey extends CollationKey {
CharsetCollationKey(String source) {
super(source);
}
@Override
public int compareTo(CollationKey target) {
return COMPARATOR.compare(toByteArray(), toBytes(target.getSourceString()));
}
@Override
public byte[] toByteArray() {
return toBytes(getSourceString());
}
}
}