001// Copyright 2006 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.services;
016
017import static org.apache.tapestry5.ioc.internal.util.CollectionFactory.newMap;
018
019import java.util.Map;
020
021/**
022 * Support code for generating code (used when transforming component classes).
023 */
024public final class TransformUtils
025{
026    private static final Map<String, PrimitiveTypeInfo> nameToInfo = newMap();
027
028    private static final Map<Class, PrimitiveTypeInfo> classToInfo = newMap();
029
030    static class PrimitiveTypeInfo
031    {
032        private final Class wrapperType;
033
034        private final String unwrapperMethodName;
035
036        private final String defaultValue;
037
038        public PrimitiveTypeInfo(Class wrapperType, String unwrapperMethodName, String defaultValue)
039        {
040            this.wrapperType = wrapperType;
041            this.unwrapperMethodName = unwrapperMethodName;
042            this.defaultValue = defaultValue;
043        }
044    }
045
046    static
047    {
048        add(boolean.class, Boolean.class, "booleanValue", "false");
049        add(byte.class, Byte.class, "byteValue", "0");
050        add(char.class, Character.class, "charValue", "0");
051        add(short.class, Short.class, "shortValue", "0");
052        add(int.class, Integer.class, "intValue", "0");
053        add(long.class, Long.class, "longValue", "0L");
054        add(float.class, Float.class, "floatValue", "0.0f");
055        add(double.class, Double.class, "doubleValue", "0.0d");
056    }
057
058    private TransformUtils()
059    {
060    }
061
062    private static void add(Class primitiveType, Class wrapperType, String unwrapperMethodName, String defaultValue)
063    {
064        PrimitiveTypeInfo info = new PrimitiveTypeInfo(wrapperType, unwrapperMethodName, defaultValue);
065
066        classToInfo.put(primitiveType, info);
067        nameToInfo.put(primitiveType.getName(), info);
068    }
069
070    /**
071     * Returns true if the specified type is a primitive type.
072     */
073    public static boolean isPrimitive(String type)
074    {
075        return nameToInfo.containsKey(type);
076    }
077
078    /**
079     * Returns the name of wrapper type for a given input type. For primitive types, returns the wrapper type. For other
080     * types, returns the input type name.
081     *
082     * @param type primitive type name, or fully qualified class name
083     */
084    public static String getWrapperTypeName(String type)
085    {
086        PrimitiveTypeInfo info = nameToInfo.get(type);
087
088        return info == null ? type : info.wrapperType.getName();
089    }
090
091    /**
092     * For primitive types, returns the method on the <em>wrapper type</em> that converts back to the primitive.
093     *
094     * @param type the primitive type
095     * @return the method of the corresponding wrapper type, or null if type is not a primitive type
096     */
097    public static String getUnwrapperMethodName(String type)
098    {
099        PrimitiveTypeInfo info = nameToInfo.get(type);
100
101        return info == null ? null : info.unwrapperMethodName;
102    }
103
104    /**
105     * Returns the wrapper type for a given input type. For primitive types, returns the wrapper type. For other types,
106     * returns the type itself.
107     *
108     * @param type primitive or object type
109     */
110    public static Class getWrapperType(Class type)
111    {
112        PrimitiveTypeInfo info = classToInfo.get(type);
113
114        return info == null ? type : info.wrapperType;
115    }
116
117    /**
118     * Returns the default value for a type. This is the string "null" for most types, or a literal value for primitive
119     * types.
120     */
121    public static String getDefaultValue(String type)
122    {
123        PrimitiveTypeInfo info = nameToInfo.get(type);
124
125        return info == null ? "null" : info.defaultValue;
126    }
127}