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 package org.apache.commons.jxpath.util;
018
019 import java.util.HashMap;
020
021 /**
022 * Global type conversion utilities.
023 *
024 * @author Dmitri Plotnikov
025 * @version $Revision: 652845 $ $Date: 2008-05-02 12:46:46 -0500 (Fri, 02 May 2008) $
026 */
027 public class TypeUtils {
028 private static TypeConverter typeConverter = new BasicTypeConverter();
029 private static final HashMap PRIMITIVE_TYPE_MAP = new HashMap() {
030 {
031 put(int.class, Integer.class);
032 put(byte.class, Byte.class);
033 put(short.class, Short.class);
034 put(char.class, Character.class);
035 put(long.class, Long.class);
036 put(float.class, Float.class);
037 put(double.class, Double.class);
038 put(boolean.class, Boolean.class);
039 }
040 };
041
042 /**
043 * Install an alternative type converter.
044 * @param converter new TypeConverter
045 */
046 public static synchronized void setTypeConverter(TypeConverter converter) {
047 typeConverter = converter;
048 }
049
050 /**
051 * Returns the current type converter.
052 * @return TypeConverter
053 */
054 public static TypeConverter getTypeConverter() {
055 return typeConverter;
056 }
057
058 /**
059 * Returns true if the global converter can convert the supplied
060 * object to the specified type.
061 * @param object object to test
062 * @param toType target class
063 * @return boolean
064 */
065 public static boolean canConvert(Object object, Class toType) {
066 return typeConverter.canConvert(object, toType);
067 }
068
069 /**
070 * Converts the supplied object to the specified type. May
071 * throw a RuntimeException.
072 * @param object object to convert
073 * @param toType target class
074 * @return resulting Object
075 */
076 public static Object convert(Object object, Class toType) {
077 return typeConverter.convert(object, toType);
078 }
079
080 /**
081 * Return the appropriate wrapper type for the specified class.
082 * @param p Class for which to retrieve a wrapper class.
083 * @return the wrapper if <code>p</code> is primitive, else <code>p</code>.
084 * @since JXPath 1.3
085 */
086 public static Class wrapPrimitive(Class p) {
087 return p.isPrimitive() ? (Class) PRIMITIVE_TYPE_MAP.get(p) : p;
088 }
089 }