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
022import java.util.Collection;
023import java.util.Map;
024
025public class NotEmpty extends AbstractAssert
026{
027    NotEmpty()
028    {
029        super("[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
030    }
031    
032    /**
033     * @param message the exception message to use if the assertion fails
034     */
035    NotEmpty(String message)
036    {
037        super(message);
038    }
039    
040    /**
041     * Assert that an object is not {@code null} .
042     * <pre class="code">Assert.notNull(clazz, "The class must not be null");</pre>
043     * @param objects the object to check
044     * @throws IllegalArgumentException if the object is {@code null}
045     */
046    public void verify(Object... objects)
047    {
048        if (verifyObjects(objects))
049            throwDefaultException();
050    }
051
052    public void verify(RuntimeException e, Object... objects)
053    {
054        if (verifyObjects(objects))
055            throw e;
056    }
057
058    public void verifyArray(Object[] object)
059    {
060        if (verifyObject(object))
061            throwDefaultException();
062    }
063    
064    public void verifyArray(RuntimeException e, Object[] object)
065    {
066        if (verifyObject(object))
067            throw e;
068    }
069    
070    private boolean verifyObjects(Object... objects)
071    {
072        boolean ret = false;
073        if (objects == null || objects.length == 0)
074            ret = true;
075        else
076        {
077            for (Object o : objects)
078            {
079                if (verifyObject(o))
080                {
081                    ret = true;
082                    break;
083                }
084            }
085        }
086        return ret;
087    }
088    
089    private boolean verifyObject(Object object)
090    {
091        boolean ret = false;
092        if (object == null)
093            ret = true;
094        else
095        {
096            if (object instanceof String && ((String) object).trim().length() == 0)
097                ret = true;
098            else if (object instanceof Collection && ((Collection<?>) object).isEmpty())
099                ret = true;
100            else if (object instanceof Map && ((Map<?,?>) object).isEmpty())
101                ret = true;
102            else if (object.getClass().isArray() && ((Object[]) object).length == 0)
103                ret = true;
104        }
105        return ret;
106    }
107}