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

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

import de.ivu.elect.util.NullComparator;

public class KandidaturDocumentByRangComparator implements Comparator<KandidaturMitStimmergebnissenDocument>, Serializable {

  private static final long serialVersionUID = 1L;

  public static final Comparator<KandidaturMitStimmergebnissenDocument> ASCENDING = new KandidaturDocumentByRangComparator();

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

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

  private static final Comparator<KandidaturMitStimmergebnissenDocument> STIMMEN_COMPARATOR = KandidaturDocumentByStimmenComparator.DESCENDING;

  private KandidaturDocumentByRangComparator() {
    super();
  }

  @Override
  public int compare(KandidaturMitStimmergebnissenDocument one, KandidaturMitStimmergebnissenDocument another) {
    if (one != null && another != null) {
      int comparedByRang = compareAsc(one.rang, another.rang);

      if (comparedByRang == 0) {
        return STIMMEN_COMPARATOR.compare(one, another);
      }

      return comparedByRang;
    }

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