
package de.ivu.elect.util;

import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;

// Bei BugFix/Verbesserung auch CaseAndDiacriticIgnoringComparator in de.ivu.bundeswahlleiter:bundeswahlleiter-meta anpassen!
public class CaseAndDiacriticIgnoringComparator extends AbstractCollatorComparator {

  private static final long serialVersionUID = 1L;

  private static final Locale DEFAULT_LOCALE = Locale.getDefault();

  private static final String DEFAULT_COLLATOR_RULES = CaseAndDiacriticIgnoringNormalizer.COLLATOR_RULES_DIN_5007_VAR_1;

  /**
   * Ä == A und Diakritische Zeichen normalisiert
   */
  public static final Comparator<String> DIN_5007_VAR_1 = new CaseAndDiacriticIgnoringComparator(CaseAndDiacriticIgnoringNormalizer.COLLATOR_RULES_DIN_5007_VAR_1);

  /**
   * Ä == Ae und Diakritische Zeichen normalisiert
   */
  public static final Comparator<String> DIN_5007_VAR_2 = new CaseAndDiacriticIgnoringComparator(CaseAndDiacriticIgnoringNormalizer.COLLATOR_RULES_DIN_5007_VAR_2);

  /**
   * @see CaseAndDiacriticIgnoringComparator#DIN_5007_VAR_1
   */
  public static final Comparator<String> DEFAULT_INSTANCE = DIN_5007_VAR_1;

  private String collatorRules;

  public CaseAndDiacriticIgnoringComparator() {
    this(DEFAULT_LOCALE, DEFAULT_COLLATOR_RULES);
  }

  public CaseAndDiacriticIgnoringComparator(Locale locale) {
    this(locale, DEFAULT_COLLATOR_RULES);
  }

  public CaseAndDiacriticIgnoringComparator(String collatorRules) {
    this(DEFAULT_LOCALE, collatorRules);
  }

  public CaseAndDiacriticIgnoringComparator(Locale locale, String collatorRules) {
    this(locale, collatorRules, "true".equals(System.getProperty("CaseAndDiacriticIgnoringComparator.COLLATOR_RULES_ENABLED", "true")));

    this.collatorRules = collatorRules;
  }

  public CaseAndDiacriticIgnoringComparator(Locale locale, String collatorRules, boolean collatorRulesEnabled) {
    super(AbstractCollatorComparator.createCollator(locale, Collator.PRIMARY, collatorRulesEnabled ? collatorRules : ""));
  }

  public String getCollatorRules() {
    return collatorRules;
  }
}
