001/**
002 * The MIT License (MIT)
003 *
004 * Copyright (c) 2017 tools4j.org (Marco Terzer)
005 *
006 * Permission is hereby granted, free of charge, to any person obtaining a copy
007 * of this software and associated documentation files (the "Software"), to deal
008 * in the Software without restriction, including without limitation the rights
009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010 * copies of the Software, and to permit persons to whom the Software is
011 * furnished to do so, subject to the following conditions:
012 *
013 * The above copyright notice and this permission notice shall be included in all
014 * copies or substantial portions of the Software.
015 *
016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022 * SOFTWARE.
023 */
024package org.tools4j.spockito;
025
026/**
027 * Contains static helper methods related to primitive types.
028 */
029final class Primitives {
030    /**
031     * Returns the boxing type given a primitive type.
032     *
033     * @param type a primitive type such as {@code int.class}
034     * @param <T> the type parameter
035     * @return the boxing type such as {@code Integer.class}
036     */
037    static <T> Class<T> boxingTypeFor(final Class<T> type) {
038        if (double.class.equals(type)) {
039            return (Class<T>)Double.class;
040        }
041        if (float.class.equals(type)) {
042            return (Class<T>)Float.class;
043        }
044        if (long.class.equals(type)) {
045            return (Class<T>)Long.class;
046        }
047        if (int.class.equals(type)) {
048            return (Class<T>)Integer.class;
049        }
050        if (short.class.equals(type)) {
051            return (Class<T>)Short.class;
052        }
053        if (byte.class.equals(type)) {
054            return (Class<T>)Byte.class;
055        }
056        if (boolean.class.equals(type)) {
057            return (Class<T>)Boolean.class;
058        }
059        if (char.class.equals(type)) {
060            return (Class<T>)Character.class;
061        }
062        if (void.class.equals(type)) {
063            return (Class<T>)Void.class;
064        }
065        throw new IllegalArgumentException("Not a primitive type: " + type.getName());
066    }
067
068    private Primitives() {
069        throw new RuntimeException("No Primitives for you!");
070    }
071}