001: /*
002: * @(#)BooleanOperator.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.lang.reflect.Array;
013: import java.math.BigDecimal;
014: import java.math.BigInteger;
015:
016: /**
017: * Abstract base class of boolean operations
018: *
019: * @see pnuts.lang.BinaryOperator
020: */
021: public abstract class BooleanOperator implements Serializable {
022:
023: protected boolean op_boolean(boolean b1, boolean b2) {
024: throw new IllegalArgumentException(b1 + ", " + b2);
025: }
026:
027: protected boolean op_int(int i1, int i2) {
028: throw new IllegalArgumentException(i1 + ", " + i2);
029: }
030:
031: protected boolean op_long(long l1, long l2) {
032: throw new IllegalArgumentException(l1 + ", " + l2);
033: }
034:
035: protected boolean op_float(float f1, float f2) {
036: throw new IllegalArgumentException(f1 + ", " + f2);
037: }
038:
039: protected boolean op_double(double d1, double d2) {
040: throw new IllegalArgumentException(d1 + ", " + d2);
041: }
042:
043: protected boolean op_bdec(BigDecimal d1, BigDecimal d2) {
044: throw new IllegalArgumentException(d1 + ", " + d2);
045: }
046:
047: protected boolean op_bint(BigInteger b1, BigInteger b2) {
048: throw new IllegalArgumentException(b1 + ", " + b2);
049: }
050:
051: protected boolean op_numeric(Numeric n1, Object n2) {
052: throw new IllegalArgumentException(n1 + ", " + n2);
053: }
054:
055: protected boolean op_numeric(Object n1, Numeric n2) {
056: throw new IllegalArgumentException(n1 + ", " + n2);
057: }
058:
059: protected boolean op_object(Object o1, Object o2) {
060: throw new IllegalArgumentException(o1 + ", " + o2);
061: }
062:
063: protected boolean op_string(String o1, Object o2) {
064: throw new IllegalArgumentException(o1 + ", " + o2);
065: }
066:
067: protected boolean op_string(Object o1, String o2) {
068: throw new IllegalArgumentException(o1 + ", " + o2);
069: }
070:
071: public boolean operateOn(Object n1, Object n2) {
072: int t1;
073: int t2;
074: if (n1 instanceof Integer) {
075: t1 = 1;
076: } else if (n1 instanceof Character) {
077: t1 = 2;
078: } else if (n1 instanceof Byte) {
079: t1 = 4;
080: } else if (n1 instanceof Short) {
081: t1 = 8;
082: } else if (n1 instanceof Long) {
083: t1 = 16;
084: } else if (n1 instanceof Float) {
085: t1 = 32;
086: } else if (n1 instanceof Double) {
087: t1 = 64;
088: } else if (n1 instanceof String) {
089: t1 = 128;
090: } else if (n1 instanceof BigDecimal) {
091: t1 = 256;
092: } else if (n1 instanceof BigInteger) {
093: t1 = 512;
094: } else if (n1 instanceof Boolean) {
095: t1 = 1024;
096: } else if (n1 instanceof Numeric) {
097: t1 = 2048;
098: } else {
099: t1 = 0;
100: }
101: if (n2 instanceof Integer) {
102: t2 = 1;
103: } else if (n2 instanceof Character) {
104: t2 = 2;
105: } else if (n2 instanceof Byte) {
106: t2 = 4;
107: } else if (n2 instanceof Short) {
108: t2 = 8;
109: } else if (n2 instanceof Long) {
110: t2 = 16;
111: } else if (n2 instanceof Float) {
112: t2 = 32;
113: } else if (n2 instanceof Double) {
114: t2 = 64;
115: } else if (n2 instanceof String) {
116: t2 = 128;
117: } else if (n2 instanceof BigDecimal) {
118: t2 = 256;
119: } else if (n2 instanceof BigInteger) {
120: t2 = 512;
121: } else if (n2 instanceof Boolean) {
122: t2 = 1024;
123: } else if (n2 instanceof Numeric) {
124: t2 = 2048;
125: } else {
126: t2 = 0;
127: }
128:
129: switch ((t1 << 16) | t2) {
130:
131: case (1 << 16) + 4: // int, byte
132: case (1 << 16) + 8: // int, short
133: case (4 << 16) + 1: // byte, int
134: case (4 << 16) + 4: // byte, byte
135: case (4 << 16) + 8: // byte, short
136: case (8 << 16) + 1: // short, int
137: case (8 << 16) + 4: // short, byte
138: case (8 << 16) + 8: // short, short
139: return op_int(((Number) n1).intValue(), ((Number) n2)
140: .intValue());
141:
142: case (1 << 16) + 1: // int, int
143: return op_int(((Integer) n1).intValue(), ((Integer) n2)
144: .intValue());
145:
146: case (4 << 16) + 2: // byte, char
147: case (8 << 16) + 2: // short, char
148: return op_int(((Number) n1).intValue(),
149: (int) ((Character) n2).charValue());
150:
151: case (1 << 16) + 2: // int, char
152: return op_int(((Integer) n1).intValue(),
153: (int) ((Character) n2).charValue());
154:
155: case (1 << 16) + 16: // int, long
156: case (16 << 16) + 1: // long, int
157: case (16 << 16) + 16: // long, long
158: return op_long(((Number) n1).longValue(), ((Number) n2)
159: .longValue());
160:
161: case (2 << 16) + 4: // char, byte
162: case (2 << 16) + 8: // char, short
163: return op_int((int) ((Character) n1).charValue(),
164: ((Number) n2).intValue());
165:
166: case (2 << 16) + 1: // char, int
167: return op_int((int) ((Character) n1).charValue(),
168: ((Integer) n2).intValue());
169:
170: case (2 << 16) + 2: // char, char
171: return op_int((int) ((Character) n1).charValue(),
172: (int) ((Character) n2).charValue());
173:
174: case (2 << 16) + 16: // char, long
175: return op_long((long) ((Character) n1).charValue(),
176: ((Long) n2).longValue());
177:
178: case (16 << 16) + 2: // long, char
179: return op_long(((Long) n1).longValue(),
180: (long) ((Character) n2).charValue());
181:
182: case (1 << 16) + 32: // int,float
183: case (16 << 16) + 32: // long,float
184: case (32 << 16) + 1: // float, int
185: case (32 << 16) + 16: // float, long
186: case (32 << 16) + 32: // float, float
187: return op_float(((Number) n1).floatValue(), ((Number) n2)
188: .floatValue());
189:
190: case (1 << 16) + 64: // int, double
191: case (16 << 16) + 64: // long, double
192: case (32 << 16) + 64: // float, double
193: case (64 << 16) + 1: // double , int
194: case (64 << 16) + 16: // double, long
195: case (64 << 16) + 32: // double, float
196: case (64 << 16) + 64: // double, double
197: return op_double(((Number) n1).doubleValue(), ((Number) n2)
198: .doubleValue());
199:
200: case (2 << 16) + 32: // char, float
201: return op_float((float) ((Character) n1).charValue(),
202: ((Float) n2).floatValue());
203:
204: case (32 << 16) + 2: // float, char
205: return op_float(((Float) n1).floatValue(),
206: (float) ((Character) n2).charValue());
207:
208: case (2 << 16) + 64: // char, double
209: return op_double((double) ((Character) n1).charValue(),
210: ((Double) n2).doubleValue());
211:
212: case (64 << 16) + 2: // double, char
213: return op_double(((Double) n1).doubleValue(),
214: (double) ((Character) n2).charValue());
215:
216: case (1 << 16) + 512: // int, bigint
217: case (4 << 16) + 512: // byte, bigint
218: case (8 << 16) + 512: // short, bigint
219: case (16 << 16) + 512: // long, bigint
220: return op_bint(BigInteger
221: .valueOf(((Number) n1).longValue()),
222: (BigInteger) n2);
223:
224: case (2 << 16) + 512: // char, bigint
225: return op_bint(BigInteger.valueOf((long) ((Character) n1)
226: .charValue()), (BigInteger) n2);
227:
228: case (512 << 16) + 1: // bigint, int
229: case (512 << 16) + 4: // bigint, byte
230: case (512 << 16) + 8: // bigint, short
231: case (512 << 16) + 16: // bigint, long
232: return op_bint((BigInteger) n1, BigInteger
233: .valueOf(((Number) n2).longValue()));
234:
235: case (512 << 16) + 2: // bigint, char
236: return op_bint((BigInteger) n1, BigInteger
237: .valueOf((long) ((Character) n2).charValue()));
238:
239: case (512 << 16) + 512: // bigint, bigint
240: return op_bint((BigInteger) n1, (BigInteger) n2);
241:
242: case (512 << 16) + 32: // bigint, float
243: {
244: float ff = ((Float) n2).floatValue();
245: return op_bdec(new BigDecimal((BigInteger) n1),
246: doubleToDecimal(ff));
247: }
248: case (512 << 16) + 64: // bigint, double
249: {
250: double dd = ((Double) n2).doubleValue();
251: return op_bdec(new BigDecimal((BigInteger) n1),
252: doubleToDecimal(dd));
253: }
254: case (32 << 16) + 512: // float, bigint
255: {
256: float ff = ((Float) n1).floatValue();
257: return op_bdec(doubleToDecimal(ff), new BigDecimal(
258: (BigInteger) n2));
259: }
260: case (64 << 16) + 512: // double, bigint
261: {
262: double dd = ((Double) n1).doubleValue();
263: return op_bdec(doubleToDecimal(dd), new BigDecimal(
264: (BigInteger) n2));
265: }
266: case (1 << 16) + 256: // int, decimal
267: case (4 << 16) + 256: // byte, decimal
268: case (8 << 16) + 256: // short, decimal
269: case (16 << 16) + 256: // long, decimal
270: return op_bdec(new BigDecimal(((Number) n1).doubleValue()),
271: (BigDecimal) n2);
272:
273: case (2 << 16) + 256: // char, decimal
274: return op_bdec(new BigDecimal((double) ((Character) n1)
275: .charValue()), (BigDecimal) n2);
276:
277: case (32 << 16) + 256: // float, decimal
278: {
279: float ff = ((Float) n1).floatValue();
280: return op_bdec(doubleToDecimal(ff), (BigDecimal) n2);
281: }
282: case (64 << 16) + 256: // double, decimal
283: {
284: double dd = ((Double) n1).doubleValue();
285: return op_bdec(doubleToDecimal(dd), (BigDecimal) n2);
286: }
287: case (256 << 16) + 1: // decimal, int
288: case (256 << 16) + 4: // decimal, byte
289: case (256 << 16) + 8: // decimal, short
290: case (256 << 16) + 16: // decimal, long
291: return op_bdec((BigDecimal) n1, new BigDecimal(
292: ((Number) n2).doubleValue()));
293:
294: case (256 << 16) + 2: // decimal, char
295: return op_bdec((BigDecimal) n1, new BigDecimal(
296: (double) ((Character) n2).charValue()));
297:
298: case (256 << 16) + 32: // decimal, float
299: float ff = ((Float) n2).floatValue();
300: return op_bdec((BigDecimal) n1, doubleToDecimal(ff));
301: case (256 << 16) + 64: // decimal, double
302: double dd = ((Double) n2).doubleValue();
303: return op_bdec((BigDecimal) n1, doubleToDecimal(dd));
304:
305: case (256 << 16) + 256: // decimal, decimal
306: return op_bdec((BigDecimal) n1, (BigDecimal) n2);
307:
308: case (1024 << 16) + 1024: // boolean, boolean
309: return op_boolean(((Boolean) n1).booleanValue(),
310: ((Boolean) n2).booleanValue());
311:
312: case (128 << 16) + 1: // String, int
313: case (128 << 16) + 2: // String, char
314: case (128 << 16) + 4: // String, byte
315: case (128 << 16) + 8: // String, short
316: case (128 << 16) + 16: // String, long
317: case (128 << 16) + 32: // String, float
318: case (128 << 16) + 64: // String, double
319: case (128 << 16) + 128: // String, String
320: case (128 << 16) + 256: // String, BigDecimal
321: case (128 << 16) + 512: // String, BigInteger
322: case (128 << 16) + 1024: // String, boolean
323: case (128 << 16) + 2048: // String, Numeric
324: case (128 << 16) + 0: // String, Object
325: return op_string((String) n1, n2);
326:
327: case (1 << 16) + 128: // int, String
328: case (2 << 16) + 128: // char, String
329: case (4 << 16) + 128: // byte, String
330: case (8 << 16) + 128: // short, String
331: case (16 << 16) + 128: // long, String
332: case (32 << 16) + 128: // float, String
333: case (64 << 16) + 128: // double, String
334: case (256 << 16) + 128: // BigDecimal, String
335: case (512 << 16) + 128: // BigInteger, String
336: case (1024 << 16) + 128: // boolean, String
337: case (2048 << 16) + 128: // Numeric, String
338: case (0 << 16) + 128: // Object, String
339: return op_string(n1, (String) n2);
340:
341: case (2048 << 16) + 1: // Numeric, int
342: case (2048 << 16) + 2: // Numeric, char
343: case (2048 << 16) + 4: // Numeric, byte
344: case (2048 << 16) + 8: // Numeric, short
345: case (2048 << 16) + 16: // Numeric, long
346: case (2048 << 16) + 32: // Numeric, float
347: case (2048 << 16) + 64: // Numeric, double
348: case (2048 << 16) + 256: // Numeric, BigDecimal
349: case (2048 << 16) + 512: // Numeric, BigInteger
350: case (2048 << 16) + 2048: // Numeric, Numeric
351: return op_numeric((Numeric) n1, n2);
352:
353: case (1 << 16) + 2048: // int, Numeric
354: case (2 << 16) + 2048: // char, Numeric
355: case (4 << 16) + 2048: // byte, Numeric
356: case (8 << 16) + 2048: // short, Numeric
357: case (16 << 16) + 2048: // long, Numeric
358: case (32 << 16) + 2048: // float, Numeric
359: case (64 << 16) + 2048: // double, Numeric
360: case (256 << 16) + 2048: // BigDecimal, Numeric
361: case (512 << 16) + 2048: // BigInteger, Numeric
362: return op_numeric(n1, (Numeric) n2);
363:
364: default:
365: return op_object(n1, n2);
366: }
367: }
368:
369: static BigDecimal doubleToDecimal(double d) {
370: return (BigDecimal) Runtime.decimalNumber("" + d, 10);
371: }
372:
373: /**
374: * The default implementation of == operator
375: */
376: public static class EQ extends BooleanOperator {
377:
378: static EQ instance = new EQ();
379:
380: public boolean op_int(int i1, int i2) {
381: return i1 == i2;
382: }
383:
384: public boolean op_long(long l1, long l2) {
385: return l1 == l2;
386: }
387:
388: public boolean op_float(float f1, float f2) {
389: return f1 == f2;
390: }
391:
392: public boolean op_double(double d1, double d2) {
393: return d1 == d2;
394: }
395:
396: public boolean op_bdec(BigDecimal d1, BigDecimal d2) {
397: return d1.compareTo(d2) == 0;
398: }
399:
400: public boolean op_bint(BigInteger b1, BigInteger b2) {
401: return b1.compareTo(b2) == 0;
402: }
403:
404: public boolean op_boolean(boolean b1, boolean b2) {
405: return b1 == b2;
406: }
407:
408: public boolean op_object(Object n1, Object n2) {
409: if (n1 == null) {
410: return n2 == null;
411: } else if (n2 == null) {
412: return false;
413: } else if (Runtime.isArray(n1) && Runtime.isArray(n2)) {
414: int len = 0;
415: if ((len = Runtime.getArrayLength(n2)) == Runtime
416: .getArrayLength(n1)) {
417: for (int i = 0; i < len; i++) {
418: Object e1 = Array.get(n1, i);
419: Object e2 = Array.get(n2, i);
420: if (e1 == null) {
421: if (e2 != null) {
422: return false;
423: }
424: } else {
425: if (Runtime.isArray(e1)) {
426: if (!operateOn(e1, e2)) {
427: return false;
428: }
429: } else if (!e1.equals(e2)) {
430: return false;
431: }
432: }
433: }
434: return true;
435: } else {
436: return false;
437: }
438: } else {
439: return n1.equals(n2);
440: }
441: }
442:
443: public boolean op_numeric(Numeric n1, Object n2) {
444: if (n1 == null) {
445: return n2 == null;
446: } else if (n2 == null) {
447: return false;
448: } else {
449: return n1.equals(n2);
450: }
451: }
452:
453: public boolean op_numeric(Object n1, Numeric n2) {
454: if (n1 == null) {
455: return n2 == null;
456: } else if (n2 == null) {
457: return false;
458: } else {
459: return n1.equals(n2);
460: }
461: }
462:
463: public boolean op_string(String s, Object o) {
464: return s.equals(o);
465: }
466:
467: public boolean op_string(Object o, String s) {
468: return s.equals(o);
469: }
470: }
471:
472: /**
473: * The default implementation of >= operator
474: */
475: public static class GE extends BooleanOperator {
476:
477: static GE instance = new GE();
478:
479: public boolean op_int(int i1, int i2) {
480: return i1 >= i2;
481: }
482:
483: public boolean op_long(long l1, long l2) {
484: return l1 >= l2;
485: }
486:
487: public boolean op_float(float f1, float f2) {
488: return f1 >= f2;
489: }
490:
491: public boolean op_double(double d1, double d2) {
492: return d1 >= d2;
493: }
494:
495: public boolean op_bdec(BigDecimal d1, BigDecimal d2) {
496: return d1.compareTo(d2) >= 0;
497: }
498:
499: public boolean op_bint(BigInteger b1, BigInteger b2) {
500: return b1.compareTo(b2) >= 0;
501: }
502:
503: public boolean op_numeric(Numeric n1, Object n2) {
504: return n1.compareTo(n2) >= 0;
505: }
506:
507: public boolean op_numeric(Object n1, Numeric n2) {
508: return n2.compareTo(n1) <= 0;
509: }
510:
511: public boolean op_string(Object o, String s) {
512: if (s != null && o != null) {
513: return s.compareTo((String) o) < 0;
514: } else {
515: return super .op_string(o, s);
516: }
517: }
518:
519: public boolean op_string(String s, Object o) {
520: if (s != null && o != null) {
521: return s.compareTo((String) o) >= 0;
522: } else {
523: return super .op_string(s, o);
524: }
525: }
526:
527: public boolean op_object(Object n1, Object n2) {
528: /*
529: if (n1 instanceof Comparable) {
530: return ((Comparable) n1).compareTo(n2) >= 0;
531: } else if (n2 instanceof Comparable) {
532: return ((Comparable) n2).compareTo(n1) < 0;
533: } else {
534: return super.op_object(n1, n2);
535: }
536: */
537: return Runtime.compareObjects(n1, n2) >= 0;
538: }
539: }
540:
541: /**
542: * The default implementation of > operator
543: */
544: public static class GT extends BooleanOperator {
545:
546: static GT instance = new GT();
547:
548: public boolean op_int(int i1, int i2) {
549: return i1 > i2;
550: }
551:
552: public boolean op_long(long l1, long l2) {
553: return l1 > l2;
554: }
555:
556: public boolean op_float(float f1, float f2) {
557: return f1 > f2;
558: }
559:
560: public boolean op_double(double d1, double d2) {
561: return d1 > d2;
562: }
563:
564: public boolean op_bdec(BigDecimal d1, BigDecimal d2) {
565: return d1.compareTo(d2) > 0;
566: }
567:
568: public boolean op_bint(BigInteger b1, BigInteger b2) {
569: return b1.compareTo(b2) > 0;
570: }
571:
572: public boolean op_numeric(Numeric n1, Object n2) {
573: return n1.compareTo(n2) > 0;
574: }
575:
576: public boolean op_numeric(Object n1, Numeric n2) {
577: return n2.compareTo(n1) < 0;
578: }
579:
580: public boolean op_string(Object o, String s) {
581: if (s != null && o != null) {
582: return s.compareTo((String) o) < 0;
583: } else {
584: return super .op_string(o, s);
585: }
586: }
587:
588: public boolean op_string(String s, Object o) {
589: if (s != null && o != null) {
590: return s.compareTo((String) o) > 0;
591: } else {
592: return super .op_string(s, o);
593: }
594: }
595:
596: public boolean op_object(Object n1, Object n2) {
597: /*
598: if (n1 instanceof Comparable) {
599: return ((Comparable) n1).compareTo(n2) > 0;
600: } else if (n2 instanceof Comparable) {
601: return ((Comparable) n2).compareTo(n1) <= 0;
602: } else {
603: return super.op_object(n1, n2);
604: }
605: */
606: return Runtime.compareObjects(n1, n2) > 0;
607: }
608: }
609:
610: /**
611: * The default implementation of <= operator
612: */
613: public static class LE extends BooleanOperator {
614:
615: static LE instance = new LE();
616:
617: public boolean op_int(int i1, int i2) {
618: return i1 <= i2;
619: }
620:
621: public boolean op_long(long l1, long l2) {
622: return l1 <= l2;
623: }
624:
625: public boolean op_float(float f1, float f2) {
626: return f1 <= f2;
627: }
628:
629: public boolean op_double(double d1, double d2) {
630: return d1 <= d2;
631: }
632:
633: public boolean op_bdec(BigDecimal d1, BigDecimal d2) {
634: return d1.compareTo(d2) <= 0;
635: }
636:
637: public boolean op_bint(BigInteger b1, BigInteger b2) {
638: return b1.compareTo(b2) <= 0;
639: }
640:
641: public boolean op_numeric(Numeric n1, Object n2) {
642: return n1.compareTo(n2) <= 0;
643: }
644:
645: public boolean op_numeric(Object n1, Numeric n2) {
646: return n2.compareTo(n1) >= 0;
647: }
648:
649: public boolean op_string(Object o, String s) {
650: if (s != null && o != null) {
651: return s.compareTo((String) o) >= 0;
652: } else {
653: return super .op_string(o, s);
654: }
655: }
656:
657: public boolean op_string(String s, Object o) {
658: if (s != null && o != null) {
659: return s.compareTo((String) o) <= 0;
660: } else {
661: return super .op_string(s, o);
662: }
663: }
664:
665: public boolean op_object(Object n1, Object n2) {
666: /*
667: if (n1 instanceof Comparable) {
668: return ((Comparable) n1).compareTo(n2) <= 0;
669: } else if (n2 instanceof Comparable) {
670: return ((Comparable) n2).compareTo(n1) > 0;
671: } else {
672: return super.op_object(n1, n2);
673: }
674: */
675: return Runtime.compareObjects(n1, n2) <= 0;
676:
677: }
678: }
679:
680: /**
681: * The default implementation of < operator
682: */
683: public static class LT extends BooleanOperator {
684:
685: static LT instance = new LT();
686:
687: public boolean op_int(int i1, int i2) {
688: return i1 < i2;
689: }
690:
691: public boolean op_long(long l1, long l2) {
692: return l1 < l2;
693: }
694:
695: public boolean op_float(float f1, float f2) {
696: return f1 < f2;
697: }
698:
699: public boolean op_double(double d1, double d2) {
700: return d1 < d2;
701: }
702:
703: public boolean op_bdec(BigDecimal d1, BigDecimal d2) {
704: return d1.compareTo(d2) < 0;
705: }
706:
707: public boolean op_bint(BigInteger b1, BigInteger b2) {
708: return b1.compareTo(b2) < 0;
709: }
710:
711: public boolean op_numeric(Numeric n1, Object n2) {
712: return n1.compareTo(n2) < 0;
713: }
714:
715: public boolean op_numeric(Object n1, Numeric n2) {
716: return n2.compareTo(n1) > 0;
717: }
718:
719: public boolean op_string(Object o, String s) {
720: if (s != null && o != null) {
721: return s.compareTo((String) o) > 0;
722: } else {
723: return super .op_string(o, s);
724: }
725: }
726:
727: public boolean op_string(String s, Object o) {
728: if (s != null && o != null) {
729: return s.compareTo((String) o) < 0;
730: } else {
731: return super .op_string(s, o);
732: }
733: }
734:
735: public boolean op_object(Object n1, Object n2) {
736: /*
737: if (n1 instanceof Comparable) {
738: return ((Comparable) n1).compareTo(n2) < 0;
739: } else if (n2 instanceof Comparable) {
740: return ((Comparable) n2).compareTo(n1) >= 0;
741: } else {
742: return super.op_object(n1, n2);
743: }
744: */
745: return Runtime.compareObjects(n1, n2) < 0;
746: }
747: }
748: }
|