Java ConcurrentMap isClassClass(Class clazz)

Here you can find the source of isClassClass(Class clazz)

Description

is Class Class

License

Educational Community License

Parameter

Parameter Description
clazz class token

Exception

Parameter Description
IllegalArgumentException if the given Class is null

Return

true if the given Class is a Class type

Declaration

public static boolean isClassClass(Class<?> clazz) 

Method Source Code

//package com.java2s;
/**//  w w  w. ja  v  a2 s. c  o m
 * Copyright 2005-2015 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.common.collect.MapMaker;

import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentMap;

public class Main {
    private static final Collection<Class<?>> CLASS_CLASSES = Collections.<Class<?>>singleton(Class.class);
    private static final ConcurrentMap<Class<?>, Boolean> IS_CACHE_CACHE = new MapMaker().weakKeys().makeMap();

    /**
     * @param clazz class token
     * @return true if the given Class is a Class type
     * @throws IllegalArgumentException if the given Class is null
     */
    public static boolean isClassClass(Class<?> clazz) {
        return is(clazz, CLASS_CLASSES, IS_CACHE_CACHE);
    }

    /**
     * Checks is a class is assignable to a class in the give collection.  Also handles caching.
     *
     * @param clazz the class to check
     * @param clazzes the collection of classes to check against
     * @param cache the cache to check against
     * @return a if the class is assignable
     * @throws IllegalArgumentException if any arg is null
     */
    private static boolean is(Class<?> clazz, Collection<Class<?>> clazzes,
            ConcurrentMap<Class<?>, Boolean> cache) {
        if (clazz == null) {
            throw new IllegalArgumentException("clazz is null");
        }

        if (clazzes == null) {
            throw new IllegalArgumentException("clazzes is null");
        }

        if (cache == null) {
            throw new IllegalArgumentException("cache is null");
        }

        Boolean result = cache.get(clazz);
        if (result == null) {
            result = Boolean.valueOf(isa(clazzes, clazz));
            cache.putIfAbsent(clazz, result);
        }
        return result.booleanValue();
    }

    /**
     * @param types class tokens to check against
     * @param type class token
     * @return true if the given Class is assignable from one of the classes in
     *         the given Class[]
     */
    private static boolean isa(Collection<Class<?>> types, Class<?> type) {
        for (Class<?> cur : types) {
            if (cur.isAssignableFrom(type)) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. getNestedSet(K1 key, ConcurrentMap> set)
  2. getNewLogFactor(String factorName)
  3. getSingleInstance(Class classType)
  4. getTypeFromKind(String kind)
  5. is(Class clazz, Collection> clazzes, ConcurrentMap, Boolean> cache)
  6. putIfAbsent(ConcurrentMap map, K key, V value)
  7. putIfAbsent(ConcurrentMap map, K key, V value)
  8. putIfAbsent(ConcurrentMap map, K key, V value)
  9. putIfAbsent(ConcurrentMap target, K key, V value)