001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.math4.legacy.exception; 018 019import org.apache.commons.math4.legacy.exception.util.ExceptionContext; 020import org.apache.commons.math4.legacy.exception.util.ExceptionContextProvider; 021import org.apache.commons.math4.legacy.exception.util.Localizable; 022import org.apache.commons.math4.legacy.exception.util.LocalizedFormats; 023 024/** 025 * All conditions checks that fail due to a {@code null} argument must throw 026 * this exception. 027 * This class is meant to signal a precondition violation ("null is an illegal 028 * argument") and so does not extend the standard {@code NullPointerException}. 029 * Propagation of {@code NullPointerException} from within Commons-Math is 030 * construed to be a bug. 031 * <p> 032 * Note: from 4.0 onwards, this class extends {@link NullPointerException} instead 033 * of {@link MathIllegalArgumentException}. 034 * 035 * @since 2.2 036 */ 037public class NullArgumentException extends NullPointerException 038 implements ExceptionContextProvider { 039 040 /** Serializable version Id. */ 041 private static final long serialVersionUID = 20150225L; 042 043 /** Context. */ 044 private final ExceptionContext context; 045 046 /** 047 * Default constructor. 048 */ 049 public NullArgumentException() { 050 this(LocalizedFormats.NULL_NOT_ALLOWED); 051 } 052 /** 053 * @param pattern Message pattern providing the specific context of 054 * the error. 055 * @param arguments Values for replacing the placeholders in {@code pattern}. 056 */ 057 public NullArgumentException(Localizable pattern, 058 Object... arguments) { 059 context = new ExceptionContext(this); 060 context.addMessage(pattern, arguments); 061 } 062 063 /** 064 * {@inheritDoc} 065 * @since 4.0 066 */ 067 @Override 068 public ExceptionContext getContext() { 069 return context; 070 } 071 072 /** {@inheritDoc} */ 073 @Override 074 public String getMessage() { 075 return context.getMessage(); 076 } 077 078 /** {@inheritDoc} */ 079 @Override 080 public String getLocalizedMessage() { 081 return context.getLocalizedMessage(); 082 } 083 084 /** 085 * Checks that an object is not null. 086 * 087 * @param o Object to be checked. 088 * @param pattern Message pattern. 089 * @param args Arguments to replace the placeholders in {@code pattern}. 090 * @throws NullArgumentException if {@code o} is {@code null}. 091 */ 092 public static void check(Object o, 093 Localizable pattern, 094 Object... args) { 095 if (o == null) { 096 throw new NullArgumentException(pattern, args); 097 } 098 } 099 100 /** 101 * Checks that an object is not null. 102 * 103 * @param o Object to be checked. 104 * @throws NullArgumentException if {@code o} is {@code null}. 105 */ 106 public static void check(Object o) { 107 if (o == null) { 108 throw new NullArgumentException(); 109 } 110 } 111}