RuggedException.java

  1. /* Copyright 2013-2025 CS GROUP
  2.  * Licensed to CS GROUP (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.rugged.errors;

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

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


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

  24.  * <p>
  25.  * This class is heavily based on {@code OrekitException},
  26.  * which is distributed under the terms of the Apache License V2.
  27.  * </p>
  28.  *
  29.  * @author Luc Maisonobe
  30.  * @author Guylaine Prat
  31.  */

  32. public class RuggedException extends RuntimeException implements LocalizedException {

  33.     /** Serializable UID. */
  34.     private static final long serialVersionUID = 20140309L;

  35.     /** Format specifier (to be translated). */
  36.     private final Localizable specifier;

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

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

  48.     /** Simple constructor.
  49.      * Build an exception from a cause and with a translated and formatted message
  50.      * @param cause underlying cause
  51.      * @param specifier format specifier (to be translated)
  52.      * @param parts parts to insert in the format (no translation)
  53.      */
  54.     public RuggedException(final Throwable cause, final Localizable specifier,
  55.                            final Object... parts) {
  56.         super(cause);
  57.         this.specifier = specifier;
  58.         this.parts     = (parts == null) ? new Object[0] : parts.clone();
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public String getMessage(final Locale locale) {
  63.         return buildMessage(locale, specifier, parts);
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public String getMessage() {
  68.         return getMessage(Locale.US);
  69.     }

  70.     /** {@inheritDoc} */
  71.     @Override
  72.     public String getLocalizedMessage() {
  73.         return getMessage(Locale.getDefault());
  74.     }

  75.     /** Get the localizable specifier of the error message.
  76.      * @return localizable specifier of the error message
  77.      */
  78.     public Localizable getSpecifier() {
  79.         return specifier;
  80.     }

  81.     /** Get the variable parts of the error message.
  82.      * @return a copy of the variable parts of the error message
  83.      */
  84.     public Object[] getParts() {
  85.         return parts.clone();
  86.     }

  87.     /**
  88.      * Builds a message string by from a pattern and its arguments.
  89.      * @param locale Locale in which the message should be translated
  90.      * @param specifier format specifier (to be translated)
  91.      * @param parts parts to insert in the format (no translation)
  92.      * @return a message string
  93.      */
  94.     private static String buildMessage(final Locale locale, final Localizable specifier,
  95.                                        final Object... parts) {
  96.         return (specifier == null) ? "" : new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
  97.     }

  98. }