File: TextComparator.java

package info (click to toggle)
lib-xt-java 0.19990725-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,224 kB
  • ctags: 2,133
  • sloc: java: 9,665; makefile: 54; xml: 28
file content (38 lines) | stat: -rw-r--r-- 1,120 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.jclark.xsl.util;

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

public class TextComparator implements Comparator {
  private Collator collator;
  
  public static final int UPPER_FIRST = 1;
  public static final int LOWER_FIRST = 2;

  public static Comparator create(Locale locale, int caseOrder) {
    TextComparator cmp = new TextComparator(locale);
    // Might be better to handle this by diddling the rules
    // JDK does lower-first; it would be better not to assume this
    if ((caseOrder & UPPER_FIRST) == 0)
      return cmp;
    if (locale == null)
      locale = locale.getDefault();
    if (locale.getLanguage().equals("tr"))
      return new TurkishSwapCaseComparator(cmp);
    return new SwapCaseComparator(cmp);
  }

  private TextComparator(Locale locale) {
    if (locale == null)
      collator = Collator.getInstance();
    else
      collator = Collator.getInstance(locale);
    collator.setStrength(Collator.TERTIARY);
  }

  public int compare(Object obj1, Object obj2) {
    return collator.compare((String)obj1, (String)obj2);
  }

}