001package ball.lang;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: PrimitiveTypeMap.java 5855 2020-04-27 20:01:17Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/lang/PrimitiveTypeMap.java $
007 * %%
008 * Copyright (C) 2008 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.Map;
026
027/**
028 * Provides mapping of Java primitive {@link Class}es to their "wrapper"
029 * {@link Class}es.
030 *
031 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
032 * @version $Revision: 5855 $
033 */
034public class PrimitiveTypeMap extends HashMap<Class<?>,Class<?>> {
035    private static final long serialVersionUID = 542657344546950531L;
036
037    /**
038     * Unmodifiable instance of a {@link PrimitiveTypeMap}.
039     */
040    public static final Map<Class<?>,Class<?>> INSTANCE =
041        Collections.unmodifiableMap(new PrimitiveTypeMap());
042
043    /**
044     * Sole constructor.
045     */
046    public PrimitiveTypeMap() {
047        super();
048
049        put(Boolean.TYPE, Boolean.class);
050        put(Byte.TYPE, Byte.class);
051        put(Character.TYPE, Character.class);
052        put(Double.TYPE, Double.class);
053        put(Float.TYPE, Float.class);
054        put(Integer.TYPE, Integer.class);
055        put(Long.TYPE, Long.class);
056        put(Short.TYPE, Short.class);
057        put(Void.TYPE, Void.class);
058    }
059
060    /**
061     * Static method to get the "boxed" {@link Class} for a Java primitive
062     * {@link Class}.
063     *
064     * @param   type            The {@link Class}.
065     *
066     * @return  The "boxed" {@link Class} if the argument {@link Class} is a
067     *          primitive type; the argument otherwise.
068     */
069    public static Class<?> asBoxedType(Class<?> type) {
070        return (type != null && type.isPrimitive()) ? INSTANCE.get(type) : type;
071    }
072}