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

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

import de.ivu.elect.business.datei.entity.DateiImport;
import de.ivu.elect.util.NullComparator;

public class DateiImportByImportiertAmComparator implements Comparator<DateiImport>, Serializable {

  private static final long serialVersionUID = 1L;

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

  public static final Comparator<DateiImport> ASCENDING = new DateiImportByImportiertAmComparator();

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

  public DateiImportByImportiertAmComparator() {
    super();
  }

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

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