
package de.ivu.elect.business.ergebnis.control;

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

import de.ivu.elect.business.ergebnis.entity.DHondtQuotient;
import de.ivu.elect.util.NullComparator;

public class DHondtQuotientByRangComparator implements Comparator<DHondtQuotient>, Serializable {

  private static final long serialVersionUID = 1L;

  private static final Comparator<Object> NULL_COMPARATOR = NullComparator.NULL_BEFORE_NON_NULL_COMPARATOR;

  public static final Comparator<DHondtQuotient> ASCENDING = new DHondtQuotientByRangComparator();

  public static final Comparator<DHondtQuotient> DESCENDING = Collections.reverseOrder(ASCENDING);

  public DHondtQuotientByRangComparator() {
    super();
  }

  @Override
  public int compare(DHondtQuotient one, DHondtQuotient another) {
    if (one == null || another == null) {
      return NULL_COMPARATOR.compare(one, another);
    }

    return compareRang(one.getRang(), another.getRang());
  }

  private int compareRang(Long one, Long another) {
    if (one == null || another == null) {
      return NULL_COMPARATOR.compare(one, another);
    }

    return one.compareTo(another);
  }
}
