MathRuntimeException.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.exception;

  22. import java.text.MessageFormat;
  23. import java.util.Locale;

  24. /**
  25.  * All exceptions thrown by the Hipparchus code inherit from this class.
  26.  */
  27. public class MathRuntimeException extends RuntimeException implements LocalizedException {

  28.     /** Serializable version Id. */
  29.     private static final long serialVersionUID = 20160217L;

  30.     /** URL for reporting problems for internal errors. */
  31.     private static final String REPORT_URL = "https://github.com/Hipparchus-Math/hipparchus/issues";

  32.     /** Format specifier (to be translated). */
  33.     private final Localizable specifier;

  34.     /** Parts to insert in the format (no translation). */
  35.     private final Object[] parts;

  36.     /** Simple constructor.
  37.      * @param specifier format specifier (to be translated).
  38.      * @param parts parts to insert in the format (no translation).
  39.      */
  40.     public MathRuntimeException(final Localizable specifier, final Object ... parts) {
  41.         this.specifier = specifier;
  42.         this.parts     = (parts == null) ? new Object[0] : parts.clone();
  43.     }

  44.     /** Simple constructor.
  45.      * @param cause root cause.
  46.      * @param specifier format specifier (to be translated).
  47.      * @param parts parts to insert in the format (no translation).
  48.      */
  49.     public MathRuntimeException(final Throwable cause, final Localizable specifier,
  50.                                 final Object ... parts) {
  51.         super(cause);
  52.         this.specifier = specifier;
  53.         this.parts     = (parts == null) ? new Object[0] : parts.clone();
  54.     }

  55.     /** Create an exception for an internal error.
  56.      * @return a new runtime exception indicating an internal error
  57.      */
  58.     public static MathRuntimeException createInternalError() {
  59.         return new MathRuntimeException(LocalizedCoreFormats.INTERNAL_ERROR, REPORT_URL);
  60.     }

  61.     /** Create an exception for an internal error.
  62.      * @param cause root cause
  63.      * @return a new runtime exception, indicating an internal error and wrapping the
  64.      * given throwable
  65.      */
  66.     public static MathRuntimeException createInternalError(final Throwable cause) {
  67.         return new MathRuntimeException(cause, LocalizedCoreFormats.INTERNAL_ERROR, REPORT_URL);
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public String getMessage(final Locale locale) {
  72.         return buildMessage(locale);
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public String getMessage() {
  77.         return getMessage(Locale.US);
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public String getLocalizedMessage() {
  82.         return getMessage(Locale.getDefault());
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public Localizable getSpecifier() {
  87.         return specifier;
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public Object[] getParts() {
  92.         return parts.clone();
  93.     }

  94.     /**
  95.      * Builds a message string by from a pattern and its arguments.
  96.      * @param locale Locale in which the message should be translated
  97.      * @return a message string
  98.      */
  99.     @SuppressWarnings("PMD.AvoidCatchingGenericException") // catching Exception is intentional here
  100.     private String buildMessage(final Locale locale) {
  101.         if (specifier == null) {
  102.             return "";
  103.         }
  104.         // CHECKSTYLE: stop IllegalCatch
  105.         try {
  106.             return new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
  107.         } catch (Exception e) {
  108.             this.addSuppressed(e);
  109.             return specifier.getSourceString();
  110.         }
  111.         // CHECKSTYLE: resume IllegalCatch
  112.     }

  113. }