Java by API/java.text/Collator — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

Collator.CANONICAL_DECOMPOSITION

/*
 CANONICAL DECOMPOSITION: The strings are equal.
 FULL DECOMPOSITION: The strings are equal.
Comparing a + umlaut and precomposed a-umlaut
 NO DECOMPOSITION: The strings are NOT equal.
 CANONICAL DECOMPOSITION: The strings are equal.
 FULL DECOMPOSITION: The strings are equal.
*/
import java.text.Collator;
import java.util.Locale;
public class Decomposition {
  public void compare(Collator c, String a, String b) {
    switch (c.getDecomposition()) {
    case Collator.NO_DECOMPOSITION:
      System.out.print(" NO DECOMPOSITION: ");
      break;
    case Collator.CANONICAL_DECOMPOSITION:
      System.out.print(" CANONICAL DECOMPOSITION: ");
      break;
    case Collator.FULL_DECOMPOSITION:
      System.out.print(" FULL DECOMPOSITION: ");
      break;
    default:
      System.out.print(" UNKNOWN DECOMPOSITION: ");
    }
    if (c.equals(a, b))
      System.out.println("The strings are equal.");
    else
      System.out.println("The strings are NOT equal.");
  }
  public Decomposition() {
    String pairs[][] = new String[3][3];
    pairs[0][0] = "Half-Width and full-width A";
    pairs[0][1] = "A";
    pairs[0][2] = "\uFF21"; // full-width A
    pairs[1][0] = "A with Ring and Angstrom Sign";
    pairs[1][1] = "\u00c5"; // A with ring
    pairs[1][2] = "\u212b"; // Angstrom
    pairs[2][0] = "a + umlaut and precomposed a-umlaut";
    pairs[2][1] = "a\u0308";
    pairs[2][2] = "\u00e4";
    for (int i = 0; i < 3; i++) {
      Collator collate = Collator.getInstance(Locale.US);
      collate.setStrength(Collator.IDENTICAL);
      System.out.println("Comparing " + pairs[i][0]);
      collate.setDecomposition(Collator.NO_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      collate.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      collate.setDecomposition(Collator.FULL_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      System.out.println("");
    }
  }
  public static void main(String[] s) {
    new Decomposition();
  }
}





Collator.FULL_DECOMPOSITION

/*
 CANONICAL DECOMPOSITION: The strings are equal.
 FULL DECOMPOSITION: The strings are equal.
Comparing a + umlaut and precomposed a-umlaut
 NO DECOMPOSITION: The strings are NOT equal.
 CANONICAL DECOMPOSITION: The strings are equal.
 FULL DECOMPOSITION: The strings are equal.
*/
import java.text.Collator;
import java.util.Locale;
public class Decomposition {
  public void compare(Collator c, String a, String b) {
    switch (c.getDecomposition()) {
    case Collator.NO_DECOMPOSITION:
      System.out.print(" NO DECOMPOSITION: ");
      break;
    case Collator.CANONICAL_DECOMPOSITION:
      System.out.print(" CANONICAL DECOMPOSITION: ");
      break;
    case Collator.FULL_DECOMPOSITION:
      System.out.print(" FULL DECOMPOSITION: ");
      break;
    default:
      System.out.print(" UNKNOWN DECOMPOSITION: ");
    }
    if (c.equals(a, b))
      System.out.println("The strings are equal.");
    else
      System.out.println("The strings are NOT equal.");
  }
  public Decomposition() {
    String pairs[][] = new String[3][3];
    pairs[0][0] = "Half-Width and full-width A";
    pairs[0][1] = "A";
    pairs[0][2] = "\uFF21"; // full-width A
    pairs[1][0] = "A with Ring and Angstrom Sign";
    pairs[1][1] = "\u00c5"; // A with ring
    pairs[1][2] = "\u212b"; // Angstrom
    pairs[2][0] = "a + umlaut and precomposed a-umlaut";
    pairs[2][1] = "a\u0308";
    pairs[2][2] = "\u00e4";
    for (int i = 0; i < 3; i++) {
      Collator collate = Collator.getInstance(Locale.US);
      collate.setStrength(Collator.IDENTICAL);
      System.out.println("Comparing " + pairs[i][0]);
      collate.setDecomposition(Collator.NO_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      collate.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      collate.setDecomposition(Collator.FULL_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      System.out.println("");
    }
  }
  public static void main(String[] s) {
    new Decomposition();
  }
}





Collator: getCollationKey(String source)

import java.text.CollationKey;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
public class MainClass {
  public static void main(String[] args) {
    // Build a vector of words to be sorted
    ArrayList list = new ArrayList();
    list.add("m");
    list.add("c2");
    list.add("e");
    list.add("c1");
    Collator collate = Collator.getInstance();
    CollationKey[] keys = new CollationKey[list.size()];
    for (int k = 0; k < list.size(); k ++)
      keys[k] = collate.getCollationKey((String)list.get(k));
    Arrays.sort(keys);
    for (int l= 0;l < keys.length; l++) {
      System.out.println(keys[l].getSourceString());
    }
  }
}





Collator.getInstance()

import java.text.CollationKey;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
public class MainClass {
  public static void main(String[] args) {
    // Build a vector of words to be sorted
    ArrayList list = new ArrayList();
    list.add("m");
    list.add("c2");
    list.add("e");
    list.add("c1");
    Collator collate = Collator.getInstance();
    CollationKey[] keys = new CollationKey[list.size()];
    for (int k = 0; k < list.size(); k ++)
      keys[k] = collate.getCollationKey((String)list.get(k));
    Arrays.sort(keys);
    for (int l= 0;l < keys.length; l++) {
      System.out.println(keys[l].getSourceString());
    }
  }
}





Collator: getInstance(Locale desiredLocale)

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import javax.swing.JLabel;
public class MainClass {
  public static void main(String[] args) {
    ArrayList list = new ArrayList();
    list.add("\u00e4pple");
    list.add("banan");
    list.add("p\u00e4ron");
    list.add("orange");
    // Obtain a Swedish collator
    Collator collate = Collator.getInstance(new Locale("sv", ""));
    Collections.sort(list, collate);
    for (int i= 0;i < list.size(); i++) {
      System.out.println(list.get(i));
    }
  }
}





Collator.IDENTICAL

/*
 CANONICAL DECOMPOSITION: The strings are equal.
 FULL DECOMPOSITION: The strings are equal.
Comparing a + umlaut and precomposed a-umlaut
 NO DECOMPOSITION: The strings are NOT equal.
 CANONICAL DECOMPOSITION: The strings are equal.
 FULL DECOMPOSITION: The strings are equal.
*/
import java.text.Collator;
import java.util.Locale;
public class Decomposition {
  public void compare(Collator c, String a, String b) {
    switch (c.getDecomposition()) {
    case Collator.NO_DECOMPOSITION:
      System.out.print(" NO DECOMPOSITION: ");
      break;
    case Collator.CANONICAL_DECOMPOSITION:
      System.out.print(" CANONICAL DECOMPOSITION: ");
      break;
    case Collator.FULL_DECOMPOSITION:
      System.out.print(" FULL DECOMPOSITION: ");
      break;
    default:
      System.out.print(" UNKNOWN DECOMPOSITION: ");
    }
    if (c.equals(a, b))
      System.out.println("The strings are equal.");
    else
      System.out.println("The strings are NOT equal.");
  }
  public Decomposition() {
    String pairs[][] = new String[3][3];
    pairs[0][0] = "Half-Width and full-width A";
    pairs[0][1] = "A";
    pairs[0][2] = "\uFF21"; // full-width A
    pairs[1][0] = "A with Ring and Angstrom Sign";
    pairs[1][1] = "\u00c5"; // A with ring
    pairs[1][2] = "\u212b"; // Angstrom
    pairs[2][0] = "a + umlaut and precomposed a-umlaut";
    pairs[2][1] = "a\u0308";
    pairs[2][2] = "\u00e4";
    for (int i = 0; i < 3; i++) {
      Collator collate = Collator.getInstance(Locale.US);
      collate.setStrength(Collator.IDENTICAL);
      System.out.println("Comparing " + pairs[i][0]);
      collate.setDecomposition(Collator.NO_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      collate.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      collate.setDecomposition(Collator.FULL_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      System.out.println("");
    }
  }
  public static void main(String[] s) {
    new Decomposition();
  }
}





Collator.NO_DECOMPOSITION

/*
 CANONICAL DECOMPOSITION: The strings are equal.
 FULL DECOMPOSITION: The strings are equal.
Comparing a + umlaut and precomposed a-umlaut
 NO DECOMPOSITION: The strings are NOT equal.
 CANONICAL DECOMPOSITION: The strings are equal.
 FULL DECOMPOSITION: The strings are equal.
*/
import java.text.Collator;
import java.util.Locale;
public class Decomposition {
  public void compare(Collator c, String a, String b) {
    switch (c.getDecomposition()) {
    case Collator.NO_DECOMPOSITION:
      System.out.print(" NO DECOMPOSITION: ");
      break;
    case Collator.CANONICAL_DECOMPOSITION:
      System.out.print(" CANONICAL DECOMPOSITION: ");
      break;
    case Collator.FULL_DECOMPOSITION:
      System.out.print(" FULL DECOMPOSITION: ");
      break;
    default:
      System.out.print(" UNKNOWN DECOMPOSITION: ");
    }
    if (c.equals(a, b))
      System.out.println("The strings are equal.");
    else
      System.out.println("The strings are NOT equal.");
  }
  public Decomposition() {
    String pairs[][] = new String[3][3];
    pairs[0][0] = "Half-Width and full-width A";
    pairs[0][1] = "A";
    pairs[0][2] = "\uFF21"; // full-width A
    pairs[1][0] = "A with Ring and Angstrom Sign";
    pairs[1][1] = "\u00c5"; // A with ring
    pairs[1][2] = "\u212b"; // Angstrom
    pairs[2][0] = "a + umlaut and precomposed a-umlaut";
    pairs[2][1] = "a\u0308";
    pairs[2][2] = "\u00e4";
    for (int i = 0; i < 3; i++) {
      Collator collate = Collator.getInstance(Locale.US);
      collate.setStrength(Collator.IDENTICAL);
      System.out.println("Comparing " + pairs[i][0]);
      collate.setDecomposition(Collator.NO_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      collate.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      collate.setDecomposition(Collator.FULL_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      System.out.println("");
    }
  }
  public static void main(String[] s) {
    new Decomposition();
  }
}





Collator.SECONDARY

import java.text.CollationElementIterator;
import java.text.Collator;
import java.text.RuleBasedCollator;
public class Search {
  public static int indexOf(String source, String pattern) {
    RuleBasedCollator rbc = (RuleBasedCollator) Collator.getInstance();
    rbc.setStrength(Collator.SECONDARY);
    CollationElementIterator textCEI;
    CollationElementIterator patCEI;
    textCEI = rbc.getCollationElementIterator(source);
    patCEI = rbc.getCollationElementIterator(pattern);
    // e1 will contain the collation element for the source
    // e2 will contain the collation element for the pattern
    int e1, e2;
    int startMatch = -1;
    // initialize e2 with the first collation element in the pattern
    e2 = patCEI.next();
    while ((e1 = textCEI.next()) != CollationElementIterator.NULLORDER) {
      if (e1 == e2) { // if the elements match
        if (startMatch == -1)
          startMatch = textCEI.getOffset();
        e2 = patCEI.next(); // increment to the next element
        if (e2 == CollationElementIterator.NULLORDER)
          break;
      } else { // elements do not match
        if (startMatch != -1) {
          patCEI.reset();
          e2 = patCEI.next();
          startMatch = -1;
        }
      }
    }
    return startMatch;
  }
  public static void main(String[] args) {
    String text = "Wie hei\u00DFen Sie?"; // Wie hei&szlig;en Sie?
    String pattern = "heissen";
    int index = indexOf(text, pattern);
    if (index != -1)
      System.out.println("Found a match at: " + index);
    else
      System.out.println("No match found!");
  }
}





Collator: setDecomposition(int decompositionMode)

/*
 CANONICAL DECOMPOSITION: The strings are equal.
 FULL DECOMPOSITION: The strings are equal.
Comparing a + umlaut and precomposed a-umlaut
 NO DECOMPOSITION: The strings are NOT equal.
 CANONICAL DECOMPOSITION: The strings are equal.
 FULL DECOMPOSITION: The strings are equal.
*/
import java.text.Collator;
import java.util.Locale;
public class Decomposition {
  public void compare(Collator c, String a, String b) {
    switch (c.getDecomposition()) {
    case Collator.NO_DECOMPOSITION:
      System.out.print(" NO DECOMPOSITION: ");
      break;
    case Collator.CANONICAL_DECOMPOSITION:
      System.out.print(" CANONICAL DECOMPOSITION: ");
      break;
    case Collator.FULL_DECOMPOSITION:
      System.out.print(" FULL DECOMPOSITION: ");
      break;
    default:
      System.out.print(" UNKNOWN DECOMPOSITION: ");
    }
    if (c.equals(a, b))
      System.out.println("The strings are equal.");
    else
      System.out.println("The strings are NOT equal.");
  }
  public Decomposition() {
    String pairs[][] = new String[3][3];
    pairs[0][0] = "Half-Width and full-width A";
    pairs[0][1] = "A";
    pairs[0][2] = "\uFF21"; // full-width A
    pairs[1][0] = "A with Ring and Angstrom Sign";
    pairs[1][1] = "\u00c5"; // A with ring
    pairs[1][2] = "\u212b"; // Angstrom
    pairs[2][0] = "a + umlaut and precomposed a-umlaut";
    pairs[2][1] = "a\u0308";
    pairs[2][2] = "\u00e4";
    for (int i = 0; i < 3; i++) {
      Collator collate = Collator.getInstance(Locale.US);
      collate.setStrength(Collator.IDENTICAL);
      System.out.println("Comparing " + pairs[i][0]);
      collate.setDecomposition(Collator.NO_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      collate.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      collate.setDecomposition(Collator.FULL_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      System.out.println("");
    }
  }
  public static void main(String[] s) {
    new Decomposition();
  }
}





Collator: setStrength(int newStrength)

/*
 CANONICAL DECOMPOSITION: The strings are equal.
 FULL DECOMPOSITION: The strings are equal.
Comparing a + umlaut and precomposed a-umlaut
 NO DECOMPOSITION: The strings are NOT equal.
 CANONICAL DECOMPOSITION: The strings are equal.
 FULL DECOMPOSITION: The strings are equal.
*/
import java.text.Collator;
import java.util.Locale;
public class Decomposition {
  public void compare(Collator c, String a, String b) {
    switch (c.getDecomposition()) {
    case Collator.NO_DECOMPOSITION:
      System.out.print(" NO DECOMPOSITION: ");
      break;
    case Collator.CANONICAL_DECOMPOSITION:
      System.out.print(" CANONICAL DECOMPOSITION: ");
      break;
    case Collator.FULL_DECOMPOSITION:
      System.out.print(" FULL DECOMPOSITION: ");
      break;
    default:
      System.out.print(" UNKNOWN DECOMPOSITION: ");
    }
    if (c.equals(a, b))
      System.out.println("The strings are equal.");
    else
      System.out.println("The strings are NOT equal.");
  }
  public Decomposition() {
    String pairs[][] = new String[3][3];
    pairs[0][0] = "Half-Width and full-width A";
    pairs[0][1] = "A";
    pairs[0][2] = "\uFF21"; // full-width A
    pairs[1][0] = "A with Ring and Angstrom Sign";
    pairs[1][1] = "\u00c5"; // A with ring
    pairs[1][2] = "\u212b"; // Angstrom
    pairs[2][0] = "a + umlaut and precomposed a-umlaut";
    pairs[2][1] = "a\u0308";
    pairs[2][2] = "\u00e4";
    for (int i = 0; i < 3; i++) {
      Collator collate = Collator.getInstance(Locale.US);
      collate.setStrength(Collator.IDENTICAL);
      System.out.println("Comparing " + pairs[i][0]);
      collate.setDecomposition(Collator.NO_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      collate.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      collate.setDecomposition(Collator.FULL_DECOMPOSITION);
      compare(collate, pairs[i][1], pairs[i][2]);
      System.out.println("");
    }
  }
  public static void main(String[] s) {
    new Decomposition();
  }
}