All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xapi.collect.impl.HashComparator Maven / Gradle / Ivy

There is a newer version: 0.5
Show newest version
package xapi.collect.impl;

import java.util.Comparator;

public final class HashComparator  implements Comparator {
  public int compare(T o1, T o2) {
    if (o1 == o2)
      return 0;
    if (o1 == null) {
      return 1;
    } else if (o2 == null) {
      return -1;
    }
    int delta = o1.hashCode() - o2.hashCode();
    if (delta == 0) {
      if (o1.equals(o2)) {
        return 0;
      } else {
        throw new RuntimeException("Hash collision for inequal objects: "+o1+" && "+o2);
      }
    }
    return delta > 0 ? 1 : -1;
  };
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy