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

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

import de.ivu.elect.business.gruppe.control.GruppeNamenComparator;
import de.ivu.elect.business.gruppeimgebiet.entity.GruppeImGebiet;
import de.ivu.elect.util.NullComparator;

public class GruppeImGebietByNameAufGebietComparator implements Comparator<GruppeImGebiet>, Serializable {

  private static final long serialVersionUID = 1L;

  // WASDE-1334
  private final GruppeNamenComparator gruppeNamenComparator;

  public GruppeImGebietByNameAufGebietComparator() {
    super();
    gruppeNamenComparator = GruppeNamenComparator.DEFAULT_INSTANCE;
  }

  public GruppeImGebietByNameAufGebietComparator(GruppeNamenComparator gruppeNamenComparator) {
    super();
    this.gruppeNamenComparator = gruppeNamenComparator;
  }

  @Override
  public int compare(GruppeImGebiet one, GruppeImGebiet another) {
    if (one != null && another != null) {
      int comparedByNameAufGebiet = compareByNameAufGebiet(one.getNameAufGebiet(), another.getNameAufGebiet());

      if (comparedByNameAufGebiet == 0) {
        return one.getPk().compareTo(another.getPk());
      }

      return comparedByNameAufGebiet;
    }

    return NullComparator.NON_NULL_BEFORE_NULL_COMPARATOR.compare(one, another);
  }

  private int compareByNameAufGebiet(String one, String another) {
    if (one == null) {
      if (another == null) {
        return 0;
      }

      return 1;
    }

    if (another == null) {
      return -1;
    }

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