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 IsTrue extends AbstractAssert
023{
024    IsTrue()
025    {
026        super("[Assertion failed] - this expression must be true");
027    }
028    
029    /**
030     * @param message the exception message to use if the assertion fails
031     */
032    IsTrue(String message)
033    {
034        super(message);
035    }
036    
037    /**
038     * Assert a boolean expression, throwing {@code IllegalArgumentException}
039     * if the test result is {@code false}.
040     * <pre class="code">Assert.isTrue(i &gt; 0);</pre>
041     * @param objects a boolean expression
042     * @throws IllegalArgumentException if expression is {@code false}
043     */
044    public void verify(Object... objects)
045    {
046        if (verifyObjects(objects))
047            throwDefaultException();
048    }
049
050    public void verify(RuntimeException e, Object... objects)
051    {
052        if (verifyObjects(objects))
053            throw e;
054    }
055
056    public void verifyArray(Object[] object)
057    {
058        if (verifyObject(object))
059            throwDefaultException();
060    }
061
062    public void verifyArray(RuntimeException e, Object[] object)
063    {
064        if (verifyObject(object))
065            throw e;
066    }
067
068    private boolean verifyObjects(Object... objects)
069    {
070        boolean ret = false;
071        if (objects == null || objects.length == 0)
072            ret =  true;
073        else
074        {
075            for (Object o : objects)
076            {
077                if (verifyObject(o))
078                {
079                    ret = true;
080                    break;
081                }
082            }
083        }
084        return ret;
085    }
086    
087    private boolean verifyObject(Object object)
088    {
089        boolean ret = false;
090        if (object == null)
091            ret = true;
092        else
093        {
094            if (object == null || !object.getClass().isAssignableFrom(Boolean.class))
095                ret = true;
096            else if (!((Boolean) object).booleanValue())
097                ret = true;
098        }
099        return ret;
100    }
101}