Collator.FULL_DECOMPOSITION : Collator « java.text « Java by API






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();
  }
}

           
       








Related examples in the same category

1.Collator.IDENTICAL
2.Collator.NO_DECOMPOSITION
3.Collator.CANONICAL_DECOMPOSITION
4.Collator.SECONDARY
5.Collator: getCollationKey(String source)
6.Collator: getInstance(Locale desiredLocale)
7.Collator.getInstance()
8.Collator: setDecomposition(int decompositionMode)
9.Collator: setStrength(int newStrength)