Problem with correlation between MessageFormat and ChoiceFormat with usage Format[] objects

36 views Asked by At

It's my first time with MessageFormat class, and I'm confuse.

I have that method which print result from my Stock list.I had IllegalArgumentException, I had to add one "null" to

Format[] testFormats = {null, null, dateFormat, fileform};

Now it's works, but I don't understand this structure.

public static void printStocks(List<Stock> stocks, Date actualDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

        double[] filelimits = {0d, actualDate.getTime()};
        String[] filepart = {"change {4}", "open {2} and last {3}"};

        ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
        Format[] testFormats = {null, null, dateFormat, fileform};
        MessageFormat pattform = new MessageFormat("{0}    {1} | {5} {6}");

        pattform.setFormats(testFormats);

        Object[] testArgs = {name, symbol, open, last, change, date, date.getTime()};
public static class Stock extends HashMap<String, Object> {
        public Stock(String name, String symbol, double open, double last) {
            put("name", name);
            put("symbol", symbol);
            put("open", open);
            put("last", last);
            put("date", getRandomDate(2020));
        }

        public Stock(String name, String symbol, double change, Date date) {
            put("name", name);
            put("symbol", symbol);
            put("date", date);
            put("change", change);
        }
    }

Example output:

Apple Inc.    AAPL | 20-09-2047 open 125.64 and last 123.43
Fake Applied Materials, Inc.   AMAT | 15-01-1983 change 0.26

I suppose that {0} is related to "name", {1} to "symbol", {5} and {6} are correlated somehow with ChoiseFormat object which decide to use version from

String[] filepart = {"change {4}", "open {2} and last {3}"};

and Format[] testFormats four elements I suppose are correlated with four elements

("{0}    {1} | {5} {6}")

How to grasp and better understand connection and cooperation ChoiceFormat with MessageFormat?

0

There are 0 answers