org.jsimpledb.util.ConvertedComparator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsimpledb-util Show documentation
Show all versions of jsimpledb-util Show documentation
Common utility classes used by JSimpleDB.
/*
* Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
*/
package org.jsimpledb.util;
import com.google.common.base.Converter;
import com.google.common.base.Preconditions;
import java.util.Comparator;
/**
* Comparator that compares using converted values.
*/
class ConvertedComparator implements Comparator {
private final Comparator super W> comparator;
private final Converter converter;
ConvertedComparator(Comparator super W> comparator, Converter converter) {
Preconditions.checkArgument(converter != null, "null converter");
this.comparator = comparator;
this.converter = converter;
}
public Converter getConverter() {
return this.converter;
}
@Override
@SuppressWarnings("unchecked")
public int compare(E obj1, E obj2) {
final W wobj1 = this.converter.convert(obj1);
final W wobj2 = this.converter.convert(obj2);
return this.comparator != null ? this.comparator.compare(wobj1, wobj2) : ((Comparable)wobj1).compareTo(wobj2);
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
final ConvertedComparator, ?> that = (ConvertedComparator, ?>)obj;
return (this.comparator != null ? this.comparator.equals(that.comparator) : that.comparator == null)
&& this.converter.equals(that.converter);
}
@Override
public int hashCode() {
return (this.comparator != null ? this.comparator.hashCode() : 0) ^ this.converter.hashCode();
}
@Override
public String toString() {
return this.getClass().getSimpleName() + "[converter=" + this.converter + ",comparator=" + this.comparator + "]";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy