Java Assert Null assertInstance(final Object object, final Class c, final boolean allowNull)

Here you can find the source of assertInstance(final Object object, final Class c, final boolean allowNull)

Description

Asserts the the given object is an instance of the given class -- optionally allowing the object to be null.

License

Open Source License

Parameter

Parameter Description
object The object for which the type should be checked.
c The class that the object must be; fails if the class is <code>null</code>.
allowNull Whether the object being <code>null</code> will not cause a failure.

Declaration

public static final void assertInstance(final Object object,
        final Class c, final boolean allowNull) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*ww w  . j av  a  2  s.  co  m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Asserts the the given object is an instance of the given class --
     * optionally allowing the object to be <code>null</code>.
     * 
     * @param object
     *            The object for which the type should be checked.
     * @param c
     *            The class that the object must be; fails if the class is
     *            <code>null</code>.
     * @param allowNull
     *            Whether the object being <code>null</code> will not cause a
     *            failure.
     */
    public static final void assertInstance(final Object object,
            final Class c, final boolean allowNull) {
        if (object == null && allowNull) {
            return;
        }

        if (object == null || c == null) {
            throw new NullPointerException();
        } else if (!c.isInstance(object)) {
            throw new IllegalArgumentException();
        }
    }
}

Related

  1. assertAllAreNull(String messageIfNull, Object... objects)
  2. assertIsNull(Object obj)
  3. assertNonNull(final T object, final String paramName)
  4. assertNonNull(String argumentName, Object argument)
  5. assertNoNull(Object object, String paramName)