OrekitIllegalArgumentException.java

  1. /* Copyright 2002-2020 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.errors;

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

  20. import org.hipparchus.exception.Localizable;

  21. /** Extension of {@link java.lang.IllegalArgumentException} with localized message.
  22.  * @since 7.1
  23.  */
  24. public class OrekitIllegalArgumentException extends IllegalArgumentException implements LocalizedException {

  25.     /** Serializable UID. */
  26.     private static final long serialVersionUID = 20150611L;

  27.     /** Format specifier (to be translated). */
  28.     private final Localizable specifier;

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

  31.     /** Create an exception with localized message.
  32.      * @param specifier format specifier (to be translated)
  33.      * @param parts parts to insert in the format (no translation)
  34.      */
  35.     public OrekitIllegalArgumentException(final Localizable specifier, final Object... parts) {
  36.         this.specifier = specifier;
  37.         this.parts     = (parts == null) ? new Object[0] : parts.clone();
  38.     }

  39.     /** {@inheritDoc} */
  40.     @Override
  41.     public String getMessage(final Locale locale) {
  42.         return buildMessage(locale);
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     public String getMessage() {
  47.         return buildMessage(Locale.US);
  48.     }

  49.     /** {@inheritDoc} */
  50.     @Override
  51.     public String getLocalizedMessage() {
  52.         return buildMessage(Locale.getDefault());
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public Localizable getSpecifier() {
  57.         return specifier;
  58.     }

  59.     /** {@inheritDoc} */
  60.     @Override
  61.     public Object[] getParts() {
  62.         return parts.clone();
  63.     }

  64.     /**
  65.      * Builds a message string by from a pattern and its arguments.
  66.      * @param locale Locale in which the message should be translated
  67.      * @return a message string
  68.      */
  69.     private String buildMessage(final Locale locale) {
  70.         return (specifier == null) ?
  71.                 "" :
  72.                 new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
  73.     }

  74. }