Source Code Cross Referenced for Coercion.java in  » Library » Apache-commons-jexl-1.1-src » org » apache » commons » jexl » util » 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 » Library » Apache commons jexl 1.1 src » org.apache.commons.jexl.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002,2004 The Apache Software Foundation.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.apache.commons.jexl.util;
017:
018:        /**
019:         *  Coercion utilities for the JSTL EL-like coercion.
020:         *
021:         *  @since 1.0
022:         *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
023:         */
024:        public class Coercion {
025:
026:            /**
027:             * Coerce to a Boolean.
028:             *
029:             * @param val Object to be coerced.
030:             * @return The Boolean coerced value, or null if none possible.
031:             */
032:            public static Boolean coerceBoolean(Object val) {
033:                if (val == null) {
034:                    return Boolean.FALSE;
035:                } else if (val instanceof  Boolean) {
036:                    return (Boolean) val;
037:                } else if (val instanceof  String) {
038:                    return Boolean.valueOf((String) val);
039:                }
040:                return null;
041:            }
042:
043:            /**
044:             * Coerce to a Integer.
045:             *
046:             * @param val Object to be coerced.
047:             * @return The Integer coerced value.
048:             * @throws Exception If Integer coercion fails.
049:             */
050:            public static Integer coerceInteger(Object val) throws Exception {
051:                if (val == null) {
052:                    return new Integer(0);
053:                } else if (val instanceof  String) {
054:                    if ("".equals(val)) {
055:                        return new Integer(0);
056:                    }
057:                    return Integer.valueOf((String) val);
058:                } else if (val instanceof  Character) {
059:                    return new Integer(((Character) val).charValue());
060:                } else if (val instanceof  Boolean) {
061:                    throw new Exception("Boolean->Integer coercion exception");
062:                } else if (val instanceof  Number) {
063:                    return new Integer(((Number) val).intValue());
064:                }
065:
066:                throw new Exception("Integer coercion exception");
067:            }
068:
069:            /**
070:             * Coerce to a Long.
071:             *
072:             * @param val Object to be coerced.
073:             * @return The Long coerced value.
074:             * @throws Exception If Long coercion fails.
075:             */
076:            public static Long coerceLong(Object val) throws Exception {
077:                if (val == null) {
078:                    return new Long(0);
079:                } else if (val instanceof  String) {
080:                    if ("".equals(val)) {
081:                        return new Long(0);
082:                    }
083:                    return Long.valueOf((String) val);
084:                } else if (val instanceof  Character) {
085:                    return new Long(((Character) val).charValue());
086:                } else if (val instanceof  Boolean) {
087:                    throw new Exception("Boolean->Long coercion exception");
088:                } else if (val instanceof  Number) {
089:                    return new Long(((Number) val).longValue());
090:                }
091:
092:                throw new Exception("Long coercion exception");
093:            }
094:
095:            /**
096:             * Coerce to a Double.
097:             *
098:             * @param val Object to be coerced.
099:             * @return The Double coerced value.
100:             * @throws Exception If Double coercion fails.
101:             */
102:            public static Double coerceDouble(Object val) throws Exception {
103:                if (val == null) {
104:                    return new Double(0);
105:                } else if (val instanceof  String) {
106:                    if ("".equals(val)) {
107:                        return new Double(0);
108:                    }
109:
110:                    /*
111:                     * the spec seems to be iffy about this.  Going to give it a wack
112:                     *  anyway
113:                     */
114:
115:                    return new Double((String) val);
116:                } else if (val instanceof  Character) {
117:                    int i = ((Character) val).charValue();
118:
119:                    return new Double(Double.parseDouble(String.valueOf(i)));
120:                } else if (val instanceof  Boolean) {
121:                    throw new Exception("Boolean->Double coercion exception");
122:                } else if (val instanceof  Double) {
123:                    return (Double) val;
124:                } else if (val instanceof  Number) {
125:                    //The below construct is used rather than ((Number)val).doubleValue() to ensure
126:                    //equality between comparint new Double( 6.4 / 3 ) and the jexl expression of 6.4 / 3
127:                    return new Double(Double.parseDouble(String.valueOf(val)));
128:                }
129:
130:                throw new Exception("Double coercion exception");
131:            }
132:
133:            /**
134:             * Is Object a floating point number.
135:             *
136:             * @param o Object to be analyzed.
137:             * @return true if it is a Float or a Double.
138:             */
139:            public static boolean isFloatingPoint(final Object o) {
140:                return o instanceof  Float || o instanceof  Double;
141:            }
142:
143:            /**
144:             * Is Object a whole number.
145:             *
146:             * @param o Object to be analyzed.
147:             * @return true if Integer, Long, Byte, Short or Character.
148:             */
149:            public static boolean isNumberable(final Object o) {
150:                return o instanceof  Integer || o instanceof  Long
151:                        || o instanceof  Byte || o instanceof  Short
152:                        || o instanceof  Character;
153:            }
154:
155:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.