View Javadoc
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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.exception;
23  
24  import java.text.MessageFormat;
25  import java.util.Locale;
26  
27  /**
28   * All conditions checks that fail due to a {@code null} argument must throw
29   * this exception.
30   * This class is meant to signal a precondition violation ("null is an illegal
31   * argument") and so does not extend the standard {@code NullPointerException}.
32   * Propagation of {@code NullPointerException} from within Hipparchus is
33   * construed to be a bug.
34   * <p>
35   * Note: from 1.0 onwards, this class extends {@link NullPointerException} instead
36   * of {@link MathIllegalArgumentException}.
37   *
38   */
39  public class NullArgumentException extends NullPointerException
40      implements LocalizedException {
41  
42      /** Serializable version Id. */
43      private static final long serialVersionUID = 20160217L;
44  
45      /** Format specifier (to be translated). */
46      private final Localizable specifier;
47  
48      /** Parts to insert in the format (no translation). */
49      private final Object[] parts;
50  
51      /**
52       * Default constructor.
53       */
54      public NullArgumentException() {
55          this(LocalizedCoreFormats.NULL_NOT_ALLOWED);
56      }
57  
58      /** Simple constructor.
59       * @param specifier format specifier (to be translated).
60       * @param parts parts to insert in the format (no translation).
61       */
62      public NullArgumentException(final Localizable specifier, final Object ... parts) {
63          this.specifier = specifier;
64          this.parts     = (parts == null) ? new Object[0] : parts.clone();
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public String getMessage(final Locale locale) {
70          return buildMessage(locale, specifier, parts);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public String getMessage() {
76          return getMessage(Locale.US);
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public String getLocalizedMessage() {
82          return getMessage(Locale.getDefault());
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public Localizable getSpecifier() {
88          return specifier;
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public Object[] getParts() {
94          return parts.clone();
95      }
96  
97      /**
98       * Builds a message string by from a pattern and its arguments.
99       * @param locale Locale in which the message should be translated
100      * @param specifier format specifier (to be translated)
101      * @param parts parts to insert in the format (no translation)
102      * @return a message string
103      */
104     private static String buildMessage(final Locale locale, final Localizable specifier, final Object ... parts) {
105         return (specifier == null) ? "" : new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
106     }
107 
108 }