Source Code Cross Referenced for ArithmeticExpression.java in  » Testing » mockrunner-0.4 » org » activemq » filter » mockrunner » 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 » Testing » mockrunner 0.4 » org.activemq.filter.mockrunner 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /** 
002:         * 
003:         * Copyright 2004 Protique Ltd
004:         * Copyright 2004 Hiram Chirino
005:         * 
006:         * Licensed under the Apache License, Version 2.0 (the "License"); 
007:         * you may not use this file except in compliance with the License. 
008:         * You may obtain a copy of the License at 
009:         * 
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS, 
014:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
015:         * See the License for the specific language governing permissions and 
016:         * limitations under the License. 
017:         * 
018:         **/package org.activemq.filter.mockrunner;
019:
020:        import javax.jms.JMSException;
021:        import javax.jms.Message;
022:
023:        /**
024:         * Alwin Ibba: Changed package
025:         * 
026:         * An expression which performs an operation on two expression values
027:         * 
028:         * @version $Revision: 1.3 $
029:         */
030:        public abstract class ArithmeticExpression extends BinaryExpression {
031:
032:            protected static final int INTEGER = 1;
033:            protected static final int LONG = 2;
034:            protected static final int DOUBLE = 3;
035:
036:            /**
037:             * @param left
038:             * @param right
039:             */
040:            public ArithmeticExpression(Expression left, Expression right) {
041:                super (left, right);
042:            }
043:
044:            public static Expression createPlus(Expression left,
045:                    Expression right) {
046:                return new ArithmeticExpression(left, right) {
047:                    protected Object evaluate(Object lvalue, Object rvalue) {
048:                        if (lvalue instanceof  String) {
049:                            String text = (String) lvalue;
050:                            String answer = text + rvalue;
051:                            System.out.println("lvalue: " + lvalue
052:                                    + " rvalue: " + rvalue + " result: "
053:                                    + answer);
054:                            return answer;
055:                        } else if (lvalue instanceof  Number) {
056:                            return plus((Number) lvalue, asNumber(rvalue));
057:                        }
058:                        throw new RuntimeException(
059:                                "Cannot call plus operation on: " + lvalue
060:                                        + " and: " + rvalue);
061:                    }
062:
063:                    public String getExpressionSymbol() {
064:                        return "+";
065:                    }
066:                };
067:            }
068:
069:            public static Expression createMinus(Expression left,
070:                    Expression right) {
071:                return new ArithmeticExpression(left, right) {
072:                    protected Object evaluate(Object lvalue, Object rvalue) {
073:                        if (lvalue instanceof  Number) {
074:                            return minus((Number) lvalue, asNumber(rvalue));
075:                        }
076:                        throw new RuntimeException(
077:                                "Cannot call minus operation on: " + lvalue
078:                                        + " and: " + rvalue);
079:                    }
080:
081:                    public String getExpressionSymbol() {
082:                        return "-";
083:                    }
084:                };
085:            }
086:
087:            public static Expression createMultiply(Expression left,
088:                    Expression right) {
089:                return new ArithmeticExpression(left, right) {
090:
091:                    protected Object evaluate(Object lvalue, Object rvalue) {
092:                        if (lvalue instanceof  Number) {
093:                            return multiply((Number) lvalue, asNumber(rvalue));
094:                        }
095:                        throw new RuntimeException(
096:                                "Cannot call multiply operation on: " + lvalue
097:                                        + " and: " + rvalue);
098:                    }
099:
100:                    public String getExpressionSymbol() {
101:                        return "*";
102:                    }
103:                };
104:            }
105:
106:            public static Expression createDivide(Expression left,
107:                    Expression right) {
108:                return new ArithmeticExpression(left, right) {
109:
110:                    protected Object evaluate(Object lvalue, Object rvalue) {
111:                        if (lvalue instanceof  Number) {
112:                            return divide((Number) lvalue, asNumber(rvalue));
113:                        }
114:                        throw new RuntimeException(
115:                                "Cannot call divide operation on: " + lvalue
116:                                        + " and: " + rvalue);
117:                    }
118:
119:                    public String getExpressionSymbol() {
120:                        return "/";
121:                    }
122:                };
123:            }
124:
125:            public static Expression createMod(Expression left, Expression right) {
126:                return new ArithmeticExpression(left, right) {
127:
128:                    protected Object evaluate(Object lvalue, Object rvalue) {
129:                        if (lvalue instanceof  Number) {
130:                            return mod((Number) lvalue, asNumber(rvalue));
131:                        }
132:                        throw new RuntimeException(
133:                                "Cannot call mod operation on: " + lvalue
134:                                        + " and: " + rvalue);
135:                    }
136:
137:                    public String getExpressionSymbol() {
138:                        return "%";
139:                    }
140:                };
141:            }
142:
143:            protected Number plus(Number left, Number right) {
144:                switch (numberType(left, right)) {
145:                case INTEGER:
146:                    return new Integer(left.intValue() + right.intValue());
147:                case LONG:
148:                    return new Long(left.longValue() + right.longValue());
149:                default:
150:                    return new Double(left.doubleValue() + right.doubleValue());
151:                }
152:            }
153:
154:            protected Number minus(Number left, Number right) {
155:                switch (numberType(left, right)) {
156:                case INTEGER:
157:                    return new Integer(left.intValue() - right.intValue());
158:                case LONG:
159:                    return new Long(left.longValue() - right.longValue());
160:                default:
161:                    return new Double(left.doubleValue() - right.doubleValue());
162:                }
163:            }
164:
165:            protected Number multiply(Number left, Number right) {
166:                switch (numberType(left, right)) {
167:                case INTEGER:
168:                    return new Integer(left.intValue() * right.intValue());
169:                case LONG:
170:                    return new Long(left.longValue() * right.longValue());
171:                default:
172:                    return new Double(left.doubleValue() * right.doubleValue());
173:                }
174:            }
175:
176:            protected Number divide(Number left, Number right) {
177:                return new Double(left.doubleValue() / right.doubleValue());
178:            }
179:
180:            protected Number mod(Number left, Number right) {
181:                return new Double(left.doubleValue() % right.doubleValue());
182:            }
183:
184:            private int numberType(Number left, Number right) {
185:                if (isDouble(left) || isDouble(right)) {
186:                    return DOUBLE;
187:                } else if (left instanceof  Long || right instanceof  Long) {
188:                    return LONG;
189:                } else {
190:                    return INTEGER;
191:                }
192:            }
193:
194:            private boolean isDouble(Number n) {
195:                return n instanceof  Float || n instanceof  Double;
196:            }
197:
198:            protected Number asNumber(Object value) {
199:                if (value instanceof  Number) {
200:                    return (Number) value;
201:                } else {
202:                    throw new RuntimeException("Cannot convert value: " + value
203:                            + " into a number");
204:                }
205:            }
206:
207:            public Object evaluate(Message message) throws JMSException {
208:                Object lvalue = left.evaluate(message);
209:                if (lvalue == null) {
210:                    return null;
211:                }
212:                Object rvalue = right.evaluate(message);
213:                if (rvalue == null) {
214:                    return null;
215:                }
216:                return evaluate(lvalue, rvalue);
217:            }
218:
219:            /**
220:             * @param lvalue
221:             * @param rvalue
222:             * @return
223:             */
224:            abstract protected Object evaluate(Object lvalue, Object rvalue);
225:
226:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.