001: // ============================================================================
002: // $Id: ParserUtils.java,v 1.3 2006/12/16 16:48:57 davidahall Exp $
003: // Copyright (c) 2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.parser;
034:
035: import java.util.HashMap;
036: import java.util.Map;
037:
038: /**
039: * ParserUtils.java
040: *
041: * <p>
042: * Copyright © 2005 David A. Hall
043: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
044: */
045:
046: public class ParserUtils {
047: private ParserUtils() {
048: }
049:
050: //======================
051: // Boxing/Unboxing
052: //======================
053:
054: static private Map boxedTypes = new HashMap();
055: static private Map unboxedTypes = new HashMap();
056: static private Map boxFunctors = new HashMap();
057: static private Map unboxFunctors = new HashMap();
058:
059: static {
060: boxedTypes.put(Boolean.TYPE, Boolean.class);
061: boxedTypes.put(Character.TYPE, Character.class);
062: boxedTypes.put(Byte.TYPE, Byte.class);
063: boxedTypes.put(Short.TYPE, Short.class);
064: boxedTypes.put(Integer.TYPE, Integer.class);
065: boxedTypes.put(Long.TYPE, Long.class);
066: boxedTypes.put(Float.TYPE, Float.class);
067: boxedTypes.put(Double.TYPE, Double.class);
068: boxedTypes.put(Void.TYPE, Object.class);
069:
070: unboxedTypes.put(Boolean.class, Boolean.TYPE);
071: unboxedTypes.put(Character.class, Character.TYPE);
072: unboxedTypes.put(Byte.class, Byte.TYPE);
073: unboxedTypes.put(Short.class, Short.TYPE);
074: unboxedTypes.put(Integer.class, Integer.TYPE);
075: unboxedTypes.put(Long.class, Long.TYPE);
076: unboxedTypes.put(Float.class, Float.TYPE);
077: unboxedTypes.put(Double.class, Double.TYPE);
078:
079: }
080:
081: /**
082: * Returns a boxed version of the input type if it is a primitive. Otherwise, returns
083: * the input type.
084: */
085: static public Class getBoxedType(Class type) {
086: Class c = (Class) boxedTypes.get(type);
087: return (c == null) ? type : c;
088: }
089:
090: /**
091: * Returns an unboxed version of the input type if it is a reference. Otherwise, returns
092: * the input type.
093: */
094: static public Class getUnboxedType(Class type) {
095: Class c = (Class) unboxedTypes.get(type);
096: return (c == null) ? type : c;
097: }
098:
099: /**
100: * Returns true if first class is the boxed type of the second
101: */
102:
103: static public boolean isBoxedType(Class primitive, Class boxedType) {
104: return primitive.isPrimitive()
105: && (primitive == getUnboxedType(boxedType));
106: }
107:
108: /**
109: * Returns the simple name of a class. The built in method in 1.5 is unavailable in 1.4, and
110: * slightly buggy when it comes to names of nested classes.
111: */
112: static public String getSimpleName(Class clasz) {
113: String hostName = "";
114: String delim = ".";
115:
116: Class host = clasz.getDeclaringClass();
117: if (host != null) {
118: hostName += getSimpleName(host) + ".";
119: delim = "$";
120: }
121:
122: String name = clasz.getName();
123: String simple = name.substring(name.lastIndexOf(delim) + 1);
124:
125: return hostName + simple;
126: }
127: }
|