Decomposition.java Source code

Java tutorial

Introduction

Here is the source code for Decomposition.java

Source

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