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

import java.util.Comparator;

import de.ivu.elect.business.search.entity.BenutzerSuchparameter;
import de.ivu.elect.util.CaseAndDiacriticIgnoringComparator;
import de.ivu.elect.util.NullComparator;

public class BenutzerSuchparameterByNameComparator implements Comparator<BenutzerSuchparameter> {

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

  public BenutzerSuchparameterByNameComparator() {
    super();
  }

  @Override
  public int compare(BenutzerSuchparameter one, BenutzerSuchparameter another) {
    if (one != null && another != null) {
      if (one.isSuchparameterAlsDefault()) {
        if (another.isSuchparameterAlsDefault()) {
          return 0;
        }

        return -1;
      }

      if (another.isSuchparameterAlsDefault()) {
        return 1;
      }

      return compareByName(one.getName(), another.getName());
    }

    return NULL_COMPARATOR.compare(one, another);
  }

  private int compareByName(String one, String another) {
    return CaseAndDiacriticIgnoringComparator.DEFAULT_INSTANCE.compare(one, another);
  }
}
