ParserUtils.java :  » Development » jga-Generic-Algorithms » net » sf » jga » parser » Java Open Source

Java Open Source » Development » jga Generic Algorithms 
jga Generic Algorithms » net » sf » jga » parser » ParserUtils.java
// ============================================================================
// $Id: ParserUtils.java,v 1.3 2006/12/16 16:48:57 davidahall Exp $
// Copyright (c) 2005  David A. Hall
// ============================================================================
// The contents of this file are subject to the Common Development and
// Distribution License (CDDL), Version 1.0 (the License); you may not use this 
// file except in compliance with the License.  You should have received a copy
// of the the License along with this file: if not, a copy of the License is 
// available from Sun Microsystems, Inc.
//
// http://www.sun.com/cddl/cddl.html
//
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
// publish revised and/or new versions of the License.  You may not use,  
// distribute, or otherwise make this file available under subsequent versions 
// of the License.
// 
// Alternatively, the contents of this file may be used under the terms of the 
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
// case the provisions of the LGPL are applicable instead of those above. If you 
// wish to allow use of your version of this file only under the terms of the 
// LGPL, and not to allow others to use your version of this file under the 
// terms of the CDDL, indicate your decision by deleting the provisions above 
// and replace them with the notice and other provisions required by the LGPL. 
// If you do not delete the provisions above, a recipient may use your version 
// of this file under the terms of either the CDDL or the LGPL.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
// ============================================================================

package net.sf.jga.parser;

import java.util.HashMap;
import java.util.Map;

/**
 * ParserUtils.java
 *
 * <p>
 * Copyright &copy; 2005  David A. Hall
 * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
 */

public class ParserUtils {
    private ParserUtils (){}

    //======================
    // Boxing/Unboxing
    //======================
                            
    static private Map boxedTypes = new HashMap();
    static private Map unboxedTypes = new HashMap();
    static private Map boxFunctors = new HashMap();
    static private Map unboxFunctors = new HashMap();

    static {
            boxedTypes.put(Boolean.TYPE, Boolean.class);
            boxedTypes.put(Character.TYPE, Character.class);
            boxedTypes.put(Byte.TYPE, Byte.class);
            boxedTypes.put(Short.TYPE, Short.class);
            boxedTypes.put(Integer.TYPE, Integer.class);
            boxedTypes.put(Long.TYPE, Long.class);
            boxedTypes.put(Float.TYPE, Float.class);
            boxedTypes.put(Double.TYPE, Double.class);
            boxedTypes.put(Void.TYPE, Object.class);

            unboxedTypes.put(Boolean.class, Boolean.TYPE);
            unboxedTypes.put(Character.class, Character.TYPE);
            unboxedTypes.put(Byte.class, Byte.TYPE);
            unboxedTypes.put(Short.class, Short.TYPE);
            unboxedTypes.put(Integer.class, Integer.TYPE);
            unboxedTypes.put(Long.class, Long.TYPE);
            unboxedTypes.put(Float.class, Float.TYPE);
            unboxedTypes.put(Double.class, Double.TYPE);

    }

    /**
     * Returns a boxed version of the input type if it is a primitive.  Otherwise, returns
     * the input type.
     */
    static public Class getBoxedType(Class type) {
        Class c = (Class) boxedTypes.get(type);
        return (c == null) ? type : c;
    }


    /**
     * Returns an unboxed version of the input type if it is a reference.  Otherwise, returns
     * the input type.
     */
    static public Class getUnboxedType(Class type) {
        Class c = (Class) unboxedTypes.get(type);
        return (c == null) ? type : c;
    }


    /**
     * Returns true if first class is the boxed type of the second
     */

    static public boolean isBoxedType(Class primitive, Class boxedType) {
        return primitive.isPrimitive() && (primitive == getUnboxedType(boxedType));
    }

    /**
     * Returns the simple name of a class.  The built in method in 1.5 is unavailable in 1.4, and
     * slightly buggy when it comes to names of nested classes.
     */
    static public  String getSimpleName(Class clasz) {
        String hostName = "";
        String delim = ".";
        
        Class host = clasz.getDeclaringClass();
        if (host != null) {
            hostName += getSimpleName(host) + ".";
            delim = "$";
        }
        
        String name = clasz.getName();
        String simple = name.substring(name.lastIndexOf(delim)+1);

        return hostName + simple;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.