001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.jxpath.util;
019
020import java.util.HashMap;
021
022/**
023 * Global type conversion utilities.
024 */
025public class TypeUtils {
026
027    private static TypeConverter typeConverter = new BasicTypeConverter();
028    private static final HashMap<Class, Class> PRIMITIVE_TYPE_MAP = new HashMap<>();
029    static {
030        PRIMITIVE_TYPE_MAP.put(int.class, Integer.class);
031        PRIMITIVE_TYPE_MAP.put(byte.class, Byte.class);
032        PRIMITIVE_TYPE_MAP.put(short.class, Short.class);
033        PRIMITIVE_TYPE_MAP.put(char.class, Character.class);
034        PRIMITIVE_TYPE_MAP.put(long.class, Long.class);
035        PRIMITIVE_TYPE_MAP.put(float.class, Float.class);
036        PRIMITIVE_TYPE_MAP.put(double.class, Double.class);
037        PRIMITIVE_TYPE_MAP.put(boolean.class, Boolean.class);
038    }
039
040    /**
041     * Returns true if the global converter can convert the supplied object to the specified type.
042     *
043     * @param object object to test
044     * @param toType target class
045     * @return boolean
046     */
047    public static boolean canConvert(final Object object, final Class toType) {
048        return typeConverter.canConvert(object, toType);
049    }
050
051    /**
052     * Converts the supplied object to the specified type. May throw a RuntimeException.
053     *
054     * @param object object to convert
055     * @param toType target class
056     * @return resulting Object
057     */
058    public static Object convert(final Object object, final Class toType) {
059        return typeConverter.convert(object, toType);
060    }
061
062    /**
063     * Returns the current type converter.
064     *
065     * @return TypeConverter
066     */
067    public static TypeConverter getTypeConverter() {
068        return typeConverter;
069    }
070
071    /**
072     * Install an alternative type converter.
073     *
074     * @param converter new TypeConverter
075     */
076    public static synchronized void setTypeConverter(final TypeConverter converter) {
077        typeConverter = converter;
078    }
079
080    /**
081     * Return the appropriate wrapper type for the specified class.
082     *
083     * @param p Class for which to retrieve a wrapper class.
084     * @return the wrapper if {@code p} is primitive, else {@code p}.
085     * @since JXPath 1.3
086     */
087    public static Class wrapPrimitive(final Class p) {
088        return p.isPrimitive() ? (Class) PRIMITIVE_TYPE_MAP.get(p) : p;
089    }
090
091    /**
092     * Constructs a new instance.
093     *
094     * @deprecated Will be private in the next major version.
095     */
096    @Deprecated
097    public TypeUtils() {
098        // empty
099    }
100}