
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.DHondtQuotientListenInGruppe;
import de.ivu.elect.business.gruppe.control.GruppeByKurznameComparator;
import de.ivu.elect.business.gruppe.entity.Gruppe;
import de.ivu.elect.util.NullComparator;

public class DHondtQuotientListenInGruppeByRangInGruppeComparator implements Comparator<DHondtQuotientListenInGruppe> {

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

  private static final Comparator<Gruppe> GRUPPE_COMPARATOR = GruppeByKurznameComparator.ASCENDING;

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

  public static final Comparator<DHondtQuotientListenInGruppe> ASCENDING = new DHondtQuotientListenInGruppeByRangInGruppeComparator();

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

  public DHondtQuotientListenInGruppeByRangInGruppeComparator() {
    super();
  }

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

    int comparedByGruppe = compareGruppe(one, another);

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

    return comparedByGruppe;
  }

  private int compareGruppe(DHondtQuotientListenInGruppe one, DHondtQuotientListenInGruppe another) {
    return GRUPPE_COMPARATOR.compare(one.getPolitischeGruppe(), another.getPolitischeGruppe());
  }

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