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;
018    
019    import java.util.Locale;
020    
021    import org.apache.commons.math.exception.util.ArgUtils;
022    import org.apache.commons.math.exception.util.MessageFactory;
023    import org.apache.commons.math.exception.util.Localizable;
024    import org.apache.commons.math.exception.util.LocalizedFormats;
025    
026    /**
027     * Base class for all unsupported features.
028     * It is used for all the exceptions that share the semantics of the standard
029     * {@link UnsupportedOperationException}, but must also provide a localized
030     * message.
031     *
032     * @since 2.2
033     * @version $Revision$ $Date$
034     */
035    public class MathUnsupportedOperationException extends UnsupportedOperationException implements MathThrowable {
036    
037        /** Serializable version Id. */
038        private static final long serialVersionUID = -6024911025449780478L;
039    
040        /**
041         * Pattern used to build the message (specific context).
042         */
043        private final Localizable specific;
044        /**
045         * Arguments used to build the message.
046         */
047        private final Object[] arguments;
048    
049        /**
050         * @param args Arguments.
051         */
052        public MathUnsupportedOperationException(Object ... args) {
053            this(null, args);
054        }
055        /**
056         * @param specific Message pattern providing the specific context of
057         * the error.
058         * @param args Arguments.
059         */
060        public MathUnsupportedOperationException(Localizable specific,
061                                                 Object ... args) {
062            this.specific = specific;
063            arguments = ArgUtils.flatten(args);
064        }
065    
066        /** {@inheritDoc} */
067        public Localizable getSpecificPattern() {
068            return specific;
069        }
070    
071        /** {@inheritDoc} */
072        public Localizable getGeneralPattern() {
073            return LocalizedFormats.UNSUPPORTED_OPERATION;
074        }
075    
076        /** {@inheritDoc} */
077        public Object[] getArguments() {
078            return arguments.clone();
079        }
080    
081        /**
082         * Get the message in a specified locale.
083         *
084         * @param locale Locale in which the message should be translated.
085         *
086         * @return the localized message.
087         */
088        public String getMessage(final Locale locale) {
089            return MessageFactory.buildMessage(locale,
090                                               specific,
091                                               LocalizedFormats.UNSUPPORTED_OPERATION,
092                                               arguments);
093        }
094    
095       /** {@inheritDoc} */
096        @Override
097        public String getMessage() {
098            return getMessage(Locale.US);
099        }
100    
101        /** {@inheritDoc} */
102        @Override
103        public String getLocalizedMessage() {
104            return getMessage(Locale.getDefault());
105        }
106    }