PersistComparator.java [plain text]
package com.sleepycat.persist.impl;
import java.io.Serializable;
import java.util.Comparator;
import java.util.List;
import com.sleepycat.persist.model.FieldMetadata;
public class PersistComparator implements Comparator<Object>, Serializable {
private static final long serialVersionUID = 5221576538843355317L;
private String keyClassName;
private String[] comositeFieldOrder;
private transient PersistKeyBinding binding;
public PersistComparator(String keyClassName,
List<FieldMetadata> compositeKeyFields,
PersistKeyBinding binding) {
this.keyClassName = keyClassName;
this.binding = binding;
if (compositeKeyFields != null) {
comositeFieldOrder =
CompositeKeyFormat.getFieldNameArray(compositeKeyFields);
}
}
public int compare(Object o1, Object o2) {
if (binding == null) {
Class keyClass;
try {
keyClass = SimpleCatalog.classForName(keyClassName);
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
binding = new PersistKeyBinding(keyClass, comositeFieldOrder);
}
byte[] b1 = (byte[]) o1;
byte[] b2 = (byte[]) o2;
Comparable k1 = (Comparable) binding.bytesToObject(b1, 0, b1.length);
Comparable k2 = (Comparable) binding.bytesToObject(b2, 0, b2.length);
return k1.compareTo(k2);
}
}