176f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti/*
276f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
376f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti * Copyright (C) 2011, 2013-2016 The JavaParser Team.
476f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti *
576f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti * This file is part of JavaParser.
676f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti *
776f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti * JavaParser can be used either under the terms of
876f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti * a) the GNU Lesser General Public License as published by
976f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti *     the Free Software Foundation, either version 3 of the License, or
1076f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti *     (at your option) any later version.
1176f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti * b) the terms of the Apache License
1276f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti *
1376f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti * You should have received a copy of both licenses in LICENCE.LGPL and
1476f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti * LICENCE.APACHE. Please refer to those files for details.
1576f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti *
1676f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti * JavaParser is distributed in the hope that it will be useful,
1776f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti * but WITHOUT ANY WARRANTY; without even the implied warranty of
1876f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1976f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti * GNU Lesser General Public License for more details.
2076f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti */
2176f5dc9d043237e8ada0e6597d6c8d48b67f6d8fFederico Tomassetti
229985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoidpackage com.github.javaparser.utils;
239985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid
249985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoidimport java.util.HashMap;
259985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoidimport java.util.Map;
269985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid
279985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoidpublic class ClassUtils {
289985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    /**
299985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * Maps primitive {@code Class}es to their corresponding wrapper {@code Class}.
309985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     */
31c3f372b0c7c8ca4bed1261e7f1d3f17ed01b4889Danny van Bruggen    private static final Map<Class<?>, Class<?>> primitiveWrapperMap = new HashMap<>();
324296abb695b767f7471dd255d0679516183b062aDanny van Bruggen
339985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    static {
349985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Boolean.TYPE, Boolean.class);
359985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Byte.TYPE, Byte.class);
369985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Character.TYPE, Character.class);
379985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Short.TYPE, Short.class);
389985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Integer.TYPE, Integer.class);
399985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Long.TYPE, Long.class);
409985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Double.TYPE, Double.class);
419985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Float.TYPE, Float.class);
429985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Void.TYPE, Void.TYPE);
439985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    }
449985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid
459985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    /**
469985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * Maps wrapper {@code Class}es to their corresponding primitive types.
479985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     */
48c3f372b0c7c8ca4bed1261e7f1d3f17ed01b4889Danny van Bruggen    private static final Map<Class<?>, Class<?>> wrapperPrimitiveMap = new HashMap<>();
494296abb695b767f7471dd255d0679516183b062aDanny van Bruggen
509985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    static {
519985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        for (final Class<?> primitiveClass : primitiveWrapperMap.keySet()) {
529985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid            final Class<?> wrapperClass = primitiveWrapperMap.get(primitiveClass);
539985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid            if (!primitiveClass.equals(wrapperClass)) {
549985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid                wrapperPrimitiveMap.put(wrapperClass, primitiveClass);
559985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid            }
569985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        }
579985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    }
589985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid
599985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    /**
609985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * Returns whether the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte},
619985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * {@link Character},
629985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
639985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     *
644296abb695b767f7471dd255d0679516183b062aDanny van Bruggen     * @param type The class to query or null.
654296abb695b767f7471dd255d0679516183b062aDanny van Bruggen     * @return true if the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, {@link
664296abb695b767f7471dd255d0679516183b062aDanny van Bruggen     * Character}, {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
679985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     */
689985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    public static boolean isPrimitiveOrWrapper(final Class<?> type) {
699985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        if (type == null) {
709985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid            return false;
719985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        }
729985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        return type.isPrimitive() || isPrimitiveWrapper(type);
739985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    }
749985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid
759985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    /**
769985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * Returns whether the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},
779985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * {@link Short},
789985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
799985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     *
804296abb695b767f7471dd255d0679516183b062aDanny van Bruggen     * @param type The class to query or null.
819985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * @return true if the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},
824296abb695b767f7471dd255d0679516183b062aDanny van Bruggen     * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
839985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * @since 3.1
849985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     */
859985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    public static boolean isPrimitiveWrapper(final Class<?> type) {
869985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        return wrapperPrimitiveMap.containsKey(type);
879985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    }
889985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid}
89