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

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

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

import de.ivu.elect.business.geodata.entity.GeodataOrt;
import de.ivu.elect.util.CaseAndDiacriticIgnoringComparator;

public class GeodataOrtByNamenComparator implements Comparator<GeodataOrt>, Serializable {

  private static final long serialVersionUID = 1L;

  private static final Comparator<String> NAME_COMPARATOR = CaseAndDiacriticIgnoringComparator.DEFAULT_INSTANCE;

  public GeodataOrtByNamenComparator() {
    super();
  }

  @Override
  public int compare(GeodataOrt one, GeodataOrt another) {
    if (nonNull(one) && nonNull(another)) {
      int comparedByOrtsname = NAME_COMPARATOR.compare(one.getOrtsname(), another.getOrtsname());

      if (comparedByOrtsname == 0) {
        return NAME_COMPARATOR.compare(one.getOrtsteilname(), another.getOrtsteilname());
      }

      return comparedByOrtsname;
    }

    if (isNull(one)) {
      if (isNull(another)) {
        return 0;
      }

      return -1;
    }

    return 1;
  }
}
