
package de.ivu.elect.presentation.ergebnis.dokumente.gesamtergebnis;

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

import de.ivu.elect.dokumente.wahlvorschlag.CoreListenverbindungDokument;
import de.ivu.elect.util.NullComparator;

public class ListenverbindungDocumentByPositionComparator implements Comparator<CoreListenverbindungDokument>, Serializable {

  private static final long serialVersionUID = 1L;

  public static final Comparator<CoreListenverbindungDokument> ASCENDING = new ListenverbindungDocumentByPositionComparator();

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

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

  private ListenverbindungDocumentByPositionComparator() {
    super();
  }

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

    return NULL_COMPARATOR.compare(one, another);
  }

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

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