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

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

import de.ivu.elect.business.wahlvorschlag.entity.Wahlvorschlag;
import de.ivu.elect.util.NullComparator;

public class WahlvorschlagByZeitpunktDerEinreichungComparator implements Comparator<Wahlvorschlag>, Serializable {

  private static final long serialVersionUID = 1L;

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

  public static final Comparator<Wahlvorschlag> ASCENDING = new WahlvorschlagByZeitpunktDerEinreichungComparator();

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

  public WahlvorschlagByZeitpunktDerEinreichungComparator() {
    super();
  }

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

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

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

    return one.compareTo(another);
  }
}
