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

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

import de.ivu.elect.business.person.entity.PersonendatenDateiExport;
import de.ivu.elect.util.NullComparator;

public class PersonendatenDateiExportByErstellungszeitpunktComparator implements Comparator<PersonendatenDateiExport>, Serializable {

  private static final long serialVersionUID = 1L;

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

  public static final Comparator<PersonendatenDateiExport> ASCENDING = new PersonendatenDateiExportByErstellungszeitpunktComparator();

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

  public PersonendatenDateiExportByErstellungszeitpunktComparator() {
    super();
  }

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

    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);
  }
}
