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

public class WahlvorschlagDocumentByPositionComparator implements Comparator<CoreWahlvorschlagsdatenDocument>, Serializable {

  private static final long serialVersionUID = 1L;

  public static final Comparator<CoreWahlvorschlagsdatenDocument> ASCENDING = new WahlvorschlagDocumentByPositionComparator();

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

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

  private WahlvorschlagDocumentByPositionComparator() {
    super();
  }

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

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