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.exception; 18 19 import java.util.Locale; 20 21 import org.apache.commons.math.exception.util.ArgUtils; 22 import org.apache.commons.math.exception.util.MessageFactory; 23 import org.apache.commons.math.exception.util.Localizable; 24 25 /** 26 * Base class for all exceptions that signal a mismatch between the 27 * current state and the user's expectations. 28 * 29 * @since 2.2 30 * @version $Revision: 1061496 $ $Date: 2011-01-20 21:32:16 +0100 (jeu. 20 janv. 2011) $ 31 */ 32 public class MathIllegalStateException extends IllegalStateException implements MathThrowable { 33 34 /** Serializable version Id. */ 35 private static final long serialVersionUID = -6024911025449780478L; 36 37 /** 38 * Pattern used to build the message (specific context). 39 */ 40 private final Localizable specific; 41 /** 42 * Pattern used to build the message (general problem description). 43 */ 44 private final Localizable general; 45 /** 46 * Arguments used to build the message. 47 */ 48 private final Object[] arguments; 49 50 /** 51 * Simple constructor. 52 * @param specific Message pattern providing the specific context of 53 * the error. 54 * @param general Message pattern explaining the cause of the error. 55 * @param args Arguments. 56 */ 57 public MathIllegalStateException(Localizable specific, 58 Localizable general, 59 Object ... args) { 60 this(null, specific, general, args); 61 } 62 63 /** 64 * Simple constructor. 65 * @param cause root cause 66 * @param specific Message pattern providing the specific context of 67 * the error. 68 * @param general Message pattern explaining the cause of the error. 69 * @param args Arguments. 70 */ 71 public MathIllegalStateException(Throwable cause, 72 Localizable specific, 73 Localizable general, 74 Object ... args) { 75 super(cause); 76 this.specific = specific; 77 this.general = general; 78 arguments = ArgUtils.flatten(args); 79 } 80 81 /** 82 * @param general Message pattern explaining the cause of the error. 83 * @param args Arguments. 84 */ 85 public MathIllegalStateException(Localizable general, 86 Object ... args) { 87 this(null, null, general, args); 88 } 89 90 /** 91 * Simple constructor. 92 * @param cause root cause 93 * @param general Message pattern explaining the cause of the error. 94 * @param args Arguments. 95 */ 96 public MathIllegalStateException(Throwable cause, 97 Localizable general, 98 Object ... args) { 99 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 } 141