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 * https://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
18 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22 package org.hipparchus.exception;
23
24 import java.text.MessageFormat;
25 import java.util.Locale;
26
27 /**
28 * All exceptions thrown by the Hipparchus code inherit from this class.
29 */
30 public class MathRuntimeException extends RuntimeException implements LocalizedException {
31
32 /** Serializable version Id. */
33 private static final long serialVersionUID = 20160217L;
34
35 /** URL for reporting problems for internal errors. */
36 private static final String REPORT_URL = "https://github.com/Hipparchus-Math/hipparchus/issues";
37
38 /** Format specifier (to be translated). */
39 private final Localizable specifier;
40
41 /** Parts to insert in the format (no translation). */
42 private final Object[] parts;
43
44 /** Simple constructor.
45 * @param specifier format specifier (to be translated).
46 * @param parts parts to insert in the format (no translation).
47 */
48 public MathRuntimeException(final Localizable specifier, final Object ... parts) {
49 this.specifier = specifier;
50 this.parts = (parts == null) ? new Object[0] : parts.clone();
51 }
52
53 /** Simple constructor.
54 * @param cause root cause.
55 * @param specifier format specifier (to be translated).
56 * @param parts parts to insert in the format (no translation).
57 */
58 public MathRuntimeException(final Throwable cause, final Localizable specifier,
59 final Object ... parts) {
60 super(cause);
61 this.specifier = specifier;
62 this.parts = (parts == null) ? new Object[0] : parts.clone();
63 }
64
65 /** Create an exception for an internal error.
66 * @return a new runtime exception indicating an internal error
67 */
68 public static MathRuntimeException createInternalError() {
69 return new MathRuntimeException(LocalizedCoreFormats.INTERNAL_ERROR, REPORT_URL);
70 }
71
72 /** Create an exception for an internal error.
73 * @param cause root cause
74 * @return a new runtime exception, indicating an internal error and wrapping the
75 * given throwable
76 */
77 public static MathRuntimeException createInternalError(final Throwable cause) {
78 return new MathRuntimeException(cause, LocalizedCoreFormats.INTERNAL_ERROR, REPORT_URL);
79 }
80
81 /** {@inheritDoc} */
82 @Override
83 public String getMessage(final Locale locale) {
84 return buildMessage(locale);
85 }
86
87 /** {@inheritDoc} */
88 @Override
89 public String getMessage() {
90 return getMessage(Locale.US);
91 }
92
93 /** {@inheritDoc} */
94 @Override
95 public String getLocalizedMessage() {
96 return getMessage(Locale.getDefault());
97 }
98
99 /** {@inheritDoc} */
100 @Override
101 public Localizable getSpecifier() {
102 return specifier;
103 }
104
105 /** {@inheritDoc} */
106 @Override
107 public Object[] getParts() {
108 return parts.clone();
109 }
110
111 /**
112 * Builds a message string by from a pattern and its arguments.
113 * @param locale Locale in which the message should be translated
114 * @return a message string
115 */
116 @SuppressWarnings("PMD.AvoidCatchingGenericException") // catching Exception is intentional here
117 private String buildMessage(final Locale locale) {
118 if (specifier == null) {
119 return "";
120 }
121 // CHECKSTYLE: stop IllegalCatch
122 try {
123 return new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
124 } catch (Exception e) {
125 this.addSuppressed(e);
126 return specifier.getSourceString();
127 }
128 // CHECKSTYLE: resume IllegalCatch
129 }
130
131 }