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

import java.util.Collections;
import java.util.Comparator;

import de.ivu.elect.business.ergebnis.entity.DHondtQuotient;
import de.ivu.elect.business.ergebnis.entity.DHondtQuotientListenImGebiet;
import de.ivu.elect.business.gebietsbaum.control.GebietByPositionComparator;
import de.ivu.elect.business.gebietsbaum.entity.Gebiet;
import de.ivu.elect.util.NullComparator;

public class DHondtQuotientListenImGebietByRangImGebietComparator implements Comparator<DHondtQuotientListenImGebiet> {

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

  private static final Comparator<Gebiet> GEBIET_COMPARATOR = GebietByPositionComparator.ASCENDING;

  private static final Comparator<DHondtQuotient> RANG_COMPARATOR = DHondtQuotientByRangComparator.ASCENDING;

  public static final Comparator<DHondtQuotientListenImGebiet> ASCENDING = new DHondtQuotientListenImGebietByRangImGebietComparator();

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

  public DHondtQuotientListenImGebietByRangImGebietComparator() {
    super();
  }

  @Override
  public int compare(DHondtQuotientListenImGebiet one, DHondtQuotientListenImGebiet another) {
    if (one == null || another == null) {
      return NULL_COMPARATOR.compare(one, another);
    }

    int comparedByGebiet = compareGebiet(one, another);

    if (comparedByGebiet == 0) {
      return compareRang(one, another);
    }

    return comparedByGebiet;
  }

  private int compareGebiet(DHondtQuotientListenImGebiet one, DHondtQuotientListenImGebiet another) {
    return GEBIET_COMPARATOR.compare(one.getWahldurchfuehrungsgebiet(), another.getWahldurchfuehrungsgebiet());
  }

  private int compareRang(DHondtQuotientListenImGebiet one, DHondtQuotientListenImGebiet another) {
    return RANG_COMPARATOR.compare(one, another);
  }
}
