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 */ 17package org.apache.commons.math.exception; 18 19import java.util.Locale; 20 21import org.apache.commons.math.exception.util.ArgUtils; 22import org.apache.commons.math.exception.util.MessageFactory; 23import org.apache.commons.math.exception.util.Localizable; 24 25/** 26 * Base class for all preconditions violation exceptions. 27 * This class is not intended to be instantiated directly: it should serve 28 * as a base class to create all the exceptions that share the semantics of 29 * the standard {@link IllegalArgumentException}, but must also provide a 30 * localized message. 31 * 32 * @since 2.2 33 * @version $Revision$ $Date$ 34 */ 35public class MathIllegalArgumentException extends IllegalArgumentException implements MathThrowable { 36 37 /** Serializable version Id. */ 38 private static final long serialVersionUID = -6024911025449780478L; 39 40 /** 41 * Pattern used to build the message (specific context). 42 */ 43 private final Localizable specific; 44 /** 45 * Pattern used to build the message (general problem description). 46 */ 47 private final Localizable general; 48 /** 49 * Arguments used to build the message. 50 */ 51 private final Object[] arguments; 52 53 /** 54 * @param specific Message pattern providing the specific context of 55 * the error. 56 * @param general Message pattern explaining the cause of the error. 57 * @param args Arguments. 58 */ 59 protected MathIllegalArgumentException(Localizable specific, 60 Localizable general, 61 Object ... args) { 62 this.specific = specific; 63 this.general = general; 64 arguments = ArgUtils.flatten(args); 65 } 66 /** 67 * @param general Message pattern explaining the cause of the error. 68 * @param args Arguments. 69 */ 70 protected MathIllegalArgumentException(Localizable general, 71 Object ... args) { 72 this(null, general, args); 73 } 74 75 /** {@inheritDoc} */ 76 public Localizable getSpecificPattern() { 77 return specific; 78 } 79 80 /** {@inheritDoc} */ 81 public Localizable getGeneralPattern() { 82 return general; 83 } 84 85 /** {@inheritDoc} */ 86 public Object[] getArguments() { 87 return arguments.clone(); 88 } 89 90 /** 91 * Get the message in a specified locale. 92 * 93 * @param locale Locale in which the message should be translated. 94 * 95 * @return the localized message. 96 */ 97 public String getMessage(final Locale locale) { 98 return MessageFactory.buildMessage(locale, specific, general, arguments); 99 } 100 101 /** {@inheritDoc} */ 102 @Override 103 public String getMessage() { 104 return getMessage(Locale.US); 105 } 106 107 /** {@inheritDoc} */ 108 @Override 109 public String getLocalizedMessage() { 110 return getMessage(Locale.getDefault()); 111 } 112} 113