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
022@SuppressWarnings("rawtypes")
023public class InstanceOf extends AbstractAssert
024{
025    private static final Assertable notNull = new NotNull();
026    
027    InstanceOf()
028    {
029        super("[Assertion failed] - this argument must be instance of %s, found %s");
030    }
031    
032    /**
033     * @param message the exception message to use if the assertion fails
034     */
035    InstanceOf(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        notNull.verify(objects);
049        if (verifyObjects(objects))
050            throwDefaultException();
051    }
052
053    public void verify(RuntimeException e, Object... objects)
054    {
055        notNull.verify(objects);
056        if (verifyObjects(objects))
057            throw e;
058    }
059
060    public void verifyArray(Object[] object)
061    {
062        throw new IllegalArgumentException("[Programming failed] - instance of arrays isn't implemented");
063//        if (verifyObject(object))
064//            throwDefaultException();
065//        else if (!object.getClass().isArray())
066//        throw new IllegalArgumentException("[Assertion failed] - this argument is not array");
067    }
068
069    public void verifyArray(RuntimeException e, Object[] object)
070    {
071        throw new IllegalArgumentException("[Programming failed] - instance of arrays isn't implemented");
072//        if (verifyObject(object))
073//            throw e;
074//        else if (!object.getClass().isArray())
075//          throw new IllegalArgumentException("[Assertion failed] - this argument is not array");
076    }
077
078    private boolean verifyObjects(Object... objects)
079    {
080        boolean ret = false;
081        if (objects == null)
082            ret = true;
083        else if (objects.length%2 != 0)
084            throw new IllegalArgumentException("[Programming failed] - instance of must have pair (Object, Class...) to check right instance");
085        else
086        {
087            for (int i=0; i<objects.length; i++)
088            {
089                Object o = objects[i];
090                Object clazz = objects[++i];
091                if ( !(clazz instanceof Class) )
092                    new IllegalArgumentException(String.format("[Programming failed] - argument [%i] must be instance of Class to check right instanceof", i));
093                
094                if(verifyObject(o, (Class)clazz))
095                    throwDefaultException(((Class)clazz).getName(), o.getClass().getName());
096            }
097        }
098        return ret;
099    }
100
101    private boolean verifyObject(Object object, Class clazz)
102    {
103        boolean answer = false;
104        if (!clazz.isInstance(object))
105            answer = true;
106        return answer;
107    }
108}