OrekitException.java

  1. /* Copyright 2002-2017 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS 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.  */
  17. package org.orekit.errors;

  18. import java.text.MessageFormat;
  19. import java.util.Locale;

  20. import org.hipparchus.exception.Localizable;
  21. import org.hipparchus.exception.MathRuntimeException;

  22. /** This class is the base class for all specific exceptions thrown by
  23.  * the Orekit classes.

  24.  * <p>When the Orekit classes throw exceptions that are specific to
  25.  * the package, these exceptions are always subclasses of
  26.  * OrekitException. When exceptions that are already covered by the
  27.  * standard java API should be thrown, like
  28.  * ArrayIndexOutOfBoundsException or InvalidParameterException, these
  29.  * standard exceptions are thrown rather than the Hipparchus specific
  30.  * ones.</p>
  31.  * <p>This class also provides utility methods to throw some standard
  32.  * java exceptions with localized messages.</p>
  33.  *
  34.  * @author Luc Maisonobe

  35.  */

  36. public class OrekitException extends Exception implements LocalizedException {

  37.     /** Serializable UID. */
  38.     private static final long serialVersionUID = 20150611L;

  39.     /** Format specifier (to be translated). */
  40.     private final Localizable specifier;

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

  43.     /** Simple constructor.
  44.      * Build an exception with a translated and formatted message
  45.      * @param specifier format specifier (to be translated)
  46.      * @param parts parts to insert in the format (no translation)
  47.      */
  48.     public OrekitException(final Localizable specifier, final Object... parts) {
  49.         this.specifier = specifier;
  50.         this.parts     = (parts == null) ? new Object[0] : parts.clone();
  51.     }

  52.     /** Copy constructor.
  53.      * @param exception exception to copy from
  54.      * @since 5.1
  55.      */
  56.     public OrekitException(final OrekitException exception) {
  57.         super(exception);
  58.         this.specifier = exception.specifier;
  59.         this.parts     = exception.parts.clone();
  60.     }

  61.     /** Simple constructor.
  62.      * Build an exception from a cause and with a specified message
  63.      * @param message descriptive message
  64.      * @param cause underlying cause
  65.      */
  66.     public OrekitException(final Localizable message, final Throwable cause) {
  67.         super(cause);
  68.         this.specifier = message;
  69.         this.parts     = new Object[0];
  70.     }

  71.     /** Simple constructor.
  72.      * Build an exception from a cause and with a translated and formatted message
  73.      * @param cause underlying cause
  74.      * @param specifier format specifier (to be translated)
  75.      * @param parts parts to insert in the format (no translation)
  76.      */
  77.     public OrekitException(final Throwable cause, final Localizable specifier,
  78.                            final Object... parts) {
  79.         super(cause);
  80.         this.specifier = specifier;
  81.         this.parts     = (parts == null) ? new Object[0] : parts.clone();
  82.     }

  83.     /** Simple constructor.
  84.      * Build an exception from an Hipparchus exception
  85.      * @param exception underlying Hipparchus exception
  86.      * @since 6.0
  87.      */
  88.     public OrekitException(final MathRuntimeException exception) {
  89.         super(exception);
  90.         this.specifier = exception.getSpecifier();
  91.         this.parts     = exception.getParts();
  92.     }

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public String getMessage(final Locale locale) {
  96.         return buildMessage(locale, specifier, parts);
  97.     }

  98.     /** {@inheritDoc} */
  99.     @Override
  100.     public String getMessage() {
  101.         return getMessage(Locale.US);
  102.     }

  103.     /** {@inheritDoc} */
  104.     @Override
  105.     public String getLocalizedMessage() {
  106.         return getMessage(Locale.getDefault());
  107.     }

  108.     /** {@inheritDoc} */
  109.     @Override
  110.     public Localizable getSpecifier() {
  111.         return specifier;
  112.     }

  113.     /** {@inheritDoc} */
  114.     @Override
  115.     public Object[] getParts() {
  116.         return parts.clone();
  117.     }

  118.     /** Recover a OrekitException, possibly embedded in a {@link MathRuntimeException}.
  119.      * <p>
  120.      * If the {@code MathRuntimeException} does not embed a OrekitException, a
  121.      * new one will be created.
  122.      * </p>
  123.      * @param exception MathRuntimeException to analyze
  124.      * @return a (possibly embedded) OrekitException
  125.      */
  126.     public static OrekitException unwrap(final MathRuntimeException exception) {

  127.         for (Throwable t = exception; t != null; t = t.getCause()) {
  128.             if (t instanceof OrekitException) {
  129.                 return (OrekitException) t;
  130.             }
  131.         }

  132.         return new OrekitException(exception);

  133.     }

  134.     /**
  135.      * Builds a message string by from a pattern and its arguments.
  136.      * @param locale Locale in which the message should be translated
  137.      * @param specifier format specifier (to be translated)
  138.      * @param parts parts to insert in the format (no translation)
  139.      * @return a message string
  140.      */
  141.     private static String buildMessage(final Locale locale, final Localizable specifier, final Object... parts) {
  142.         return (specifier == null) ? "" : new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
  143.     }

  144. }