1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.exception;
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.util.Locale;
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.ArgUtils;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.MessageFactory;
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.Localizable;
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Base class for all exceptions that signal a mismatch between the
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * current state and the user's expectations.
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.2
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1061496 $ $Date: 2011-01-20 21:32:16 +0100 (jeu. 20 janv. 2011) $
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class MathIllegalStateException extends IllegalStateException implements MathThrowable {
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Serializable version Id. */
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final long serialVersionUID = -6024911025449780478L;
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Pattern used to build the message (specific context).
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final Localizable specific;
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Pattern used to build the message (general problem description).
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final Localizable general;
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Arguments used to build the message.
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final Object[] arguments;
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Simple constructor.
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param specific Message pattern providing the specific context of
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the error.
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param general Message pattern explaining the cause of the error.
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param args Arguments.
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public MathIllegalStateException(Localizable specific,
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                     Localizable general,
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                     Object ... args) {
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this(null, specific, general, args);
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Simple constructor.
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param cause root cause
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param specific Message pattern providing the specific context of
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the error.
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param general Message pattern explaining the cause of the error.
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param args Arguments.
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public MathIllegalStateException(Throwable cause,
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                     Localizable specific,
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                     Localizable general,
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                     Object ... args) {
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        super(cause);
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.specific = specific;
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.general = general;
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        arguments = ArgUtils.flatten(args);
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param general Message pattern explaining the cause of the error.
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param args Arguments.
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public MathIllegalStateException(Localizable general,
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                     Object ... args) {
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this(null, null, general, args);
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Simple constructor.
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param cause root cause
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param general Message pattern explaining the cause of the error.
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param args Arguments.
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public MathIllegalStateException(Throwable cause,
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                     Localizable general,
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                     Object ... args) {
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this(cause, null, general, args);
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Localizable getSpecificPattern() {
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return specific;
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Localizable getGeneralPattern() {
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return general;
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Object[] getArguments() {
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return arguments.clone();
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Get the message in a specified locale.
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param locale Locale in which the message should be translated.
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the localized message.
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public String getMessage(final Locale locale) {
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return MessageFactory.buildMessage(locale, specific, general, arguments);
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   /** {@inheritDoc} */
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public String getMessage() {
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return getMessage(Locale.US);
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public String getLocalizedMessage() {
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return getMessage(Locale.getDefault());
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
141