KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > spi > PrimitiveInfo


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.reflect.spi;
23
24 import java.io.ObjectStreamException JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 import org.jboss.reflect.plugins.ClassInfoImpl;
29 import org.jboss.reflect.plugins.ValueConvertor;
30 import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactory;
31
32 /**
33  * Primitive info
34  *
35  * @todo fix the introspection assumption
36  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
37  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
38  */

39 public class PrimitiveInfo implements TypeInfo, Serializable JavaDoc
40 {
41    /** serialVersionUID */
42    private static final long serialVersionUID = 3256718498443835449L;
43
44    /** The boolean info */
45    public static final PrimitiveInfo BOOLEAN = new PrimitiveInfo("boolean", 0, Boolean.TYPE);
46
47    /** The byte info */
48    public static final PrimitiveInfo BYTE = new PrimitiveInfo("byte", 1, Byte.TYPE);
49
50    /** The char info */
51    public static final PrimitiveInfo CHAR = new PrimitiveInfo("char", 2, Character.TYPE);
52
53    /** The double info */
54    public static final PrimitiveInfo DOUBLE = new PrimitiveInfo("double", 3, Double.TYPE);
55
56    /** The float info */
57    public static final PrimitiveInfo FLOAT = new PrimitiveInfo("float", 4, Float.TYPE);
58
59    /** The int info */
60    public static final PrimitiveInfo INT = new PrimitiveInfo("int", 5, Integer.TYPE);
61
62    /** The long info */
63    public static final PrimitiveInfo LONG = new PrimitiveInfo("long", 6, Long.TYPE);
64
65    /** The short info */
66    public static final PrimitiveInfo SHORT = new PrimitiveInfo("short", 7, Short.TYPE);
67
68    /** The void info */
69    public static final PrimitiveInfo VOID = new PrimitiveInfo("void", 8, Void.TYPE);
70
71    /** The primitives */
72    private static final PrimitiveInfo[] values = {BOOLEAN, BYTE, CHAR, DOUBLE, FLOAT, INT, LONG, SHORT, VOID};
73
74    /** The type info factory */
75    protected static final TypeInfoFactory typeInfoFactory = new IntrospectionTypeInfoFactory();
76
77    /** The name */
78    protected final transient String JavaDoc name;
79
80    /** The ordinal */
81    protected final int ordinal;
82    
83    /** The type */
84    protected final transient Class JavaDoc<? extends Object JavaDoc> type;
85
86    /** The primitives */
87    private static HashMap JavaDoc<String JavaDoc, PrimitiveInfo> map = new HashMap JavaDoc<String JavaDoc, PrimitiveInfo>();
88
89    static
90    {
91       map.put("boolean", BOOLEAN);
92       map.put("byte", BYTE);
93       map.put("char", CHAR);
94       map.put("double", DOUBLE);
95       map.put("float", FLOAT);
96       map.put("int", INT);
97       map.put("long", LONG);
98       map.put("short", SHORT);
99       map.put("void", VOID);
100    }
101
102    /**
103     * Get the primitive info for a type
104     *
105     * @param name the name
106     * @return the info
107     */

108    public static PrimitiveInfo valueOf(String JavaDoc name)
109    {
110       return map.get(name);
111    }
112
113    /**
114     * Create a new primitive info
115     *
116     * @param name the name
117     * @param ordinal the oridinal
118     * @param type the class
119     */

120    protected PrimitiveInfo(String JavaDoc name, int ordinal, Class JavaDoc<? extends Object JavaDoc> type)
121    {
122       this.name = name;
123       this.ordinal = ordinal;
124       this.type = type;
125    }
126
127    /**
128     * Get the ordinal
129     *
130     * @return the oridinal
131     */

132    public int ordinal()
133    {
134       return ordinal;
135    }
136
137    public String JavaDoc getName()
138    {
139       return name;
140    }
141    
142    public Class JavaDoc getType()
143    {
144       return type;
145    }
146    
147    public Object JavaDoc convertValue(Object JavaDoc value) throws Throwable JavaDoc
148    {
149       return ValueConvertor.convertValue(type, value);
150    }
151
152    public boolean isArray()
153    {
154       return false;
155    }
156
157    public TypeInfo getArrayType(int depth)
158    {
159       Class JavaDoc arrayClass = ClassInfoImpl.getArrayClass(getType(), depth);
160       return typeInfoFactory.getTypeInfo(arrayClass);
161    }
162
163    public Object JavaDoc[] newArrayInstance(int size) throws Throwable JavaDoc
164    {
165       throw new UnsupportedOperationException JavaDoc("Not an array " + name);
166    }
167
168    public String JavaDoc toString()
169    {
170       return name;
171    }
172
173    public boolean equals(Object JavaDoc obj)
174    {
175       if (obj == this)
176          return true;
177       if (obj == null)
178          return false;
179       if (!(obj instanceof PrimitiveInfo))
180          return false;
181       if (!obj.getClass().equals(this.getClass()))
182          return false;
183       PrimitiveInfo other = (PrimitiveInfo) obj;
184       return other.ordinal == this.ordinal;
185    }
186
187    public int hashCode()
188    {
189       return name.hashCode();
190    }
191
192    Object JavaDoc readResolve() throws ObjectStreamException JavaDoc
193    {
194       return values[ordinal];
195    }
196 }
197
Popular Tags