1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.math; 18 19 import java.io.PrintStream; 20 import java.io.PrintWriter; 21 import java.text.MessageFormat; 22 import java.util.Locale; 23 import java.util.MissingResourceException; 24 import java.util.ResourceBundle; 25 26 27 /** 28 * Base class for commons-math checked exceptions. 29 * <p> 30 * Supports nesting, emulating JDK 1.4 behavior if necessary.</p> 31 * <p> 32 * Adapted from <a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/FunctorException.html"/>.</p> 33 * 34 * @version $Revision: 780674 $ $Date: 2009-06-01 11:10:55 -0400 (Mon, 01 Jun 2009) $ 35 */ 36 public class MathException extends Exception { 37 38 /** Serializable version identifier. */ 39 private static final long serialVersionUID = -9004610152740737812L; 40 41 /** 42 * Pattern used to build the message. 43 */ 44 private final String pattern; 45 46 /** 47 * Arguments used to build the message. 48 */ 49 private final Object[] arguments; 50 51 /** 52 * Translate a string to a given locale. 53 * @param s string to translate 54 * @param locale locale into which to translate the string 55 * @return translated string or original string 56 * for unsupported locales or unknown strings 57 */ 58 private static String translate(String s, Locale locale) { 59 try { 60 ResourceBundle bundle = 61 ResourceBundle.getBundle("org.apache.commons.math.MessagesResources", locale); 62 if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) { 63 // the value of the resource is the translated string 64 return bundle.getString(s); 65 } 66 67 } catch (MissingResourceException mre) { 68 // do nothing here 69 } 70 71 // the locale is not supported or the resource is unknown 72 // don't translate and fall back to using the string as is 73 return s; 74 75 } 76 77 /** 78 * Builds a message string by from a pattern and its arguments. 79 * @param locale Locale in which the message should be translated 80 * @param pattern format specifier 81 * @param arguments format arguments 82 * @return a message string 83 */ 84 private static String buildMessage(Locale locale, String pattern, Object ... arguments) { 85 return (pattern == null) ? "" : new MessageFormat(translate(pattern, locale), locale).format(arguments); 86 } 87 88 /** 89 * Constructs a new <code>MathException</code> with no 90 * detail message. 91 */ 92 public MathException() { 93 super(); 94 this.pattern = null; 95 this.arguments = new Object[0]; 96 } 97 98 /** 99 * Constructs a new <code>MathException</code> with specified 100 * formatted detail message. 101 * Message formatting is delegated to {@link java.text.MessageFormat}. 102 * @param pattern format specifier 103 * @param arguments format arguments 104 */ 105 public MathException(String pattern, Object ... arguments) { 106 super(buildMessage(Locale.US, pattern, arguments)); 107 this.pattern = pattern; 108 this.arguments = (arguments == null) ? new Object[0] : arguments.clone(); 109 } 110 111 /** 112 * Constructs a new <code>MathException</code> with specified 113 * nested <code>Throwable</code> root cause. 114 * 115 * @param rootCause the exception or error that caused this exception 116 * to be thrown. 117 */ 118 public MathException(Throwable rootCause) { 119 super(rootCause); 120 this.pattern = getMessage(); 121 this.arguments = new Object[0]; 122 } 123 124 /** 125 * Constructs a new <code>MathException</code> with specified 126 * formatted detail message and nested <code>Throwable</code> root cause. 127 * Message formatting is delegated to {@link java.text.MessageFormat}. 128 * @param rootCause the exception or error that caused this exception 129 * to be thrown. 130 * @param pattern format specifier 131 * @param arguments format arguments 132 * @since 1.2 133 */ 134 public MathException(Throwable rootCause, String pattern, Object ... arguments) { 135 super(buildMessage(Locale.US, pattern, arguments), rootCause); 136 this.pattern = pattern; 137 this.arguments = (arguments == null) ? new Object[0] : arguments.clone(); 138 } 139 140 /** Gets the pattern used to build the message of this throwable. 141 * 142 * @return the pattern used to build the message of this throwable 143 * @since 1.2 144 */ 145 public String getPattern() { 146 return pattern; 147 } 148 149 /** Gets the arguments used to build the message of this throwable. 150 * 151 * @return the arguments used to build the message of this throwable 152 * @since 1.2 153 */ 154 public Object[] getArguments() { 155 return arguments.clone(); 156 } 157 158 /** Gets the message in a specified locale. 159 * 160 * @param locale Locale in which the message should be translated 161 * 162 * @return localized message 163 * @since 1.2 164 */ 165 public String getMessage(Locale locale) { 166 return buildMessage(locale, pattern, arguments); 167 } 168 169 /** {@inheritDoc} */ 170 @Override 171 public String getLocalizedMessage() { 172 return getMessage(Locale.getDefault()); 173 } 174 175 /** 176 * Prints the stack trace of this exception to the standard error stream. 177 */ 178 @Override 179 public void printStackTrace() { 180 printStackTrace(System.err); 181 } 182 183 /** 184 * Prints the stack trace of this exception to the specified stream. 185 * 186 * @param out the <code>PrintStream</code> to use for output 187 */ 188 @Override 189 public void printStackTrace(PrintStream out) { 190 synchronized (out) { 191 PrintWriter pw = new PrintWriter(out, false); 192 printStackTrace(pw); 193 // Flush the PrintWriter before it's GC'ed. 194 pw.flush(); 195 } 196 } 197 198 }