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: }
|