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

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

import de.ivu.elect.business.wahlrechtspruefung.entity.Pruefkriterium;
import de.ivu.elect.util.NullComparator;

public class PruefkriteriumByPositionComparator implements Comparator<Pruefkriterium>, Serializable {

  private static final long serialVersionUID = 1L;

  public static final Comparator<Pruefkriterium> ASCENDING = new PruefkriteriumByPositionComparator();

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

  public PruefkriteriumByPositionComparator() {
    super();
  }

  @Override
  public int compare(Pruefkriterium one, Pruefkriterium another) {
    if (one == null || another == null) {
      return NullComparator.NULL_BEFORE_NON_NULL_COMPARATOR.compare(one, another);
    }

    return compare(one.getPosition(), another.getPosition());
  }

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

    return one.compareTo(another);
  }
}
