package org.apache.commons.collections.comparators;

import java.io.Serializable;
import java.util.Comparator;

public class ComparableComparator implements Comparator, Serializable {
  private static final ComparableComparator instance = new ComparableComparator();
  
  private static final long serialVersionUID = -291439688585137865L;
  
  public int compare(Object paramObject1, Object paramObject2) {
    if (paramObject1 == null || paramObject2 == null)
      throw new ClassCastException("There were nulls in the arguments for this method: compare(" + paramObject1 + ", " + paramObject2 + ")"); 
    if (paramObject1 instanceof Comparable) {
      if (paramObject2 instanceof Comparable) {
        int i = ((Comparable)paramObject1).compareTo(paramObject2);
        int j = ((Comparable)paramObject2).compareTo(paramObject1);
        if (i == 0 && j == 0)
          return 0; 
        if (i < 0 && j > 0)
          return i; 
        if (i > 0 && j < 0)
          return i; 
        throw new ClassCastException("o1 not comparable to o2");
      } 
      throw new ClassCastException("The first argument of this method was not a Comparable: " + paramObject2.getClass().getName());
    } 
    if (paramObject2 instanceof Comparable)
      throw new ClassCastException("The second argument of this method was not a Comparable: " + paramObject1.getClass().getName()); 
    throw new ClassCastException("Both arguments of this method were not Comparables: " + paramObject1.getClass().getName() + " and " + paramObject2.getClass().getName());
  }
  
  public static ComparableComparator getInstance() {
    return instance;
  }
}
