Source Code Cross Referenced for Primitives.java in  » Ajax » zk » org » zkoss » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Ajax » zk » org.zkoss.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.