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

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

import de.ivu.elect.business.wahl.entity.Wahltermin;
import de.ivu.elect.util.NullComparator;

public class WahlterminByWahldatumComparator implements Comparator<Wahltermin>, Serializable {

  private static final long serialVersionUID = 1L;

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

  public static final Comparator<Wahltermin> ASCENDING = new WahlterminByWahldatumComparator();

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

  public WahlterminByWahldatumComparator() {
    super();
  }

  @Override
  public int compare(Wahltermin one, Wahltermin another) {
    if (one != null && another != null) {
      return compare(one.getWahldatum(), another.getWahldatum());
    }

    return NULL_COMPARATOR.compare(one, another);
  }

  private int compare(Date one, Date another) {
    if (one != null && another != null) {
      return one.compareTo(another);
    }

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