Source Code Cross Referenced for UnaryExpr.java in  » EJB-Server-resin-3.1.5 » resin » com » caucho » el » 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 » EJB Server resin 3.1.5 » resin » com.caucho.el 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003:         *
004:         * This file is part of Resin(R) Open Source
005:         *
006:         * Each copy or derived work must preserve the copyright notice and this
007:         * notice unmodified.
008:         *
009:         * Resin Open Source is free software; you can redistribute it and/or modify
010:         * it under the terms of the GNU General Public License as published by
011:         * the Free Software Foundation; either version 2 of the License, or
012:         * (at your option) any later version.
013:         *
014:         * Resin Open Source is distributed in the hope that it will be useful,
015:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
016:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017:         * of NON-INFRINGEMENT.  See the GNU General Public License for more
018:         * details.
019:         *
020:         * You should have received a copy of the GNU General Public License
021:         * along with Resin Open Source; if not, write to the
022:         *   Free SoftwareFoundation, Inc.
023:         *   59 Temple Place, Suite 330
024:         *   Boston, MA 02111-1307  USA
025:         *
026:         * @author Scott Ferguson
027:         */
028:
029:        package com.caucho.el;
030:
031:        import com.caucho.vfs.WriteStream;
032:
033:        import javax.el.ELContext;
034:        import javax.el.ELException;
035:        import java.io.IOException;
036:        import java.util.Collection;
037:        import java.util.Map;
038:
039:        /**
040:         * Represents a unary expression.
041:         */
042:        public class UnaryExpr extends Expr {
043:            private int _op;
044:            private Expr _expr;
045:
046:            /**
047:             * Create a new unary expression.
048:             *
049:             * @param op the lexical code for the operation
050:             * @param expr the base expression
051:             */
052:            private UnaryExpr(int op, Expr expr) {
053:                _op = op;
054:                _expr = expr;
055:            }
056:
057:            public static Expr create(int op, Expr expr) {
058:                switch (op) {
059:                case MINUS:
060:                    return new MinusExpr(expr);
061:                }
062:
063:                return new UnaryExpr(op, expr);
064:            }
065:
066:            /**
067:             * Returns true if this is a constant expression.
068:             */
069:            @Override
070:            public boolean isConstant() {
071:                return _expr.isConstant();
072:            }
073:
074:            /**
075:             * Evaluate the expression as an object.
076:             *
077:             * @param env the variable resolver
078:             *
079:             * @return the value as an object
080:             */
081:            @Override
082:            public Object getValue(ELContext env) throws ELException {
083:                switch (_op) {
084:                case NOT:
085:                    return new Boolean(!_expr.evalBoolean(env));
086:
087:                case MINUS: {
088:                    Object obj = _expr.getValue(env);
089:
090:                    if (obj == null)
091:                        return new Long(0);
092:                    else if (obj instanceof  Double || obj instanceof  Float)
093:                        return new Double(-((Number) obj).doubleValue());
094:                    else if (obj instanceof  Number)
095:                        return new Long(-((Number) obj).longValue());
096:                    else if (obj instanceof  String) {
097:                        String s = (String) obj;
098:
099:                        if (s.indexOf('.') < 0)
100:                            return new Long(-toLong(obj, env));
101:                        else
102:                            return new Double(-toDouble(obj, env));
103:                    } else
104:                        return new Double(-toDouble(obj, env));
105:                }
106:
107:                case EMPTY:
108:                    if (evalBoolean(env))
109:                        return Boolean.TRUE;
110:                    else
111:                        return Boolean.FALSE;
112:                }
113:
114:                throw new UnsupportedOperationException();
115:            }
116:
117:            /**
118:             * Evaluate the expression as a boolean.
119:             *
120:             * @param env the variable resolver
121:             *
122:             * @return the value as a boolean
123:             */
124:            @Override
125:            public boolean evalBoolean(ELContext env) throws ELException {
126:                switch (_op) {
127:                case NOT:
128:                    return !_expr.evalBoolean(env);
129:
130:                case EMPTY: {
131:                    Object obj = _expr.getValue(env);
132:
133:                    if (obj == null)
134:                        return true;
135:                    else if (obj instanceof  String)
136:                        return "".equals(obj);
137:                    else if (obj instanceof  Collection)
138:                        return ((Collection) obj).isEmpty();
139:                    else if (obj instanceof  Map)
140:                        return ((Map) obj).isEmpty();
141:                    else if (obj.getClass().isArray())
142:                        return java.lang.reflect.Array.getLength(obj) == 0;
143:                    else
144:                        return false;
145:                }
146:                }
147:
148:                ELException e = new ELException(L.l("can't compare."));
149:
150:                error(e, env);
151:
152:                return false;
153:            }
154:
155:            /**
156:             * Evaluate the expression as a long
157:             *
158:             * @param env the variable resolver
159:             *
160:             * @return the value as a long
161:             */
162:            @Override
163:            public long evalLong(ELContext env) throws ELException {
164:                if (_op != MINUS) {
165:                    ELException e = new ELException(
166:                            L
167:                                    .l("'not' and 'empty' operations can not be converted to long values."));
168:                    error(e, env);
169:
170:                    return 0;
171:                }
172:
173:                // _op == MINUS
174:                return -_expr.evalLong(env);
175:            }
176:
177:            /**
178:             * Evaluate the expression as a double
179:             */
180:            @Override
181:            public double evalDouble(ELContext env) throws ELException {
182:                if (_op != MINUS) {
183:                    ELException e = new ELException(
184:                            L
185:                                    .l("'not' and 'empty' operations can not be converted to double values."));
186:
187:                    error(e, env);
188:
189:                    return 0;
190:                }
191:
192:                // _op == MINUS
193:                return -_expr.evalDouble(env);
194:            }
195:
196:            /**
197:             * Prints the Java code to recreate the UnaryExpr.
198:             */
199:            public void printCreate(WriteStream os) throws IOException {
200:                os.print("com.caucho.el.UnaryExpr.create(");
201:                os.print(_op + ", ");
202:                _expr.printCreate(os);
203:                os.print(")");
204:            }
205:
206:            /**
207:             * Returns true for equal strings.
208:             */
209:            public boolean equals(Object o) {
210:                if (!(o instanceof  UnaryExpr))
211:                    return false;
212:
213:                UnaryExpr uexpr = (UnaryExpr) o;
214:
215:                return (_op == uexpr._op && _expr.equals(uexpr._expr));
216:            }
217:
218:            /**
219:             * Returns a readable representation of the expr.
220:             */
221:            public String toString() {
222:                String op;
223:
224:                switch (_op) {
225:                case MINUS:
226:                    op = " -";
227:                    break;
228:                case NOT:
229:                    op = " not ";
230:                    break;
231:                case EMPTY:
232:                    op = " empty ";
233:                    break;
234:                default:
235:                    op = " unknown(" + _op + ") ";
236:                    break;
237:                }
238:
239:                return op + _expr;
240:            }
241:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.