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
025 /**
026 * Base class for all exceptions that signal a mismatch between the
027 * current state and the user's expectations.
028 *
029 * @since 2.2
030 * @version $Revision: 1061496 $ $Date: 2011-01-20 21:32:16 +0100 (jeu. 20 janv. 2011) $
031 */
032 public class MathIllegalStateException extends IllegalStateException implements MathThrowable {
033
034 /** Serializable version Id. */
035 private static final long serialVersionUID = -6024911025449780478L;
036
037 /**
038 * Pattern used to build the message (specific context).
039 */
040 private final Localizable specific;
041 /**
042 * Pattern used to build the message (general problem description).
043 */
044 private final Localizable general;
045 /**
046 * Arguments used to build the message.
047 */
048 private final Object[] arguments;
049
050 /**
051 * Simple constructor.
052 * @param specific Message pattern providing the specific context of
053 * the error.
054 * @param general Message pattern explaining the cause of the error.
055 * @param args Arguments.
056 */
057 public MathIllegalStateException(Localizable specific,
058 Localizable general,
059 Object ... args) {
060 this(null, specific, general, args);
061 }
062
063 /**
064 * Simple constructor.
065 * @param cause root cause
066 * @param specific Message pattern providing the specific context of
067 * the error.
068 * @param general Message pattern explaining the cause of the error.
069 * @param args Arguments.
070 */
071 public MathIllegalStateException(Throwable cause,
072 Localizable specific,
073 Localizable general,
074 Object ... args) {
075 super(cause);
076 this.specific = specific;
077 this.general = general;
078 arguments = ArgUtils.flatten(args);
079 }
080
081 /**
082 * @param general Message pattern explaining the cause of the error.
083 * @param args Arguments.
084 */
085 public MathIllegalStateException(Localizable general,
086 Object ... args) {
087 this(null, null, general, args);
088 }
089
090 /**
091 * Simple constructor.
092 * @param cause root cause
093 * @param general Message pattern explaining the cause of the error.
094 * @param args Arguments.
095 */
096 public MathIllegalStateException(Throwable cause,
097 Localizable general,
098 Object ... args) {
099 this(cause, null, general, args);
100 }
101
102 /** {@inheritDoc} */
103 public Localizable getSpecificPattern() {
104 return specific;
105 }
106
107 /** {@inheritDoc} */
108 public Localizable getGeneralPattern() {
109 return general;
110 }
111
112 /** {@inheritDoc} */
113 public Object[] getArguments() {
114 return arguments.clone();
115 }
116
117 /**
118 * Get the message in a specified locale.
119 *
120 * @param locale Locale in which the message should be translated.
121 *
122 * @return the localized message.
123 */
124 public String getMessage(final Locale locale) {
125 return MessageFactory.buildMessage(locale, specific, general, arguments);
126 }
127
128 /** {@inheritDoc} */
129 @Override
130 public String getMessage() {
131 return getMessage(Locale.US);
132 }
133
134 /** {@inheritDoc} */
135 @Override
136 public String getLocalizedMessage() {
137 return getMessage(Locale.getDefault());
138 }
139
140 }