Java Collection Check isCollection(final Object value)

Here you can find the source of isCollection(final Object value)

Description

Returns true if the given object is a non-null instance of Collection .

License

Open Source License

Parameter

Parameter Description
value The object to be evaluated.

Return

true if the given object is a non-null instance of . false otherwise.

Declaration

public static boolean isCollection(final Object value) 

Method Source Code

//package com.java2s;
/*//from www  .  j ava 2  s  . co m
 * Copyright (C) 2013 Marcius da Silva da Fonseca.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 */

import java.util.Collection;

public class Main {
    /**
     * Returns {@code true} if the given object is a <b>non-null</b> instance of {@link Collection}.
     *
     * @param value The object to be evaluated.
     * @return {@code true} if the given object is a non-null instance of {@link Collection}. {@code false} otherwise.
     */
    public static boolean isCollection(final Object value) {
        return isCollection(value, false);
    }

    /**
     * Returns {@code true} if the given object is an instance of {@link Collection}.
     *
     * @param value The object to be evaluated.
     * @param acceptNull Indicates if this method may return {@code true} in case of {@code null} values.
     * @return {@code true} if the given object is an instance of {@link Collection}. {@code false} otherwise.
     */
    public static boolean isCollection(final Object value, final boolean acceptNull) {
        return isType(Collection.class, value, acceptNull);
    }

    /**
     * Returns {@code true} if the given object is a <b>non-null</b> instance of the given type.
     *
     * @param type  The type to be tested against the object.
     * @param value The object to be evaluated.
     * @return {@code true} if the given object is a non-null instance of the given type. {@code false} otherwise.
     * @throws IllegalArgumentException If the given type is invalid or null.
     */
    public static boolean isType(final Class<?> type, final Object value) {
        return isType(type, value, false);
    }

    /**
     * Returns {@code true} if the given object is an instance of the given type.
     *
     * @param type  The type to be tested against the object. Cannot be null.
     * @param value The object to be evaluated.
     * @param acceptNull Indicates if this method may return {@code true} in case of {@code null} values.
     * @return {@code true} if the given object is an instance of the given type. {@code false} otherwise.
     * @throws IllegalArgumentException If the given type is invalid or null.
     */
    public static boolean isType(final Class<?> type, final Object value, final boolean acceptNull) {
        if (type == null) {
            throw new IllegalArgumentException("The type cannot be null.");
        }
        return (value == null && acceptNull) || (value != null && type.isAssignableFrom(value.getClass()));
    }
}

Related

  1. isCollection(Class type)
  2. isCollection(Class clazz)
  3. isCollection(Class fieldClass)
  4. isCollection(final Object collection)
  5. isCollection(final Object obj)
  6. isCollection(Object ob)
  7. isCollection(Object obj)
  8. isCollection(Object obj)
  9. isCollection(Object obj)