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 preconditions violation exceptions. 027 * This class is not intended to be instantiated directly: it should serve 028 * as a base class to create all the exceptions that share the semantics of 029 * the standard {@link IllegalArgumentException}, but must also provide a 030 * localized message. 031 * 032 * @since 2.2 033 * @version $Revision$ $Date$ 034 */ 035 public class MathIllegalArgumentException extends IllegalArgumentException implements MathThrowable { 036 037 /** Serializable version Id. */ 038 private static final long serialVersionUID = -6024911025449780478L; 039 040 /** 041 * Pattern used to build the message (specific context). 042 */ 043 private final Localizable specific; 044 /** 045 * Pattern used to build the message (general problem description). 046 */ 047 private final Localizable general; 048 /** 049 * Arguments used to build the message. 050 */ 051 private final Object[] arguments; 052 053 /** 054 * @param specific Message pattern providing the specific context of 055 * the error. 056 * @param general Message pattern explaining the cause of the error. 057 * @param args Arguments. 058 */ 059 protected MathIllegalArgumentException(Localizable specific, 060 Localizable general, 061 Object ... args) { 062 this.specific = specific; 063 this.general = general; 064 arguments = ArgUtils.flatten(args); 065 } 066 /** 067 * @param general Message pattern explaining the cause of the error. 068 * @param args Arguments. 069 */ 070 protected MathIllegalArgumentException(Localizable general, 071 Object ... args) { 072 this(null, general, args); 073 } 074 075 /** {@inheritDoc} */ 076 public Localizable getSpecificPattern() { 077 return specific; 078 } 079 080 /** {@inheritDoc} */ 081 public Localizable getGeneralPattern() { 082 return general; 083 } 084 085 /** {@inheritDoc} */ 086 public Object[] getArguments() { 087 return arguments.clone(); 088 } 089 090 /** 091 * Get the message in a specified locale. 092 * 093 * @param locale Locale in which the message should be translated. 094 * 095 * @return the localized message. 096 */ 097 public String getMessage(final Locale locale) { 098 return MessageFactory.buildMessage(locale, specific, general, arguments); 099 } 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 }