Source Code Cross Referenced for UnaryOperator.java in  » Scripting » Pnuts » pnuts » 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 » Scripting » Pnuts » pnuts.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)UnaryOperator.java 1.2 04/12/06
003:         *
004:         * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
005:         *
006:         * See the file "LICENSE.txt" for information on usage and redistribution
007:         * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008:         */
009:        package pnuts.lang;
010:
011:        import java.io.Serializable;
012:        import java.math.BigDecimal;
013:        import java.math.BigInteger;
014:
015:        /**
016:         * Abstract base class of unary operations.
017:         */
018:        public abstract class UnaryOperator implements  Serializable {
019:
020:            /**
021:             * operation on an int value
022:             */
023:            protected Object op_int(int i) {
024:                throw new IllegalArgumentException(String.valueOf(i));
025:            }
026:
027:            /**
028:             * operation on a long value
029:             */
030:            protected Object op_long(long l) {
031:                throw new IllegalArgumentException(String.valueOf(l));
032:            }
033:
034:            /**
035:             * operation on a float value
036:             */
037:            protected Object op_float(float f) {
038:                throw new IllegalArgumentException(String.valueOf(f));
039:            }
040:
041:            /**
042:             * operation on a double value
043:             */
044:            protected Object op_double(double d) {
045:                throw new IllegalArgumentException(String.valueOf(d));
046:            }
047:
048:            /**
049:             * operation on a BigDecimal
050:             */
051:            protected Object op_bdec(BigDecimal d) {
052:                throw new IllegalArgumentException(String.valueOf(d));
053:            }
054:
055:            /**
056:             * operation on a BigInteger
057:             */
058:            protected Object op_bint(BigInteger b) {
059:                throw new IllegalArgumentException(String.valueOf(b));
060:            }
061:
062:            /**
063:             * operation on a boolean value
064:             */
065:            protected Object op_boolean(boolean b) {
066:                throw new IllegalArgumentException(String.valueOf(b));
067:            }
068:
069:            /**
070:             * operation on a Numeric
071:             */
072:            protected Object op_numeric(Numeric b) {
073:                throw new IllegalArgumentException(String.valueOf(b));
074:            }
075:
076:            /*
077:             * Dispatches a unary expression to a particular operation.
078:             */
079:            public Object operateOn(Object n) {
080:                if (n instanceof  Integer) {
081:                    return op_int(((Integer) n).intValue());
082:                } else if (n instanceof  Character) {
083:                    return op_int((int) ((Character) n).charValue());
084:                } else if (n instanceof  Byte) {
085:                    return op_int(((Byte) n).intValue());
086:                } else if (n instanceof  Short) {
087:                    return op_int(((Short) n).intValue());
088:                } else if (n instanceof  Long) {
089:                    return op_long(((Long) n).longValue());
090:                } else if (n instanceof  Float) {
091:                    return op_float(((Float) n).floatValue());
092:                } else if (n instanceof  Double) {
093:                    return op_double(((Double) n).doubleValue());
094:                } else if (n instanceof  BigDecimal) {
095:                    return op_bdec((BigDecimal) n);
096:                } else if (n instanceof  BigInteger) {
097:                    return op_bint((BigInteger) n);
098:                } else if (n instanceof  Boolean) {
099:                    return op_boolean(((Boolean) n).booleanValue());
100:                } else if (n instanceof  Numeric) {
101:                    return op_numeric((Numeric) n);
102:                } else {
103:                    throw new IllegalArgumentException(String.valueOf(n));
104:                }
105:            }
106:
107:            /**
108:             * The default implementation of ++ operator
109:             */
110:            public static class Add1 extends UnaryOperator {
111:                static Add1 instance = new Add1();
112:
113:                public Object op_int(int i) {
114:
115:                    if (i == Integer.MAX_VALUE) {
116:                        return new Long((long) i + 1);
117:                    } else {
118:                        int l = i + 1;
119:                        if (l >= 0 && l < BinaryOperator.SmallIntSize) {
120:                            return BinaryOperator.smallInt[l];
121:                        } else {
122:                            return new Integer(l);
123:                        }
124:                    }
125:                }
126:
127:                public Object op_long(long i) {
128:                    if (i == Long.MAX_VALUE) {
129:                        return op_bint(BigInteger.valueOf(i));
130:                    } else {
131:                        return new Long(i + 1);
132:                    }
133:                }
134:
135:                public Object op_bint(BigInteger i) {
136:                    return Runtime.compress(i.add(BigInteger.valueOf(1L)));
137:                }
138:            }
139:
140:            /**
141:             * The default implementation of -- operator
142:             */
143:            public static class Subtract1 extends UnaryOperator {
144:                static Subtract1 instance = new Subtract1();
145:
146:                public Object op_int(int i) {
147:                    if (i > 0 && i <= BinaryOperator.SmallIntSize) {
148:                        return BinaryOperator.smallInt[i - 1];
149:                    } else if (i == Integer.MIN_VALUE) {
150:                        return new Long((long) i - 1);
151:                    } else {
152:                        return new Integer(i - 1);
153:                    }
154:                }
155:
156:                public Object op_long(long i) {
157:                    if (i == Long.MIN_VALUE) {
158:                        return op_bint(BigInteger.valueOf(i));
159:                    } else if (i == (long) Integer.MAX_VALUE + 1) {
160:                        return new Integer(Integer.MAX_VALUE);
161:                    } else {
162:                        return new Long(i - 1);
163:                    }
164:                }
165:
166:                public Object op_bint(BigInteger i) {
167:                    return Runtime.compress(i.add(BigInteger.valueOf(-1L)));
168:                }
169:            }
170:
171:            /**
172:             * The default implementation of unary - operator
173:             */
174:            public static class Negate extends UnaryOperator {
175:
176:                static Negate instance = new Negate();
177:
178:                public Object op_int(int n) {
179:                    int l = -n;
180:                    if (l >= 0 && l < BinaryOperator.SmallIntSize) {
181:                        return BinaryOperator.smallInt[l];
182:                    }
183:                    return new Integer(l);
184:                }
185:
186:                public Object op_long(long n) {
187:                    return new Long(-n);
188:                }
189:
190:                public Object op_float(float n) {
191:                    return new Float(-n);
192:                }
193:
194:                public Object op_double(double n) {
195:                    return new Double(-n);
196:                }
197:
198:                public Object op_bdec(BigDecimal n) {
199:                    return ((BigDecimal) n).negate();
200:                }
201:
202:                public Object op_bint(BigInteger n) {
203:                    return ((BigInteger) n).negate();
204:                }
205:
206:                public Object op_numeric(Numeric b) {
207:                    return b.negate();
208:                }
209:            }
210:
211:            /**
212:             * The default implementation of ~ operator
213:             */
214:            public static class Not extends UnaryOperator {
215:
216:                static Not instance = new Not();
217:
218:                public Object op_int(int n) {
219:                    int l = ~n;
220:                    if (l >= 0 && l < BinaryOperator.SmallIntSize) {
221:                        return BinaryOperator.smallInt[l];
222:                    }
223:                    return new Integer(l);
224:                }
225:
226:                public Object op_long(long n) {
227:                    return new Long(~n);
228:                }
229:
230:                public Object op_bint(BigInteger n) {
231:                    return n.not();
232:                }
233:            }
234:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.