Java Code Examples for java.text.ChoiceFormat#format()

The following examples show how to use java.text.ChoiceFormat#format() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Bug4387255.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ChoiceFormat choiceFormat1 = new ChoiceFormat(doubles, strings);
    ChoiceFormat choiceFormat2 = new ChoiceFormat(pattern);
    if (!choiceFormat1.equals(choiceFormat2)) {
        System.out.println("choiceFormat1: " + choiceFormat1.toPattern());
        System.out.println("choiceFormat2: " + choiceFormat2.toPattern());
        throw new RuntimeException();
    }

    for (int i = 0; i < doubles.length; i++) {
        String result = choiceFormat2.format(doubles[i]);
        if (!result.equals(strings[i])) {
            throw new RuntimeException("Wrong format result - expected " +
                    strings[i] + ", got " + result);
        }
    }
}
 
Example 2
Source File: Bug4387255.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ChoiceFormat choiceFormat1 = new ChoiceFormat(doubles, strings);
    ChoiceFormat choiceFormat2 = new ChoiceFormat(pattern);
    if (!choiceFormat1.equals(choiceFormat2)) {
        System.out.println("choiceFormat1: " + choiceFormat1.toPattern());
        System.out.println("choiceFormat2: " + choiceFormat2.toPattern());
        throw new RuntimeException();
    }

    for (int i = 0; i < doubles.length; i++) {
        String result = choiceFormat2.format(doubles[i]);
        if (!result.equals(strings[i])) {
            throw new RuntimeException("Wrong format result - expected " +
                    strings[i] + ", got " + result);
        }
    }
}
 
Example 3
Source File: Bug4387255.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ChoiceFormat choiceFormat1 = new ChoiceFormat(doubles, strings);
    ChoiceFormat choiceFormat2 = new ChoiceFormat(pattern);
    if (!choiceFormat1.equals(choiceFormat2)) {
        System.out.println("choiceFormat1: " + choiceFormat1.toPattern());
        System.out.println("choiceFormat2: " + choiceFormat2.toPattern());
        throw new RuntimeException();
    }

    for (int i = 0; i < doubles.length; i++) {
        String result = choiceFormat2.format(doubles[i]);
        if (!result.equals(strings[i])) {
            throw new RuntimeException("Wrong format result - expected " +
                    strings[i] + ", got " + result);
        }
    }
}
 
Example 4
Source File: Bug4387255.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ChoiceFormat choiceFormat1 = new ChoiceFormat(doubles, strings);
    ChoiceFormat choiceFormat2 = new ChoiceFormat(pattern);
    if (!choiceFormat1.equals(choiceFormat2)) {
        System.out.println("choiceFormat1: " + choiceFormat1.toPattern());
        System.out.println("choiceFormat2: " + choiceFormat2.toPattern());
        throw new RuntimeException();
    }

    for (int i = 0; i < doubles.length; i++) {
        String result = choiceFormat2.format(doubles[i]);
        if (!result.equals(strings[i])) {
            throw new RuntimeException("Wrong format result - expected " +
                    strings[i] + ", got " + result);
        }
    }
}
 
Example 5
Source File: Bug4387255.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ChoiceFormat choiceFormat1 = new ChoiceFormat(doubles, strings);
    ChoiceFormat choiceFormat2 = new ChoiceFormat(pattern);
    if (!choiceFormat1.equals(choiceFormat2)) {
        System.out.println("choiceFormat1: " + choiceFormat1.toPattern());
        System.out.println("choiceFormat2: " + choiceFormat2.toPattern());
        throw new RuntimeException();
    }

    for (int i = 0; i < doubles.length; i++) {
        String result = choiceFormat2.format(doubles[i]);
        if (!result.equals(strings[i])) {
            throw new RuntimeException("Wrong format result - expected " +
                    strings[i] + ", got " + result);
        }
    }
}
 
Example 6
Source File: MessageRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void Test4106660()
{
    double[] limits = {3, 1, 2};
    String[] formats = {"Three", "One", "Two"};
    ChoiceFormat cf = new ChoiceFormat(limits, formats);
    double d = 5.0;
    String str = cf.format(d);
    if (!str.equals("Two"))
        errln("format(" + d + ") = " + cf.format(d));
}
 
Example 7
Source File: MessageRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * @bug 4142938
 * Test the applyPattern and toPattern handling of single quotes
 * by ChoiceFormat.  (This is in here because this was a bug reported
 * against MessageFormat.)  The single quote is used to quote the
 * pattern characters '|', '#', '<', and '\u2264'.  Two quotes in a row
 * is a quote literal.
 */
@Test
public void TestChoicePatternQuote() {
    String[] DATA = {
        // Pattern                  0 value           1 value
        "0#can''t|1#can",           "can't",          "can",
        "0#'pound(#)=''#'''|1#xyz", "pound(#)='#'",   "xyz",
        "0#'1<2 | 1\u22641'|1#''",  "1<2 | 1\u22641", "'",
    };
    for (int i=0; i<DATA.length; i+=3) {
        try {
            ChoiceFormat cf = new ChoiceFormat(DATA[i]);
            for (int j=0; j<=1; ++j) {
                String out = cf.format(j);
                if (!out.equals(DATA[i+1+j]))
                    errln("Fail: Pattern \"" + DATA[i] + "\" x "+j+" -> " +
                          out + "; want \"" + DATA[i+1+j] + '"');
            }
            String pat = cf.toPattern();
            String pat2 = new ChoiceFormat(pat).toPattern();
            if (!pat.equals(pat2))
                errln("Fail: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"');
            else
                logln("Ok: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"');
        }
        catch (IllegalArgumentException e) {
            errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
        }
    }
}
 
Example 8
Source File: ChoiceFormatTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * @tests java.text.ChoiceFormat#ChoiceFormat(double[], java.lang.String[])
 */
public void test_Constructor$D$Ljava_lang_String() {
    // Test for method java.text.ChoiceFormat(double [], java.lang.String
    // [])
    String formattedString;
    double[] appleLimits = { 1, 2, 3, 4, 5 };
    String[] appleFormats = { "Tiny Apple", "Small Apple", "Medium Apple",
            "Large Apple", "Huge Apple" };
    ChoiceFormat cf = new ChoiceFormat(appleLimits, appleFormats);

    formattedString = cf.format(Double.NEGATIVE_INFINITY);
    assertTrue("a) Incorrect format returned: " + formattedString,
            formattedString.equals("Tiny Apple"));
    formattedString = cf.format(0.5d);
    assertTrue("b) Incorrect format returned: " + formattedString,
            formattedString.equals("Tiny Apple"));
    formattedString = cf.format(1d);
    assertTrue("c) Incorrect format returned: " + formattedString,
            formattedString.equals("Tiny Apple"));
    formattedString = cf.format(1.5d);
    assertTrue("d) Incorrect format returned: " + formattedString,
            formattedString.equals("Tiny Apple"));
    formattedString = cf.format(2d);
    assertTrue("e) Incorrect format returned: " + formattedString,
            formattedString.equals("Small Apple"));
    formattedString = cf.format(2.5d);
    assertTrue("f) Incorrect format returned: " + formattedString,
            formattedString.equals("Small Apple"));
    formattedString = cf.format(3d);
    assertTrue("g) Incorrect format returned: " + formattedString,
            formattedString.equals("Medium Apple"));
    formattedString = cf.format(4d);
    assertTrue("h) Incorrect format returned: " + formattedString,
            formattedString.equals("Large Apple"));
    formattedString = cf.format(5d);
    assertTrue("i) Incorrect format returned: " + formattedString,
            formattedString.equals("Huge Apple"));
    formattedString = cf.format(5.5d);
    assertTrue("j) Incorrect format returned: " + formattedString,
            formattedString.equals("Huge Apple"));
    formattedString = cf.format(6.0d);
    assertTrue("k) Incorrect format returned: " + formattedString,
            formattedString.equals("Huge Apple"));
    formattedString = cf.format(Double.POSITIVE_INFINITY);
    assertTrue("l) Incorrect format returned: " + formattedString,
            formattedString.equals("Huge Apple"));
}
 
Example 9
Source File: ChoiceFormatTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * @tests java.text.ChoiceFormat#ChoiceFormat(java.lang.String)
 */
public void test_ConstructorLjava_lang_String() {
    // Test for method java.text.ChoiceFormat(java.lang.String)
    String formattedString;
    String patternString = "-2#Inverted Orange| 0#No Orange| 0<Almost No Orange| 1#Normal Orange| 2#Expensive Orange";
    ChoiceFormat cf = new ChoiceFormat(patternString);

    formattedString = cf.format(Double.NEGATIVE_INFINITY);
    assertTrue("a) Incorrect format returned: " + formattedString,
            formattedString.equals("Inverted Orange"));
    formattedString = cf.format(-3);
    assertTrue("b) Incorrect format returned: " + formattedString,
            formattedString.equals("Inverted Orange"));
    formattedString = cf.format(-2);
    assertTrue("c) Incorrect format returned: " + formattedString,
            formattedString.equals("Inverted Orange"));
    formattedString = cf.format(-1);
    assertTrue("d) Incorrect format returned: " + formattedString,
            formattedString.equals("Inverted Orange"));
    formattedString = cf.format(-0);
    assertTrue("e) Incorrect format returned: " + formattedString,
            formattedString.equals("No Orange"));
    formattedString = cf.format(0);
    assertTrue("f) Incorrect format returned: " + formattedString,
            formattedString.equals("No Orange"));
    formattedString = cf.format(0.1);
    assertTrue("g) Incorrect format returned: " + formattedString,
            formattedString.equals("Almost No Orange"));
    formattedString = cf.format(1);
    assertTrue("h) Incorrect format returned: " + formattedString,
            formattedString.equals("Normal Orange"));
    formattedString = cf.format(1.5);
    assertTrue("i) Incorrect format returned: " + formattedString,
            formattedString.equals("Normal Orange"));
    formattedString = cf.format(2);
    assertTrue("j) Incorrect format returned: " + formattedString,
            formattedString.equals("Expensive Orange"));
    formattedString = cf.format(3);
    assertTrue("k) Incorrect format returned: " + formattedString,
            formattedString.equals("Expensive Orange"));
    formattedString = cf.format(Double.POSITIVE_INFINITY);
    assertTrue("l) Incorrect format returned: " + formattedString,
            formattedString.equals("Expensive Orange"));

}