001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.exception.util;
018    
019    import java.text.MessageFormat;
020    import java.util.Locale;
021    
022    /**
023     * Class for constructing localized messages.
024     *
025     * @since 2.2
026     * @version $Revision$ $Date$
027     */
028    public class MessageFactory {
029        /**
030         * Class contains only static methods.
031         */
032        private MessageFactory() {}
033    
034        /**
035         * Builds a message string by from a pattern and its arguments.
036         *
037         * @param locale Locale in which the message should be translated.
038         * @param pattern Format specifier.
039         * @param arguments Format arguments.
040         * @return a localized message string.
041         */
042        public static String buildMessage(Locale locale,
043                                          Localizable pattern,
044                                          Object ... arguments) {
045            return buildMessage(locale, null, pattern, arguments);
046        }
047    
048        /**
049         * Builds a message string by from two patterns (specific and general) and
050         * an argument list.
051         *
052         * @param locale Locale in which the message should be translated.
053         * @param specific Format specifier (may be null).
054         * @param general Format specifier (may be null).
055         * @param arguments Format arguments. They will be substituted in
056         * <em>both</em> the {@code general} and {@code specific} format specifiers.
057         * @return a localized message string.
058         */
059        public static String buildMessage(Locale locale,
060                                          Localizable specific,
061                                          Localizable general,
062                                          Object ... arguments) {
063            final StringBuilder sb = new StringBuilder();
064            if (general != null) {
065                final MessageFormat fmt = new MessageFormat(general.getLocalizedString(locale), locale);
066                sb.append(fmt.format(arguments));
067            }
068            if (specific != null) {
069                if (general != null) {
070                    sb.append(": ");
071                }
072                final MessageFormat fmt = new MessageFormat(specific.getLocalizedString(locale), locale);
073                sb.append(fmt.format(arguments));
074            }
075    
076            return sb.toString();
077        }
078    }