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

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

import de.ivu.elect.business.wertelisten.entity.Wertelisten;
import de.ivu.elect.business.wertelisten.entity.WertelistenArt;
import de.ivu.elect.util.CaseAndDiacriticIgnoringComparator;

public class WertelistenByWertelistenArtAndNameComparator implements Comparator<Wertelisten>, Serializable {

  private static final long serialVersionUID = 1L;

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

  @Override
  public int compare(Wertelisten one, Wertelisten another) {
    if (one != null && another != null) {
      int comparedByWertelistenArt = compare(one.getWertelistenArt(), another.getWertelistenArt());

      if (comparedByWertelistenArt == 0) {
        return compare(one.getName(), another.getName());
      }

      return comparedByWertelistenArt;
    }

    if (one == null) {
      if (another == null) {
        return 0;
      }

      return -1;
    }

    return 1;
  }

  private int compare(WertelistenArt one, WertelistenArt another) {
    if (one != null && another != null) {
      return one.name().compareTo(another.name());
    }

    if (one == null) {
      if (another == null) {
        return 0;
      }

      return -1;
    }

    return 1;
  }

  private int compare(String one, String another) {
    return NAME_COMPARATOR.compare(one, another);
  }
}
