001/* 
002 * JKNIV, utils - Helper utilities for jdk code.
003 * 
004 * Copyright (C) 2017, the original author or authors.
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 2.1 of the License.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with this library; if not, write to the Free Software Foundation, Inc., 
018 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
019 */
020package net.sf.jkniv.asserts;
021
022public class NotNull extends AbstractAssert
023{
024    NotNull()
025    {
026        super("[Assertion failed] - this argument is required; it must not be null");
027    }
028    
029    /**
030     * @param message the exception message to use if the assertion fails
031     */
032    NotNull(String message)
033    {
034        super(message);
035    }
036    
037    /**
038     * Assert that an object is not {@code null} .
039     * <pre class="code">Assert.notNull(clazz, "The class must not be null");</pre>
040     * @param objects the object to check
041     * @throws IllegalArgumentException if the object is {@code null}
042     */
043    public void verify(Object... objects)
044    {
045        if (verifyObjects(objects))
046            throwDefaultException();
047    }
048
049    public void verify(RuntimeException e, Object... objects)
050    {
051        if (verifyObjects(objects))
052            throw e;
053    }
054
055    public void verifyArray(Object[] object)
056    {
057        if (verifyObject(object))
058            throwDefaultException();
059        else if (!object.getClass().isArray())
060          throw new IllegalArgumentException("[Assertion failed] - this argument is not array");
061    }
062
063    public void verifyArray(RuntimeException e, Object[] object)
064    {
065        if (verifyObject(object))
066            throw e;
067        else if (!object.getClass().isArray())
068          throw new IllegalArgumentException("[Assertion failed] - this argument is not array");
069    }
070
071    private boolean verifyObjects(Object... objects)
072    {
073        boolean ret = false;
074        if (objects == null)
075            ret = true;
076        else
077        {
078            for (Object o : objects)
079            {
080                if(verifyObject(o))
081                {
082                    ret = true;
083                    break;
084                }
085            }
086        }
087        return ret;
088    }
089
090    private boolean verifyObject(Object object)
091    {
092        boolean ret = false;
093        if (object == null)
094            ret = true;
095        return ret;
096    }
097}