
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.Personendaten;
import de.ivu.elect.util.NullComparator;

public class PersonendatenByGeburtsdatumComparator<E extends Personendaten> implements Comparator<E>, Serializable {

  private static final long serialVersionUID = 1L;

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

  public static final Comparator<Personendaten> ASCENDING = new PersonendatenByGeburtsdatumComparator<>();

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

  public PersonendatenByGeburtsdatumComparator() {
    super();
  }

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

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