001: /* Primitives.java
002:
003: {{IS_NOTE
004:
005: Purpose:
006: Description:
007: History:
008: 2002/3/25, Henri Chen: Created.
009: 2003/4/17, Tom M. Yeh: Moves primitive relevant utilities from Classes
010:
011: }}IS_NOTE
012:
013: Copyright (C) 2001 Potix Corporation. All Rights Reserved.
014:
015: {{IS_RIGHT
016: This program is distributed under GPL Version 2.0 in the hope that
017: it will be useful, but WITHOUT ANY WARRANTY.
018: }}IS_RIGHT
019: */
020: package org.zkoss.lang;
021:
022: import java.util.Map;
023: import java.util.HashMap;
024:
025: /**
026: * Utilities regarding primitive type and its wrapper class.
027: *
028: * @author henrichen
029: */
030: public class Primitives {
031:
032: /**
033: * Convert Boolean object to primitive boolean.
034: */
035: public static final boolean toPrimitive(Boolean obj) {
036: return obj.booleanValue();
037: }
038:
039: /**
040: * Convert primitive boolean to Boolean.
041: */
042: public static final Boolean toWrapper(boolean obj) {
043: return Boolean.valueOf(obj);
044: }
045:
046: /**
047: * Convert Byte object to primitive byte.
048: */
049: public static final byte toPrimitive(Byte obj) {
050: return obj.byteValue();
051: }
052:
053: /**
054: * Convert primitive byte to Byte.
055: */
056: public static final Byte toWrapper(byte obj) {
057: return new Byte(obj);
058: }
059:
060: /**
061: * Convert Character object to primitive char.
062: */
063: public static final char toPrimitive(Character obj) {
064: return obj.charValue();
065: }
066:
067: /**
068: * Convert primitive char to Character.
069: */
070: public static final Character toWrapper(char obj) {
071: return new Character(obj);
072: }
073:
074: /**
075: * Convert Double object to primitive double.
076: */
077: public static final double toPrimitive(Double obj) {
078: return obj.doubleValue();
079: }
080:
081: /**
082: * Convert primitive double to Double.
083: */
084: public static final Double toWrapper(double obj) {
085: return new Double(obj);
086: }
087:
088: /**
089: * Convert Float object to primitive float.
090: */
091: public static final float toPrimitive(Float obj) {
092: return obj.floatValue();
093: }
094:
095: /**
096: * Convert primitive float to Float.
097: */
098: public static final Float toWrapper(float obj) {
099: return new Float(obj);
100: }
101:
102: /**
103: * Convert Integer object to primitive int.
104: */
105: public static final int toPrimitive(Integer obj) {
106: return obj.intValue();
107: }
108:
109: /**
110: * Convert primitive int to Integer.
111: */
112: public static final Integer toWrapper(int obj) {
113: return new Integer(obj);
114: }
115:
116: /**
117: * Convert Long object to primitive long.
118: */
119: public static final long toPrimitive(Long obj) {
120: return obj.longValue();
121: }
122:
123: /**
124: * Convert primitive long to Long.
125: */
126: public static final Long toWrapper(long obj) {
127: return new Long(obj);
128: }
129:
130: /**
131: * Convert Short object to primitive short.
132: */
133: public static final short toPrimitive(Short obj) {
134: return obj.shortValue();
135: }
136:
137: /**
138: * Convert primitive short to Short.
139: */
140: public static final Short toWrapper(short obj) {
141: return new Short(obj);
142: }
143:
144: /** The infomation about a primitive. */
145: private static class PrimInfo {
146: private final Class cls;
147: private final Object defVal;
148: private final char code;
149:
150: private PrimInfo(Class cls, Object defVal, char code) {
151: this .cls = cls;
152: this .defVal = defVal;
153: this .code = code;
154: }
155: }
156:
157: private static final Map _prims = new HashMap(23);
158: static {
159: _prims.put("int", new PrimInfo(int.class, new Integer(0), 'I'));
160: _prims.put("boolean", new PrimInfo(boolean.class,
161: Boolean.FALSE, 'Z'));
162: _prims.put("short", new PrimInfo(short.class, new Short(
163: (short) 0), 'S'));
164: _prims.put("byte", new PrimInfo(byte.class, new Byte((byte) 0),
165: 'B'));
166: _prims.put("char", new PrimInfo(char.class, new Character(
167: (char) 0), 'C'));
168: _prims.put("long", new PrimInfo(long.class, new Long(0), 'L'));
169: _prims.put("double", new PrimInfo(double.class, new Double(0),
170: 'D'));
171: _prims.put("float",
172: new PrimInfo(float.class, new Float(0), 'F'));
173: _prims.put("void", new PrimInfo(void.class, null, 'V'));
174:
175: //we can use the same map because key is in diff class
176: _prims.put(Integer.class, int.class);
177: _prims.put(Boolean.class, boolean.class);
178: _prims.put(Short.class, short.class);
179: _prims.put(Byte.class, byte.class);
180: _prims.put(Character.class, char.class);
181: _prims.put(Long.class, long.class);
182: _prims.put(Double.class, double.class);
183: _prims.put(Float.class, float.class);
184: _prims.put(Void.class, void.class);
185: };
186:
187: /** Returns the notation of a primitive class,
188: * or ((char)0) if it is not a primitive class.
189: * Example, I for int, Z for boolean...
190: */
191: public static final char getNotation(String className) {
192: final PrimInfo pi = (PrimInfo) _prims.get(className);
193: return pi != null ? pi.code : (char) 0;
194: }
195:
196: /** Returns the default value of a primitive class,
197: * or null if it is not a primitive class.
198: * Example, getDefaultValue(int.class) returns Integer(0).
199: */
200: public static final Object getDefaultValue(Class cls) {
201: final PrimInfo pi = (PrimInfo) _prims.get(cls.getName());
202: return pi != null ? pi.defVal : null;
203: }
204:
205: /** Converts a primitive from name to the class,
206: * or null if it is not a primitive class.
207: * <p>Example, toClass("int") returns int.class.
208: */
209: public static final Class toClass(String clsName) {
210: final PrimInfo pi = (PrimInfo) _prims.get(clsName);
211: return pi != null ? pi.cls : null;
212: }
213:
214: /** Returns the primitive class of the giving wrapper class,
215: * or null if it is not a wrapper class.
216: * <p>Example, toPrimitive(Integer.class) returns int.class.
217: */
218: public static final Class toPrimitive(Class wrapper) {
219: return (Class) _prims.get(wrapper);
220: }
221:
222: /** Returns the wrapper class of a primitive class,
223: * or null if it is not a primitive class.
224: * <p>Example, toWrapper(int.class) return Integer.class.
225: */
226: public static final Class toWrapper(Class primitive) {
227: if (!primitive.isPrimitive())
228: return null;
229: if (primitive.equals(void.class))
230: return Void.class;
231: return getDefaultValue(primitive).getClass();
232: }
233:
234: /** Tests whether a class name is a primitive class, e.g., int and void.
235: */
236: public static final boolean isPrimitive(String clsName) {
237: return _prims.containsKey(clsName);
238: }
239: }
|