1   /* Copyright 2002-2016 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  
19  import java.text.MessageFormat;
20  import java.util.Locale;
21  
22  import org.hipparchus.exception.Localizable;
23  
24  /** Extension of {@link java.lang.IllegalArgumentException} with localized message.
25   * @since 7.1
26   */
27  public class OrekitIllegalArgumentException extends IllegalArgumentException implements LocalizedException {
28  
29      /** Serializable UID. */
30      private static final long serialVersionUID = 20150611L;
31  
32      /** Format specifier (to be translated). */
33      private final Localizable specifier;
34  
35      /** Parts to insert in the format (no translation). */
36      private final Object[] parts;
37  
38      /** Create an exception with localized message.
39       * @param specifier format specifier (to be translated)
40       * @param parts parts to insert in the format (no translation)
41       */
42      public OrekitIllegalArgumentException(final Localizable specifier, final Object ... parts) {
43          this.specifier = specifier;
44          this.parts     = (parts == null) ? new Object[0] : parts.clone();
45      }
46  
47      /** {@inheritDoc} */
48      @Override
49      public String getMessage(final Locale locale) {
50          return buildMessage(locale);
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public String getMessage() {
56          return buildMessage(Locale.US);
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public String getLocalizedMessage() {
62          return buildMessage(Locale.getDefault());
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public Localizable getSpecifier() {
68          return specifier;
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public Object[] getParts() {
74          return parts.clone();
75      }
76  
77      /**
78       * Builds a message string by from a pattern and its arguments.
79       * @param locale Locale in which the message should be translated
80       * @return a message string
81       */
82      private String buildMessage(final Locale locale) {
83          return (specifier == null) ?
84                  "" :
85                  new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
86      }
87  
88  }