NullArgumentException.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.exception;

  22. import java.text.MessageFormat;
  23. import java.util.Locale;

  24. /**
  25.  * All conditions checks that fail due to a {@code null} argument must throw
  26.  * this exception.
  27.  * This class is meant to signal a precondition violation ("null is an illegal
  28.  * argument") and so does not extend the standard {@code NullPointerException}.
  29.  * Propagation of {@code NullPointerException} from within Hipparchus is
  30.  * construed to be a bug.
  31.  * <p>
  32.  * Note: from 1.0 onwards, this class extends {@link NullPointerException} instead
  33.  * of {@link MathIllegalArgumentException}.
  34.  *
  35.  */
  36. public class NullArgumentException extends NullPointerException
  37.     implements LocalizedException {

  38.     /** Serializable version Id. */
  39.     private static final long serialVersionUID = 20160217L;

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

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

  44.     /**
  45.      * Default constructor.
  46.      */
  47.     public NullArgumentException() {
  48.         this(LocalizedCoreFormats.NULL_NOT_ALLOWED);
  49.     }

  50.     /** Simple constructor.
  51.      * @param specifier format specifier (to be translated).
  52.      * @param parts parts to insert in the format (no translation).
  53.      */
  54.     public NullArgumentException(final Localizable specifier, final Object ... parts) {
  55.         this.specifier = specifier;
  56.         this.parts     = (parts == null) ? new Object[0] : parts.clone();
  57.     }

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

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

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

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public Localizable getSpecifier() {
  76.         return specifier;
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public Object[] getParts() {
  81.         return parts.clone();
  82.     }

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

  93. }