KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > parser > ParserUtils


1 // ============================================================================
2
// $Id: ParserUtils.java,v 1.3 2006/12/16 16:48:57 davidahall Exp $
3
// Copyright (c) 2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.parser;
34
35 import java.util.HashMap JavaDoc;
36 import java.util.Map JavaDoc;
37
38 /**
39  * ParserUtils.java
40  *
41  * <p>
42  * Copyright &copy; 2005 David A. Hall
43  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
44  */

45
46 public class ParserUtils {
47     private ParserUtils (){}
48
49     //======================
50
// Boxing/Unboxing
51
//======================
52

53     static private Map JavaDoc boxedTypes = new HashMap JavaDoc();
54     static private Map JavaDoc unboxedTypes = new HashMap JavaDoc();
55     static private Map JavaDoc boxFunctors = new HashMap JavaDoc();
56     static private Map JavaDoc unboxFunctors = new HashMap JavaDoc();
57
58     static {
59             boxedTypes.put(Boolean.TYPE, Boolean JavaDoc.class);
60             boxedTypes.put(Character.TYPE, Character JavaDoc.class);
61             boxedTypes.put(Byte.TYPE, Byte JavaDoc.class);
62             boxedTypes.put(Short.TYPE, Short JavaDoc.class);
63             boxedTypes.put(Integer.TYPE, Integer JavaDoc.class);
64             boxedTypes.put(Long.TYPE, Long JavaDoc.class);
65             boxedTypes.put(Float.TYPE, Float JavaDoc.class);
66             boxedTypes.put(Double.TYPE, Double JavaDoc.class);
67             boxedTypes.put(Void.TYPE, Object JavaDoc.class);
68
69             unboxedTypes.put(Boolean JavaDoc.class, Boolean.TYPE);
70             unboxedTypes.put(Character JavaDoc.class, Character.TYPE);
71             unboxedTypes.put(Byte JavaDoc.class, Byte.TYPE);
72             unboxedTypes.put(Short JavaDoc.class, Short.TYPE);
73             unboxedTypes.put(Integer JavaDoc.class, Integer.TYPE);
74             unboxedTypes.put(Long JavaDoc.class, Long.TYPE);
75             unboxedTypes.put(Float JavaDoc.class, Float.TYPE);
76             unboxedTypes.put(Double JavaDoc.class, Double.TYPE);
77
78     }
79
80     /**
81      * Returns a boxed version of the input type if it is a primitive. Otherwise, returns
82      * the input type.
83      */

84     static public Class JavaDoc getBoxedType(Class JavaDoc type) {
85         Class JavaDoc c = (Class JavaDoc) boxedTypes.get(type);
86         return (c == null) ? type : c;
87     }
88
89
90     /**
91      * Returns an unboxed version of the input type if it is a reference. Otherwise, returns
92      * the input type.
93      */

94     static public Class JavaDoc getUnboxedType(Class JavaDoc type) {
95         Class JavaDoc c = (Class JavaDoc) unboxedTypes.get(type);
96         return (c == null) ? type : c;
97     }
98
99
100     /**
101      * Returns true if first class is the boxed type of the second
102      */

103
104     static public boolean isBoxedType(Class JavaDoc primitive, Class JavaDoc boxedType) {
105         return primitive.isPrimitive() && (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 JavaDoc getSimpleName(Class JavaDoc clasz) {
113         String JavaDoc hostName = "";
114         String JavaDoc delim = ".";
115         
116         Class JavaDoc host = clasz.getDeclaringClass();
117         if (host != null) {
118             hostName += getSimpleName(host) + ".";
119             delim = "$";
120         }
121         
122         String JavaDoc name = clasz.getName();
123         String JavaDoc simple = name.substring(name.lastIndexOf(delim)+1);
124
125         return hostName + simple;
126     }
127 }
128
Popular Tags