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.math3.exception;
018    
019    import org.apache.commons.math3.exception.util.Localizable;
020    import org.apache.commons.math3.exception.util.LocalizedFormats;
021    import org.apache.commons.math3.exception.util.ExceptionContext;
022    import org.apache.commons.math3.exception.util.ExceptionContextProvider;
023    
024    /**
025     * Base class for all exceptions that signal a mismatch between the
026     * current state and the user's expectations.
027     *
028     * @since 2.2
029     * @version $Id: MathIllegalStateException.java 1364378 2012-07-22 17:42:38Z tn $
030     */
031    public class MathIllegalStateException extends IllegalStateException
032        implements ExceptionContextProvider {
033        /** Serializable version Id. */
034        private static final long serialVersionUID = -6024911025449780478L;
035        /** Context. */
036        private final ExceptionContext context;
037    
038        /**
039         * Simple constructor.
040         *
041         * @param pattern Message pattern explaining the cause of the error.
042         * @param args Arguments.
043         */
044        public MathIllegalStateException(Localizable pattern,
045                                         Object ... args) {
046            context = new ExceptionContext(this);
047            context.addMessage(pattern, args);
048        }
049    
050        /**
051         * Simple constructor.
052         *
053         * @param cause Root cause.
054         * @param pattern Message pattern explaining the cause of the error.
055         * @param args Arguments.
056         */
057        public MathIllegalStateException(Throwable cause,
058                                         Localizable pattern,
059                                         Object ... args) {
060            super(cause);
061            context = new ExceptionContext(this);
062            context.addMessage(pattern, args);
063        }
064    
065        /**
066         * Default constructor.
067         */
068        public MathIllegalStateException() {
069            this(LocalizedFormats.ILLEGAL_STATE);
070        }
071    
072        /** {@inheritDoc} */
073        public ExceptionContext getContext() {
074            return context;
075        }
076    
077        /** {@inheritDoc} */
078        @Override
079        public String getMessage() {
080            return context.getMessage();
081        }
082    
083        /** {@inheritDoc} */
084        @Override
085        public String getLocalizedMessage() {
086            return context.getLocalizedMessage();
087        }
088    }